mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
refactor: remove seeding from randomVal
This commit is contained in:
@@ -40,12 +40,11 @@ describe('shared.fns.randomVal', () => {
|
||||
let result = randomVal(obj, {
|
||||
user,
|
||||
seed: 100,
|
||||
randomFunc: randomSpy,
|
||||
predictableRandom: randomSpy,
|
||||
});
|
||||
|
||||
expect(Math.random).to.not.be.called;
|
||||
expect(randomSpy).to.be.calledOnce;
|
||||
expect(randomSpy).to.be.calledWith(user, 100);
|
||||
expect(result).to.equal(2);
|
||||
});
|
||||
|
||||
|
||||
@@ -3,17 +3,17 @@ import _ from 'lodash';
|
||||
// Get a random property from an object
|
||||
// returns random property (the value)
|
||||
|
||||
function randomGenerator (user, seed, providedRandom) {
|
||||
return providedRandom ? providedRandom(user, seed) : Math.random();
|
||||
function trueRandom () {
|
||||
return Math.random();
|
||||
}
|
||||
|
||||
module.exports = function randomVal (obj, options = {}) {
|
||||
let array = options.key ? _.keys(obj) : _.values(obj);
|
||||
let rand = randomGenerator(options.user, options.seed, options.randomFunc);
|
||||
let random = (options.predictableRandom || trueRandom)();
|
||||
|
||||
array.sort();
|
||||
|
||||
let randomIndex = Math.floor(rand * array.length);
|
||||
let randomIndex = Math.floor(random * array.length);
|
||||
|
||||
return array[randomIndex];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user