Skip to content

Commit 4d80335

Browse files
committed
feat(oneOf): Support maker functions as items
1 parent ab2e6dd commit 4d80335

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

oneOf.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
var hash = require('./hash')
22

3-
function oneOf(input, samples) {
4-
return input != null && samples != null
5-
? samples[hash(input) % samples.length]
6-
: oneOfCurried(input)
3+
function oneOf(a, b) {
4+
return a != null && b != null ? oneOfMain(a, b) : oneOfCurried(a)
5+
}
6+
7+
function oneOfMain(input, samples) {
8+
var id = hash(input)
9+
var result = samples[id % samples.length]
10+
return typeof result === 'function' ? result(hash([id, 'oneOf'])) : result
711
}
812

913
function oneOfCurried(samples) {

tests/oneOf.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const tap = require('tap')
2+
const { oneOf } = require('..')
3+
4+
tap.test('makers as items', t => {
5+
const mod2 = id => id % 2
6+
7+
let i = -1
8+
9+
while (++i < 50) {
10+
const result = oneOf(i, [mod2])
11+
t.ok(0 <= result && result <= 1)
12+
}
13+
14+
t.end()
15+
})

0 commit comments

Comments
 (0)