diff options
| author | Marak <[email protected]> | 2021-02-11 10:38:01 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-02-11 10:38:01 -0500 |
| commit | 08d43e82a2fa7d142695fa9102eb6ed1152e5791 (patch) | |
| tree | c1da445887d10ddcf6c6eb14b0c3973d8e9440b8 /lib | |
| parent | 65d2f9e5ad366a92de619d855ee278628b67de33 (diff) | |
| parent | 12f624a5473b5f4b37078ecb1bb931f2a801417f (diff) | |
| download | faker-08d43e82a2fa7d142695fa9102eb6ed1152e5791.tar.xz faker-08d43e82a2fa7d142695fa9102eb6ed1152e5791.zip | |
Merge pull request #1063 from DanielLipowicz/fix/incorrectVinNumber
Fix issue 1062; Add new alpha and alphanumeric functionality
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/random.js | 74 | ||||
| -rw-r--r-- | lib/vehicle.js | 7 |
2 files changed, 59 insertions, 22 deletions
diff --git a/lib/random.js b/lib/random.js index f959440d..f815c94f 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,6 +1,21 @@ 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 */ @@ -53,7 +68,7 @@ function Random (faker, seed) { return randomNumber; - } + }; /** * returns a single random floating-point number based on a max number or range @@ -76,8 +91,8 @@ function Random (faker, seed) { opts.precision = 0.01; } return faker.random.number(opts); - } - + }; + /** * takes an array and returns a random element of the array * @@ -88,7 +103,7 @@ function Random (faker, seed) { array = array || ["a", "b", "c"]; var r = faker.random.number({ max: array.length - 1 }); return array[r]; - } + }; /** * takes an array and returns a subset with random elements of the array @@ -116,7 +131,7 @@ function Random (faker, seed) { } return arrayCopy; - } + }; /** * takes an object and returns the randomly key or value @@ -131,7 +146,7 @@ function Random (faker, seed) { var key = faker.random.arrayElement(array); return field === "key" ? key : object[key]; - } + }; /** * uuid @@ -146,7 +161,7 @@ function Random (faker, seed) { return value.toString(16); }; return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); - } + }; /** * boolean @@ -155,7 +170,7 @@ function Random (faker, seed) { */ this.boolean = function () { return !!faker.random.number(1) - } + }; // TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc /** @@ -203,7 +218,7 @@ function Random (faker, seed) { var randomWordMethod = faker.random.arrayElement(wordMethods); var result = faker.fake('{{' + randomWordMethod + '}}'); return faker.random.arrayElement(result.split(' ')); - } + }; /** * randomWords @@ -220,7 +235,7 @@ function Random (faker, seed) { words.push(faker.random.word()); } return words.join(' '); - } + }; /** * locale @@ -229,7 +244,7 @@ function Random (faker, seed) { */ this.image = function randomImage () { return faker.image.image(); - } + }; /** * locale @@ -244,47 +259,68 @@ 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; diff --git a/lib/vehicle.js b/lib/vehicle.js index cea42d87..3a8fa7a5 100644 --- a/lib/vehicle.js +++ b/lib/vehicle.js @@ -83,10 +83,11 @@ var Vehicle = function (faker) { * @method faker.vehicle.vin */ self.vin = function () { + var bannedChars=['o','i','q']; return ( - faker.random.alphaNumeric(10) + - faker.random.alpha({ count: 1, upcase: true }) + - faker.random.alphaNumeric(1) + + faker.random.alphaNumeric(10, {bannedChars:bannedChars}) + + faker.random.alpha({ count: 1, upcase: true ,bannedChars:bannedChars}) + + faker.random.alphaNumeric(1, {bannedChars:bannedChars}) + faker.random.number({ min: 10000, max: 100000}) // return five digit # ).toUpperCase(); }; |
