2017-03-01 16:10:48 +00:00
|
|
|
import values from 'lodash/values';
|
|
|
|
|
import keys from 'lodash/keys';
|
2016-03-08 17:58:39 +00:00
|
|
|
|
2016-09-25 02:25:14 +00:00
|
|
|
function trueRandom () {
|
|
|
|
|
return Math.random();
|
2016-09-10 17:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-26 13:14:18 +00:00
|
|
|
// Get a random property from an object
|
|
|
|
|
// returns random property (the value)
|
2016-09-25 00:50:12 +00:00
|
|
|
module.exports = function randomVal (obj, options = {}) {
|
2017-03-01 16:10:48 +00:00
|
|
|
let array = options.key ? keys(obj) : values(obj);
|
2016-09-30 13:14:03 +00:00
|
|
|
let random = options.predictableRandom || trueRandom();
|
2016-09-10 17:37:15 +00:00
|
|
|
|
2016-03-08 17:58:39 +00:00
|
|
|
array.sort();
|
2016-09-10 17:37:15 +00:00
|
|
|
|
2016-09-25 02:25:14 +00:00
|
|
|
let randomIndex = Math.floor(random * array.length);
|
2016-09-10 17:37:15 +00:00
|
|
|
|
|
|
|
|
return array[randomIndex];
|
2016-03-08 17:58:39 +00:00
|
|
|
};
|
2016-10-21 03:03:15 +00:00
|
|
|
|
|
|
|
|
module.exports.trueRandom = trueRandom;
|