diff options
| author | LBuerstmayr <[email protected]> | 2021-03-01 22:46:26 +0100 |
|---|---|---|
| committer | Marak <[email protected]> | 2021-03-03 20:14:45 -0500 |
| commit | 7ad22c2e2aae2f5e6215bcdb91cf3fd28e727d92 (patch) | |
| tree | 03941188e92583a105c8ef17fde438fb1b52bdcf /lib/random.js | |
| parent | 58c61afb1b8baa160add593e5af7c110de011968 (diff) | |
| download | faker-7ad22c2e2aae2f5e6215bcdb91cf3fd28e727d92.tar.xz faker-7ad22c2e2aae2f5e6215bcdb91cf3fd28e727d92.zip | |
Issue 1114: New datatype module
Current status:
- moved number(), float(), hexaDecimal(), boolean(), uuid() from random to datatype
- moved respective test from random.unit to datatype.unit
- tests of moved methods in random now check if DeprecationWarning is printed and respective method in datatype module is called
- adapted all lib files that use moved methods (mostly number)
- adapted tests of respective files to spy on the correct method
- adapted README in order to promote usage of method that logs a DeprecationWarning
Diffstat (limited to 'lib/random.js')
| -rw-r--r-- | lib/random.js | 75 |
1 files changed, 16 insertions, 59 deletions
diff --git a/lib/random.js b/lib/random.js index 5c585236..5b4eebac 100644 --- a/lib/random.js +++ b/lib/random.js @@ -29,68 +29,27 @@ function Random (faker, seed) { } /** + * @Deprecated * returns a single random number based on a max number or range * * @method faker.random.number * @param {mixed} options {min, max, precision} */ 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 = 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 = Math.floor( - mersenne.rand(max / options.precision, options.min / options.precision)); - // Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01 - randomNumber = randomNumber / (1 / options.precision); - - return randomNumber; - + console.log("DeprecationWarning: Method is now located in faker.datatype.number"); + return faker.datatype.number(options); }; /** + * @Deprecated * returns a single random floating-point number based on a max number or range * * @method faker.random.float * @param {mixed} options */ this.float = function (options) { - if (typeof options === "number") { - options = { - precision: options - }; - } - options = options || {}; - var opts = {}; - for (var p in options) { - opts[p] = options[p]; - } - if (typeof opts.precision === 'undefined') { - opts.precision = 0.01; - } - return faker.random.number(opts); + console.log("DeprecationWarning: Method is now located in faker.datatype.float"); + return faker.datatype.float(options); }; /** @@ -101,7 +60,7 @@ function Random (faker, seed) { */ this.arrayElement = function (array) { array = array || ["a", "b", "c"]; - var r = faker.random.number({ max: array.length - 1 }); + var r = faker.datatype.number({ max: array.length - 1 }); return array[r]; }; @@ -116,7 +75,7 @@ function Random (faker, seed) { array = array || ["a", "b", "c"]; if (typeof count !== 'number') { - count = faker.random.number({ min: 1, max: array.length }); + count = faker.datatype.number({ min: 1, max: array.length }); } else if (count > array.length) { count = array.length; } else if (count < 0) { @@ -126,7 +85,7 @@ function Random (faker, seed) { var arrayCopy = array.slice(); var countToRemove = arrayCopy.length - count; for (var i = 0; i < countToRemove; i++) { - var indexToRemove = faker.random.number({ max: arrayCopy.length - 1 }); + var indexToRemove = faker.datatype.number({ max: arrayCopy.length - 1 }); arrayCopy.splice(indexToRemove, 1); } @@ -149,18 +108,14 @@ function Random (faker, seed) { }; /** + * @Deprecated * uuid * * @method faker.random.uuid */ this.uuid = function () { - var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; - var replacePlaceholders = function (placeholder) { - var random = faker.random.number({ min: 0, max: 15 }); - var value = placeholder == 'x' ? random : (random &0x3 | 0x8); - return value.toString(16); - }; - return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); + console.log("DeprecationWarning: Method is now located in faker.datatype.uuid"); + return faker.datatype.uuid(); }; /** @@ -169,7 +124,8 @@ function Random (faker, seed) { * @method faker.random.boolean */ this.boolean = function () { - return !!faker.random.number(1) + console.log("DeprecationWarning: Method is now located in faker.datatype.boolean"); + return faker.datatype.boolean(); }; // TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc @@ -229,7 +185,7 @@ function Random (faker, seed) { this.words = function randomWords (count) { var words = []; if (typeof count === "undefined") { - count = faker.random.number({min:1, max: 3}); + count = faker.datatype.number({min:1, max: 3}); } for (var i = 0; i<count; i++) { words.push(faker.random.word()); @@ -327,6 +283,7 @@ function Random (faker, seed) { }; /** + * @Deprecated * hexaDecimal * * @method faker.random.hexaDecimal |
