aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Lipowicz <[email protected]>2020-12-01 15:51:59 +0100
committerDaniel Lipowicz <[email protected]>2020-12-01 15:51:59 +0100
commite7ccc03441383ba48f2ce42b26ae1da32f8bace0 (patch)
tree6853d5d9f872250730b8c3923c6a683dbb82fe68 /lib
parent91dc8a3372426bc691be56153b33e81a16459f49 (diff)
downloadfaker-e7ccc03441383ba48f2ce42b26ae1da32f8bace0.tar.xz
faker-e7ccc03441383ba48f2ce42b26ae1da32f8bace0.zip
Implement issue 1062; Add random.aplha functionality to blacklist some characters
Diffstat (limited to 'lib')
-rw-r--r--lib/random.js74
-rw-r--r--lib/vehicle.js7
2 files changed, 59 insertions, 22 deletions
diff --git a/lib/random.js b/lib/random.js
index 5afd27e2..63912298 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
*/
@@ -54,7 +69,7 @@ function Random (faker, seed) {
return randomNumber;
- }
+ };
/**
* returns a single random floating-point number based on a max number or range
@@ -77,8 +92,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
*
@@ -89,7 +104,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
@@ -117,7 +132,7 @@ function Random (faker, seed) {
}
return arrayCopy;
- }
+ };
/**
* takes an object and returns the randomly key or value
@@ -132,7 +147,7 @@ function Random (faker, seed) {
var key = faker.random.arrayElement(array);
return field === "key" ? key : object[key];
- }
+ };
/**
* uuid
@@ -147,7 +162,7 @@ function Random (faker, seed) {
return value.toString(16);
};
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
- }
+ };
/**
* boolean
@@ -156,7 +171,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
/**
@@ -204,7 +219,7 @@ function Random (faker, seed) {
var randomWordMethod = faker.random.arrayElement(wordMethods);
var result = faker.fake('{{' + randomWordMethod + '}}');
return faker.random.arrayElement(result.split(' '));
- }
+ };
/**
* randomWords
@@ -221,7 +236,7 @@ function Random (faker, seed) {
words.push(faker.random.word());
}
return words.join(' ');
- }
+ };
/**
* locale
@@ -230,7 +245,7 @@ function Random (faker, seed) {
*/
this.image = function randomImage () {
return faker.image.image();
- }
+ };
/**
* locale
@@ -245,47 +260,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 8eaed13e..7e36d7cc 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();
};