Skip to content

Commit 195e72f

Browse files
committed
fix: Bring back support for maker functions as values in oneOfWeighted()
1 parent 849d846 commit 195e72f

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

oneOfWeighted.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var hash = require('./hash')
2+
var resolve = require('./utils/resolve')
23
var hash2 = hash.hash2
34

45
var EPS = 0.0001
@@ -16,7 +17,7 @@ function oneOfWeightedMain(input, samples) {
1617
for (var i = 0; i < samples.length; i++) {
1718
cumulative += samples[i][0]
1819
if (prob < cumulative) {
19-
return samples[i][1]
20+
return resolve(id, samples[i][1])
2021
}
2122
}
2223

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ oneOfWeighted('id-2', [
800800
[0.05, char],
801801
[0.05, int]
802802
])
803-
// => [Function: word] { options: [Function: wordOptions] }
803+
// => 'Ut'
804804
```
805805

806806
For each `[probability, value]` pair in the array of `values`, if the given

tests/oneOfWeighted.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const test = require('ava')
2-
const { oneOfWeighted } = require('..')
2+
const { oneOfWeighted, oneOf } = require('..')
33
const { diffBetween } = require('./utils')
44

55
const DIFF_THRESHOLD = 0.065
@@ -210,3 +210,19 @@ test(`omitted probabilities`, t => {
210210
])
211211
)
212212
})
213+
214+
215+
test(`curried makers`, t => {
216+
const n = 10000
217+
let i = -1
218+
219+
const fn = oneOfWeighted([
220+
[0.8, oneOf(['red', 'green'])],
221+
[0.2, oneOf(['blue', 'yellow'])],
222+
])
223+
224+
const seen = new Set()
225+
while (++i < n) seen.add(fn(i))
226+
227+
t.deepEqual(Array.from(seen).sort(), ['blue', 'green', 'red', 'yellow'])
228+
})

0 commit comments

Comments
 (0)