diff options
| author | Jakub Mandula <[email protected]> | 2016-06-13 20:48:42 +0200 |
|---|---|---|
| committer | Jakub Mandula <[email protected]> | 2016-06-13 21:28:29 +0200 |
| commit | e7084554f8f8f5104805eec4d0f17bf7b2443bb6 (patch) | |
| tree | 540e22be99fd392bf14d01fdb01f76fc0311457e | |
| parent | 937061e89de4c9ad13ff3aff1ba6b2d4e515fc30 (diff) | |
| download | faker-e7084554f8f8f5104805eec4d0f17bf7b2443bb6.tar.xz faker-e7084554f8f8f5104805eec4d0f17bf7b2443bb6.zip | |
add credit card function + helpers
| -rw-r--r-- | lib/finance.js | 48 | ||||
| -rw-r--r-- | lib/helpers.js | 93 |
2 files changed, 140 insertions, 1 deletions
diff --git a/lib/finance.js b/lib/finance.js index 1db94dcf..fbc5d558 100644 --- a/lib/finance.js +++ b/lib/finance.js @@ -150,6 +150,52 @@ var Finance = function (faker) { return address; } -} + + /** + * Credit card number + * @method faker.finance.creditCardNumber + * @param {string} provider | scheme + */ + self.creditCardNumber = function(provider){ + provider = provider || ""; + var format, formats; + var localeFormat = faker.definitions.finance.credit_card; + if (provider in localeFormat) { + formats = localeFormat[provider]; // there chould be multiple formats + if (typeof formats === "string") { + format = formats; + } else { + format = faker.random.arrayElement(formats); + } + } else if (provider.match(/#/)) { // The user chose an optional scheme + format = provider; + } else { // Choose a random provider + if (typeof localeFormat === 'string') { + format = localeFormat; + } else if( typeof localeFormat === "object") { + // Credit cards are in a object structure + formats = faker.random.objectElement(localeFormat, "value"); // There chould be multiple formats + if (typeof formats === "string") { + format = formats; + } else { + format = faker.random.arrayElement(formats); + } + } + } + format = format.replace(/\//g,"") + return Helpers.replaceCreditCardSymbols(format); + }; + /** + * Credit card CVV + * @method faker.finance.creditCardNumber + */ + self.creditCardCVV = function() { + var cvv = ""; + for (var i = 0; i < 3; i++) { + cvv += faker.random.number({max:9}).toString(); + } + return cvv; + }; +}; module['exports'] = Finance; diff --git a/lib/helpers.js b/lib/helpers.js index ac3398d7..832732f5 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -77,6 +77,99 @@ var Helpers = function (faker) { }; /** + * replace symbols in a credit card schems including Luhn checksum + * + * @method faker.helpers.replaceCreditCardSymbols + * @param {string} string + * @param {string} symbol + */ + + self.replaceCreditCardSymbols = function(string, symbol) { + symbol = symbol || "#"; + + // Function calculating the Luhn checksum of a number string + var getCheckBit = function(number) { + number.reverse(); + number = number.map(function(num, index){ + if(index%2 === 0) { + num *= 2; + if(num>9) { + num -= 9; + } + } + return num; + }); + var sum = number.reduce(function(prev,curr){return prev + curr;}); + return sum % 10; + }; + + string = string || ""; + string = faker.helpers.regexpStyleStringParse(string); // replace [4-9] with a random number in range etc... + string = faker.helpers.replaceSymbolWithNumber(string, symbol); // replace ### with random numbers + + var numberList = string.replace(/\D/g,"").split("").map(function(num){return parseInt(num);}); + var checkNum = getCheckBit(numberList); + return string.replace("L",checkNum); + }; + + /** + * parse string paterns in a similar way to RegExp + * + * e.g. "#{3}test[1-5]" -> "###test4" + * + * @method faker.helpers.regexpStyleStringParse + * @param {string} string + */ + self.regexpStyleStringParse = function(string){ + string = string || ""; + // Deal with range repeat `{min,max}` + var RANGE_REP_REG = /(.)\{(\d+)\,(\d+)\}/; + var REP_REG = /(.)\{(\d+)\}/; + var RANGE_REG = /\[(\d+)\-(\d+)\]/; + var min, max, tmp, repetitions; + var token = string.match(RANGE_REP_REG); + while(token !== null){ + min = parseInt(token[2]); + max = parseInt(token[3]); + // switch min and max + if(min>max) { + tmp = max; + max = min; + min = tmp; + } + repetitions = faker.random.number({min:min,max:max}); + string = string.slice(0,token.index)+ token[1].repeat(repetitions) + string.slice(token.index+token[0].length); + token = string.match(RANGE_REP_REG); + } + // Deal with repeat `{num}` + token = string.match(REP_REG); + while(token !== null){ + repetitions = parseInt(token[2]); + string = string.slice(0,token.index)+ token[1].repeat(repetitions) + string.slice(token.index+token[0].length); + token = string.match(REP_REG); + } + // Deal with range `[min-max]` (only works with numbers for now) + //TODO: implement for letters e.g. [0-9a-zA-Z] etc. + + token = string.match(RANGE_REG); + while(token !== null){ + min = parseInt(token[1]); // This time we are not capturing the char befor `[]` + max = parseInt(token[2]); + // switch min and max + if(min>max) { + tmp = max; + max = min; + min = tmp; + } + string = string.slice(0,token.index) + + faker.random.number({min:min, max:max}).toString() + + string.slice(token.index+token[0].length); + token = string.match(RANGE_REG); + } + return string; + }; + + /** * takes an array and returns it randomized * * @method faker.helpers.shuffle |
