UNPKG

2.36 kBJavaScriptView Raw
1var weighted = require('../')
2 , fixtures = {}
3
4function generateOptions(n) {
5 var options = []
6
7 for (;n--;) {
8 options.push(Math.random().toString().slice(2))
9 }
10
11 return options
12}
13
14function generateWeights(n) {
15 var weights = []
16
17 for (;n--;) {
18 weights.push(1)
19 }
20
21 return weights
22}
23
24module.exports = [2, 8, 512].reduce(function (exports, n) {
25 fixtures[n] = {
26 optionsArr: generateOptions(n)
27 , weightsArr: generateWeights(n)
28 , optionsObj: generateOptions(n).reduce(function (obj, key) {
29 obj[key] = 1
30
31 return obj
32 }, {})
33 , newOptionsArr: generateOptions(n)
34 , newWeightsArr: generateWeights(n)
35 , newOptionsObj: generateOptions(n).reduce(function (obj, key) {
36 obj[key] = 1
37
38 return obj
39 }, {})
40 }
41
42 function reset(arr) {
43 delete arr.__weighted_total
44 }
45
46 exports['selecting between ' + n + ' options in new Arrays'] = function () {
47 reset(fixtures[n].newOptionsArr)
48 reset(fixtures[n].newWeightsArr)
49
50 weighted.select(fixtures[n].newOptionsArr, fixtures[n].newWeightsArr)
51 }
52 exports['selecting between ' + n + ' options in a new Object'] = function () {
53 reset(fixtures[n].newOptionsObj)
54
55 weighted.select(fixtures[n].newOptionsObj)
56 }
57 exports['selecting between ' + n + ' options in old Arrays'] = function () {
58 weighted.select(fixtures[n].optionsArr, fixtures[n].weightsArr)
59 }
60 exports['selecting between ' + n + ' options in an old Object'] = function () {
61 weighted.select(fixtures[n].optionsObj)
62 }
63 exports['selecting between ' + n + ' options in new Arrays, given total'] = function () {
64 reset(fixtures[n].newOptionsArr)
65 reset(fixtures[n].newWeightsArr)
66
67 weighted.select(fixtures[n].newOptionsArr, fixtures[n].newWeightsArr, {
68 total: n
69 })
70 }
71 exports['selecting between ' + n + ' options in a new Object, given total'] = function () {
72 reset(fixtures[n].newOptionsObj)
73
74 weighted.select(fixtures[n].newOptionsObj, {
75 total: n
76 })
77 }
78 exports['selecting between ' + n + ' options in old Arrays, given total'] = function () {
79 weighted.select(fixtures[n].optionsArr, fixtures[n].weightsArr, {
80 total: n
81 })
82 }
83 exports['selecting between ' + n + ' options in an old Object, given total'] = function () {
84 weighted.select(fixtures[n].optionsObj, {
85 total: n
86 })
87 }
88
89 return exports
90}, {})