diff options
| author | Marak <[email protected]> | 2015-07-08 23:56:22 -0700 |
|---|---|---|
| committer | Marak <[email protected]> | 2015-07-08 23:56:22 -0700 |
| commit | 4c1b454b8e3c8d87f2569a2ce0d7730dc31ee821 (patch) | |
| tree | 6fcbdb8eee0a7365c78043008c8dd5b6a51d39c5 /lib/random.js | |
| parent | edb7d482a40e98ce25cd6ba296818be14f291aa1 (diff) | |
| download | faker-4c1b454b8e3c8d87f2569a2ce0d7730dc31ee821.tar.xz faker-4c1b454b8e3c8d87f2569a2ce0d7730dc31ee821.zip | |
[api] [refactor] Rename `array_element` and `object_element` to camelCase. Set default max random number to 99999. Added default arguments to some methods.
Diffstat (limited to 'lib/random.js')
| -rw-r--r-- | lib/random.js | 70 |
1 files changed, 35 insertions, 35 deletions
diff --git a/lib/random.js b/lib/random.js index 7d592849..2d37c551 100644 --- a/lib/random.js +++ b/lib/random.js @@ -5,50 +5,50 @@ function Random (faker) { // 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; - } - - 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; + if (typeof options === "number") { + options = { + max: options + }; + } + + options = options || {}; + + if (typeof options.min === "undefined") { + options.min = 0; + } + + 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.array_element = function (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.object_element = function (object, field) { - object = object || {}; + this.objectElement = function (object, field) { + object = object || { "foo": "bar", "too": "car" }; var array = Object.keys(object); - var key = faker.random.array_element(array); + var key = faker.random.arrayElement(array); return field === "key" ? key : object[key]; } @@ -68,7 +68,7 @@ function Random (faker) { } return this; - + } module['exports'] = Random; |
