mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
refactor: remove seeding from randomVal
This commit is contained in:
@@ -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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user