aboutsummaryrefslogtreecommitdiff
path: root/lib/random.js
diff options
context:
space:
mode:
authorDavid Kossoglyad <[email protected]>2021-03-18 02:42:43 +0200
committerDavid Kossoglyad <[email protected]>2021-03-18 02:42:43 +0200
commit20e665cae7d7a516362d12a18ed14656ee52f0aa (patch)
tree866ab2050671ce7f6aa4139c37f0d8634f2a0382 /lib/random.js
parentf4b0a92e1743b2a894811ae7b189592b2b6c1979 (diff)
parent81f27065297c3e29eb552911b9a5dc8755600efb (diff)
downloadfaker-20e665cae7d7a516362d12a18ed14656ee52f0aa.tar.xz
faker-20e665cae7d7a516362d12a18ed14656ee52f0aa.zip
Merge branch 'master' into feature/add-hebrew-localization
Diffstat (limited to 'lib/random.js')
-rw-r--r--lib/random.js178
1 files changed, 81 insertions, 97 deletions
diff --git a/lib/random.js b/lib/random.js
index 5afd27e2..6b532f25 100644
--- a/lib/random.js
+++ b/lib/random.js
@@ -1,84 +1,57 @@
var mersenne = require('../vendor/mersenne');
/**
+ * Method to reduce array of characters
+ * @param arr existing array of characters
+ * @param values array of characters which should be removed
+ * @return {*} new array without banned characters
+ */
+var arrayRemove = function (arr, values) {
+ values.forEach(function(value){
+ arr = arr.filter(function(ele){
+ return ele !== value;
+ });
+ });
+ return arr;
+};
+
+/**
*
* @namespace faker.random
*/
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);
- }
+ // Use a user provided seed if it is an array or number
+ if (Array.isArray(seed) && seed.length) {
+ mersenne.seed_array(seed);
}
+ else if(!isNaN(seed)) {
+ mersenne.seed(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("Deprecation Warning: faker.random.number 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("Deprecation Warning: faker.random.float is now located in faker.datatype.float");
+ return faker.datatype.float(options);
+ };
+
/**
* takes an array and returns a random element of the array
*
@@ -87,9 +60,9 @@ 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];
- }
+ };
/**
* takes an array and returns a subset with random elements of the array
@@ -102,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) {
@@ -112,15 +85,15 @@ 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);
}
return arrayCopy;
- }
+ };
/**
- * takes an object and returns the randomly key or value
+ * takes an object and returns a random key or value
*
* @method faker.random.objectElement
* @param {object} object
@@ -132,22 +105,18 @@ function Random (faker, seed) {
var key = faker.random.arrayElement(array);
return field === "key" ? key : object[key];
- }
+ };
/**
+ * @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("Deprecation Warning: faker.random.uuid is now located in faker.datatype.uuid");
+ return faker.datatype.uuid();
+ };
/**
* boolean
@@ -155,8 +124,9 @@ function Random (faker, seed) {
* @method faker.random.boolean
*/
this.boolean = function () {
- return !!faker.random.number(1)
- }
+ console.log("Deprecation Warning: faker.random.boolean 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
/**
@@ -204,7 +174,7 @@ function Random (faker, seed) {
var randomWordMethod = faker.random.arrayElement(wordMethods);
var result = faker.fake('{{' + randomWordMethod + '}}');
return faker.random.arrayElement(result.split(' '));
- }
+ };
/**
* randomWords
@@ -215,13 +185,13 @@ 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());
}
return words.join(' ');
- }
+ };
/**
* locale
@@ -230,7 +200,7 @@ function Random (faker, seed) {
*/
this.image = function randomImage () {
return faker.image.image();
- }
+ };
/**
* locale
@@ -245,69 +215,83 @@ function Random (faker, seed) {
* alpha. returns lower/upper alpha characters based count and upcase options
*
* @method faker.random.alpha
- * @param {mixed} options // defaults to { count: 1, upcase: false }
+ * @param {mixed} options // defaults to { count: 1, upcase: false, bannedChars: [] }
*/
this.alpha = function alpha(options) {
if (typeof options === "undefined") {
options = {
count: 1
- }
+ };
} else if (typeof options === "number") {
options = {
count: options,
- }
+ };
} else if (typeof options.count === "undefined") {
- options.count = 1
+ options.count = 1;
}
if (typeof options.upcase === "undefined") {
options.upcase = false;
}
+ if (typeof options.bannedChars ==="undefined"){
+ options.bannedChars = [];
+ }
var wholeString = "";
+ var charsArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
+ if(options.bannedChars){
+ charsArray = arrayRemove(charsArray,options.bannedChars);
+ }
for(var i = 0; i < options.count; i++) {
- wholeString += faker.random.arrayElement(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]);
+ wholeString += faker.random.arrayElement(charsArray);
}
return options.upcase ? wholeString.toUpperCase() : wholeString;
- };
+ }
/**
* alphaNumeric
*
* @method faker.random.alphaNumeric
* @param {number} count defaults to 1
+ * {mixed} options // defaults to { bannedChars: [] }
+ * options.bannedChars - array of characters which should be banned in new string
*/
- this.alphaNumeric = function alphaNumeric(count) {
+ this.alphaNumeric = function alphaNumeric(count, options) {
if (typeof count === "undefined") {
count = 1;
}
+ if (typeof options ==="undefined"){
+ options = {};
+ }
+ if (typeof options.bannedChars ==="undefined"){
+ options.bannedChars = [];
+ }
var wholeString = "";
+ var charsArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
+ if(options) {
+ if (options.bannedChars) {
+ charsArray = arrayRemove(charsArray, options.bannedChars);
+ }
+ }
for(var i = 0; i < count; i++) {
- wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]);
+ wholeString += faker.random.arrayElement(charsArray);
}
return wholeString;
};
/**
+ * @Deprecated
* hexaDecimal
*
* @method faker.random.hexaDecimal
* @param {number} count defaults to 1
*/
this.hexaDecimal = function hexaDecimal(count) {
- if (typeof count === "undefined") {
- count = 1;
- }
-
- var wholeString = "";
- for(var i = 0; i < count; i++) {
- wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", "E", "F"]);
- }
-
- return "0x"+wholeString;
+ console.log("Deprecation Warning: faker.random.hexaDecimal is now located in faker.datatype.hexaDecimal");
+ return faker.datatype.hexaDecimal(count);
};
return this;