refactor: remove seeding from randomVal

This commit is contained in:
Blade Barringer
2016-09-24 21:25:14 -05:00
parent 136e2de125
commit 331993c1df
2 changed files with 5 additions and 6 deletions

View File

@@ -40,12 +40,11 @@ describe('shared.fns.randomVal', () => {
let result = randomVal(obj, { let result = randomVal(obj, {
user, user,
seed: 100, seed: 100,
randomFunc: randomSpy, predictableRandom: randomSpy,
}); });
expect(Math.random).to.not.be.called; expect(Math.random).to.not.be.called;
expect(randomSpy).to.be.calledOnce; expect(randomSpy).to.be.calledOnce;
expect(randomSpy).to.be.calledWith(user, 100);
expect(result).to.equal(2); expect(result).to.equal(2);
}); });

View File

@@ -3,17 +3,17 @@ import _ from 'lodash';
// Get a random property from an object // Get a random property from an object
// returns random property (the value) // returns random property (the value)
function randomGenerator (user, seed, providedRandom) { function trueRandom () {
return providedRandom ? providedRandom(user, seed) : Math.random(); return Math.random();
} }
module.exports = function randomVal (obj, options = {}) { module.exports = function randomVal (obj, options = {}) {
let array = options.key ? _.keys(obj) : _.values(obj); let array = options.key ? _.keys(obj) : _.values(obj);
let rand = randomGenerator(options.user, options.seed, options.randomFunc); let random = (options.predictableRandom || trueRandom)();
array.sort(); array.sort();
let randomIndex = Math.floor(rand * array.length); let randomIndex = Math.floor(random * array.length);
return array[randomIndex]; return array[randomIndex];
}; };