diff options
Diffstat (limited to 'lib/random.js')
| -rw-r--r-- | lib/random.js | 137 |
1 files changed, 83 insertions, 54 deletions
diff --git a/lib/random.js b/lib/random.js index a4bc1858..fc7e54d9 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,57 +1,86 @@ var mersenne = require('../vendor/mersenne'); -var faker = require('../index'); - -var random = { - // returns a single random number based on a max number or range - number: function (options) { - - if (typeof options === "number") { - options = { - max: options - }; - } - - options = options || {}; - - if (typeof options.min === "undefined") { - options.min = 0; - } - - if (typeof options.max === "undefined") { - options.max = 1; - } - if (typeof options.precision === "undefined") { - options.precision = 1; - } - - // Make the range inclusive of the max value - var max = options.max; - if (max > 0) { - max += options.precision; - } - - var randomNumber = options.precision * Math.floor( - mersenne.rand(max / options.precision, options.min / options.precision)); - - return randomNumber; - - }, - - // takes an array and returns the array randomly sorted - array_element: function (array) { - array = array || ["a", "b", "c"]; - var r = faker.random.number({ max: array.length - 1 }); - return array[r]; - }, - - // takes an object and returns the randomly key or value - object_element: function (object, field) { - object = object || {}; - var array = Object.keys(object); - var key = faker.random.array_element(array); - - return field === "key" ? key : object[key]; + +function Random (faker, seed) { + // Use a user provided seed if it exists + if (seed) { + if (Array.isArray(seed) && seed.length) { + mersenne.seed_array(seed); + } + else { + mersenne.seed(seed); + } + } + // returns a single random number based on a max number or range + this.number = function (options) { + + if (typeof options === "number") { + options = { + max: options + }; + } + + options = options || {}; + + if (typeof options.min === "undefined") { + options.min = 0; } -}; -module.exports = random; + if (typeof options.max === "undefined") { + options.max = 99999; + } + if (typeof options.precision === "undefined") { + options.precision = 1; + } + + // Make the range inclusive of the max value + var max = options.max; + if (max >= 0) { + max += options.precision; + } + + var randomNumber = options.precision * Math.floor( + mersenne.rand(max / options.precision, options.min / options.precision)); + + return randomNumber; + + } + + // takes an array and returns a random element of the array + this.arrayElement = function (array) { + array = array || ["a", "b", "c"]; + var r = faker.random.number({ max: array.length - 1 }); + return array[r]; + } + + // takes an object and returns the randomly key or value + this.objectElement = function (object, field) { + object = object || { "foo": "bar", "too": "car" }; + var array = Object.keys(object); + var key = faker.random.arrayElement(array); + + return field === "key" ? key : object[key]; + } + + this.uuid = function () { + var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; + var replacePlaceholders = function (placeholder) { + var random = Math.random()*16|0; + var value = placeholder == 'x' ? random : (random &0x3 | 0x8); + return value.toString(16); + }; + return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); + } + + this.boolean =function () { + return !!faker.random.number(1) + } + + return this; + +} + +module['exports'] = Random; + + + +// module.exports = random; |
