aboutsummaryrefslogtreecommitdiff
path: root/lib/random.js
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-01-11 20:06:46 +0100
committerGitHub <[email protected]>2022-01-11 14:06:46 -0500
commitd4c295f698ed256cb6c53ea77249e64b544ca8c0 (patch)
treedb0b9e33d8a5d4ddbb0b8e9a0139955b444f2cbb /lib/random.js
parentee666cd2f6cded87b4f4e366e2c661fcd5253a72 (diff)
downloadfaker-d4c295f698ed256cb6c53ea77249e64b544ca8c0.tar.xz
faker-d4c295f698ed256cb6c53ea77249e64b544ca8c0.zip
chore: format lib without locales (#66)
Diffstat (limited to 'lib/random.js')
-rw-r--r--lib/random.js158
1 files changed, 115 insertions, 43 deletions
diff --git a/lib/random.js b/lib/random.js
index 4443c555..0e43a338 100644
--- a/lib/random.js
+++ b/lib/random.js
@@ -5,8 +5,8 @@
* @return {*} new array without banned characters
*/
var arrayRemove = function (arr, values) {
- values.forEach(function(value){
- arr = arr.filter(function(ele){
+ values.forEach(function (value) {
+ arr = arr.filter(function (ele) {
return ele !== value;
});
});
@@ -17,12 +17,11 @@ var arrayRemove = function (arr, values) {
*
* @namespace faker.random
*/
-function Random (faker, seed) {
+function Random(faker, seed) {
// Use a user provided seed if it is an array or number
if (Array.isArray(seed) && seed.length) {
faker.mersenne.seed_array(seed);
- }
- else if(!isNaN(seed)) {
+ } else if (!isNaN(seed)) {
faker.mersenne.seed(seed);
}
@@ -34,7 +33,9 @@ function Random (faker, seed) {
* @param {mixed} options {min, max, precision}
*/
this.number = function (options) {
- console.log("Deprecation Warning: faker.random.number is now located in faker.datatype.number");
+ console.log(
+ 'Deprecation Warning: faker.random.number is now located in faker.datatype.number'
+ );
return faker.datatype.number(options);
};
@@ -46,7 +47,9 @@ function Random (faker, seed) {
* @param {mixed} options
*/
this.float = function (options) {
- console.log("Deprecation Warning: faker.random.float is now located in faker.datatype.float");
+ console.log(
+ 'Deprecation Warning: faker.random.float is now located in faker.datatype.float'
+ );
return faker.datatype.float(options);
};
@@ -57,7 +60,7 @@ function Random (faker, seed) {
* @param {array} array
*/
this.arrayElement = function (array) {
- array = array || ["a", "b", "c"];
+ array = array || ['a', 'b', 'c'];
var r = faker.datatype.number({ max: array.length - 1 });
return array[r];
};
@@ -70,7 +73,7 @@ function Random (faker, seed) {
* @param {number} count number of elements to pick
*/
this.arrayElements = function (array, count) {
- array = array || ["a", "b", "c"];
+ array = array || ['a', 'b', 'c'];
if (typeof count !== 'number') {
count = faker.datatype.number({ min: 1, max: array.length });
@@ -104,11 +107,11 @@ function Random (faker, seed) {
* @param {mixed} field
*/
this.objectElement = function (object, field) {
- object = object || { "foo": "bar", "too": "car" };
+ object = object || { foo: 'bar', too: 'car' };
var array = Object.keys(object);
var key = faker.random.arrayElement(array);
- return field === "key" ? key : object[key];
+ return field === 'key' ? key : object[key];
};
/**
@@ -118,7 +121,9 @@ function Random (faker, seed) {
* @method faker.random.uuid
*/
this.uuid = function () {
- console.log("Deprecation Warning: faker.random.uuid is now located in faker.datatype.uuid");
+ console.log(
+ 'Deprecation Warning: faker.random.uuid is now located in faker.datatype.uuid'
+ );
return faker.datatype.uuid();
};
@@ -128,7 +133,9 @@ function Random (faker, seed) {
* @method faker.random.boolean
*/
this.boolean = function () {
- console.log("Deprecation Warning: faker.random.boolean is now located in faker.datatype.boolean");
+ console.log(
+ 'Deprecation Warning: faker.random.boolean is now located in faker.datatype.boolean'
+ );
return faker.datatype.boolean();
};
@@ -139,8 +146,7 @@ function Random (faker, seed) {
* @method faker.random.word
* @param {string} type
*/
- this.word = function randomWord (type) {
-
+ this.word = function randomWord(type) {
var wordMethods = [
'commerce.department',
'commerce.productName',
@@ -172,7 +178,8 @@ function Random (faker, seed) {
'name.jobDescriptor',
'name.jobArea',
- 'name.jobType'];
+ 'name.jobType',
+ ];
// randomly pick from the many faker methods that can generate words
var randomWordMethod = faker.random.arrayElement(wordMethods);
@@ -186,12 +193,12 @@ function Random (faker, seed) {
* @method faker.random.words
* @param {number} count defaults to a random value between 1 and 3
*/
- this.words = function randomWords (count) {
+ this.words = function randomWords(count) {
var words = [];
- if (typeof count === "undefined") {
- count = faker.datatype.number({min:1, max: 3});
+ if (typeof count === 'undefined') {
+ count = faker.datatype.number({ min: 1, max: 3 });
}
- for (var i = 0; i<count; i++) {
+ for (var i = 0; i < count; i++) {
words.push(faker.random.word());
}
return words.join(' ');
@@ -202,7 +209,7 @@ function Random (faker, seed) {
*
* @method faker.random.image
*/
- this.image = function randomImage () {
+ this.image = function randomImage() {
return faker.image.image();
};
@@ -211,7 +218,7 @@ function Random (faker, seed) {
*
* @method faker.random.locale
*/
- this.locale = function randomLocale () {
+ this.locale = function randomLocale() {
return faker.random.arrayElement(Object.keys(faker.locales));
};
@@ -222,36 +229,63 @@ function Random (faker, seed) {
* @param {mixed} options // defaults to { count: 1, upcase: false, bannedChars: [] }
*/
this.alpha = function alpha(options) {
- if (typeof options === "undefined") {
+ if (typeof options === 'undefined') {
options = {
- count: 1
+ count: 1,
};
- } else if (typeof options === "number") {
+ } else if (typeof options === 'number') {
options = {
count: options,
};
- } else if (typeof options.count === "undefined") {
+ } else if (typeof options.count === 'undefined') {
options.count = 1;
}
- if (typeof options.upcase === "undefined") {
+ if (typeof options.upcase === 'undefined') {
options.upcase = false;
}
- if (typeof options.bannedChars ==="undefined"){
+ 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);
+ 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++) {
+ for (var i = 0; i < options.count; i++) {
wholeString += faker.random.arrayElement(charsArray);
}
return options.upcase ? wholeString.toUpperCase() : wholeString;
- }
+ };
/**
* alphaNumeric
@@ -262,24 +296,61 @@ function Random (faker, seed) {
* options.bannedChars - array of characters which should be banned in new string
*/
this.alphaNumeric = function alphaNumeric(count, options) {
- if (typeof count === "undefined") {
+ if (typeof count === 'undefined') {
count = 1;
}
- if (typeof options ==="undefined"){
+ if (typeof options === 'undefined') {
options = {};
}
- if (typeof options.bannedChars ==="undefined"){
+ 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) {
+ 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++) {
+ for (var i = 0; i < count; i++) {
wholeString += faker.random.arrayElement(charsArray);
}
@@ -294,12 +365,13 @@ function Random (faker, seed) {
* @param {number} count defaults to 1
*/
this.hexaDecimal = function hexaDecimal(count) {
- console.log("Deprecation Warning: faker.random.hexaDecimal is now located in faker.datatype.hexaDecimal");
+ console.log(
+ 'Deprecation Warning: faker.random.hexaDecimal is now located in faker.datatype.hexaDecimal'
+ );
return faker.datatype.hexaDecimal(count);
};
return this;
-
}
module['exports'] = Random;