refactor: move randomVal to a lib

This commit is contained in:
Blade Barringer
2016-09-26 08:14:18 -05:00
parent 331993c1df
commit 913cb16638
9 changed files with 13 additions and 15 deletions

View 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];
};