Skip to content

Commit

Permalink
feat(oneOf): Support maker functions as items
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvdm committed Apr 30, 2020
1 parent ab2e6dd commit 4d80335
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions oneOf.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
var hash = require('./hash')

function oneOf(input, samples) {
return input != null && samples != null
? samples[hash(input) % samples.length]
: oneOfCurried(input)
function oneOf(a, b) {
return a != null && b != null ? oneOfMain(a, b) : oneOfCurried(a)
}

function oneOfMain(input, samples) {
var id = hash(input)
var result = samples[id % samples.length]
return typeof result === 'function' ? result(hash([id, 'oneOf'])) : result
}

function oneOfCurried(samples) {
Expand Down
15 changes: 15 additions & 0 deletions tests/oneOf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const tap = require('tap')
const { oneOf } = require('..')

tap.test('makers as items', t => {
const mod2 = id => id % 2

let i = -1

while (++i < 50) {
const result = oneOf(i, [mod2])
t.ok(0 <= result && result <= 1)
}

t.end()
})

0 comments on commit 4d80335

Please sign in to comment.