mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
refactor: move randomVal to a lib
This commit is contained in:
18
website/common/script/libs/randomVal.js
Normal file
18
website/common/script/libs/randomVal.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
function trueRandom () {
|
||||
return Math.random();
|
||||
}
|
||||
|
||||
// Get a random property from an object
|
||||
// returns random property (the value)
|
||||
module.exports = function randomVal (obj, options = {}) {
|
||||
let array = options.key ? _.keys(obj) : _.values(obj);
|
||||
let random = (options.predictableRandom || trueRandom)();
|
||||
|
||||
array.sort();
|
||||
|
||||
let randomIndex = Math.floor(random * array.length);
|
||||
|
||||
return array[randomIndex];
|
||||
};
|
||||
Reference in New Issue
Block a user