diff options
| author | Marak <[email protected]> | 2017-09-08 12:52:43 -0400 |
|---|---|---|
| committer | Marak <[email protected]> | 2017-09-08 12:52:43 -0400 |
| commit | b652617fa5f78279cae3a5f64e7930a19369dd38 (patch) | |
| tree | b029cbd80f86be3db39c3b72e7f7514a6624b179 /lib | |
| parent | f4236a85b904394c729c3efb383c09fa5d137c54 (diff) | |
| parent | 52d8cc1471080fe67c8abd8115240b6aa933f85f (diff) | |
| download | faker-b652617fa5f78279cae3a5f64e7930a19369dd38.tar.xz faker-b652617fa5f78279cae3a5f64e7930a19369dd38.zip | |
Merge branch 'creditCard' of https://github.com/zpiman/faker.js
Diffstat (limited to 'lib')
27 files changed, 230 insertions, 56 deletions
diff --git a/lib/finance.js b/lib/finance.js index 41cae3b1..88a992f3 100644 --- a/lib/finance.js +++ b/lib/finance.js @@ -148,6 +148,52 @@ var Finance = function (faker) { address += faker.random.alphaNumeric().toUpperCase(); 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; }; /** diff --git a/lib/helpers.js b/lib/helpers.js index 635c4ecc..8f3b5c71 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -77,6 +77,116 @@ 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); + }; + + /** string repeat helper, alternative to String.prototype.repeat.... See PR #382 + * + * @method faker.helpers.repeatString + * @param {string} string + * @param {number} num + */ + self.repeatString = function(string,num) { + if(typeof num ==="undefined") { + num = 0; + } + var text = ""; + for(var i = 0; i < num; i++){ + text += string.toString(); + } + return text; + }; + + /** + * 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) + faker.helpers.repeatString(token[1], 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)+ faker.helpers.repeatString(token[1], 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 diff --git a/lib/index.js b/lib/index.js index a8ec14b5..c071c3fd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -106,7 +106,7 @@ function Faker (opts) { "lorem": ["words"], "hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb", "phrase"], "phone_number": ["formats"], - "finance": ["account_type", "transaction_type", "currency", "iban"], + "finance": ["account_type", "transaction_type", "currency", "iban", "credit_card"], "internet": ["avatar_uri", "domain_suffix", "free_email", "example_email", "password"], "commerce": ["color", "department", "product_name", "price", "categories"], "database": ["collation", "column", "engine", "type"], diff --git a/lib/locales/en/credit_card/american_express.js b/lib/locales/en/credit_card/american_express.js deleted file mode 100644 index 2a3d782b..00000000 --- a/lib/locales/en/credit_card/american_express.js +++ /dev/null @@ -1,4 +0,0 @@ -module["exports"] = [ - "/34##-######-####L/", - "/37##-######-####L/" -]; diff --git a/lib/locales/en/credit_card/diners_club.js b/lib/locales/en/credit_card/diners_club.js deleted file mode 100644 index ddc7eb35..00000000 --- a/lib/locales/en/credit_card/diners_club.js +++ /dev/null @@ -1,4 +0,0 @@ -module["exports"] = [ - "/30[0-5]#-######-###L/", - "/368#-######-###L/" -]; diff --git a/lib/locales/en/credit_card/discover.js b/lib/locales/en/credit_card/discover.js deleted file mode 100644 index d51aa159..00000000 --- a/lib/locales/en/credit_card/discover.js +++ /dev/null @@ -1,8 +0,0 @@ -module["exports"] = [ - "/6011-####-####-###L/", - "/65##-####-####-###L/", - "/64[4-9]#-####-####-###L/", - "/6011-62##-####-####-###L/", - "/65##-62##-####-####-###L/", - "/64[4-9]#-62##-####-####-###L/" -]; diff --git a/lib/locales/en/credit_card/jcb.js b/lib/locales/en/credit_card/jcb.js deleted file mode 100644 index 03d83398..00000000 --- a/lib/locales/en/credit_card/jcb.js +++ /dev/null @@ -1,5 +0,0 @@ -module["exports"] = [ - "/3528-####-####-###L/", - "/3529-####-####-###L/", - "/35[3-8]#-####-####-###L/" -]; diff --git a/lib/locales/en/credit_card/laser.js b/lib/locales/en/credit_card/laser.js deleted file mode 100644 index 922da719..00000000 --- a/lib/locales/en/credit_card/laser.js +++ /dev/null @@ -1,10 +0,0 @@ -module["exports"] = [ - "/6304###########L/", - "/6706###########L/", - "/6771###########L/", - "/6709###########L/", - "/6304#########{5,6}L/", - "/6706#########{5,6}L/", - "/6771#########{5,6}L/", - "/6709#########{5,6}L/" -]; diff --git a/lib/locales/en/credit_card/maestro.js b/lib/locales/en/credit_card/maestro.js deleted file mode 100644 index da575f88..00000000 --- a/lib/locales/en/credit_card/maestro.js +++ /dev/null @@ -1,5 +0,0 @@ -module["exports"] = [ - "/50#{9,16}L/", - "/5[6-8]#{9,16}L/", - "/56##{9,16}L/" -]; diff --git a/lib/locales/en/credit_card/mastercard.js b/lib/locales/en/credit_card/mastercard.js deleted file mode 100644 index f6525eb8..00000000 --- a/lib/locales/en/credit_card/mastercard.js +++ /dev/null @@ -1,4 +0,0 @@ -module["exports"] = [ - "/5[1-5]##-####-####-###L/", - "/6771-89##-####-###L/" -]; diff --git a/lib/locales/en/credit_card/solo.js b/lib/locales/en/credit_card/solo.js deleted file mode 100644 index a577fb43..00000000 --- a/lib/locales/en/credit_card/solo.js +++ /dev/null @@ -1,5 +0,0 @@ -module["exports"] = [ - "/6767-####-####-###L/", - "/6767-####-####-####-#L/", - "/6767-####-####-####-##L/" -]; diff --git a/lib/locales/en/credit_card/switch.js b/lib/locales/en/credit_card/switch.js deleted file mode 100644 index b12a4d07..00000000 --- a/lib/locales/en/credit_card/switch.js +++ /dev/null @@ -1,5 +0,0 @@ -module["exports"] = [ - "/6759-####-####-###L/", - "/6759-####-####-####-#L/", - "/6759-####-####-####-##L/" -]; diff --git a/lib/locales/en/credit_card/visa.js b/lib/locales/en/credit_card/visa.js deleted file mode 100644 index 972eba8f..00000000 --- a/lib/locales/en/credit_card/visa.js +++ /dev/null @@ -1,4 +0,0 @@ -module["exports"] = [ - "/4###########L/", - "/4###-####-####-###L/" -]; diff --git a/lib/locales/en/finance/credit_card/american_express.js b/lib/locales/en/finance/credit_card/american_express.js new file mode 100644 index 00000000..c63363ab --- /dev/null +++ b/lib/locales/en/finance/credit_card/american_express.js @@ -0,0 +1,4 @@ +module["exports"] = [ + "34##-######-####L", + "37##-######-####L" +]; diff --git a/lib/locales/en/finance/credit_card/diners_club.js b/lib/locales/en/finance/credit_card/diners_club.js new file mode 100644 index 00000000..4fb5d7ca --- /dev/null +++ b/lib/locales/en/finance/credit_card/diners_club.js @@ -0,0 +1,5 @@ +module["exports"] = [ + "30[0-5]#-######-###L", + "36##-######-###L", + "54##-####-####-###L" +]; diff --git a/lib/locales/en/finance/credit_card/discover.js b/lib/locales/en/finance/credit_card/discover.js new file mode 100644 index 00000000..cd84e840 --- /dev/null +++ b/lib/locales/en/finance/credit_card/discover.js @@ -0,0 +1,8 @@ +module["exports"] = [ + "6011-####-####-###L", + "65##-####-####-###L", + "64[4-9]#-####-####-###L", + "6011-62##-####-####-###L", + "65##-62##-####-####-###L", + "64[4-9]#-62##-####-####-###L" +]; diff --git a/lib/locales/en/credit_card/index.js b/lib/locales/en/finance/credit_card/index.js index b083e967..3afd843e 100644 --- a/lib/locales/en/credit_card/index.js +++ b/lib/locales/en/finance/credit_card/index.js @@ -10,3 +10,4 @@ credit_card.switch = require("./switch"); credit_card.solo = require("./solo"); credit_card.maestro = require("./maestro"); credit_card.laser = require("./laser"); +credit_card.instapayment = require("./instapayment.js") diff --git a/lib/locales/en/finance/credit_card/instapayment.js b/lib/locales/en/finance/credit_card/instapayment.js new file mode 100644 index 00000000..545c5380 --- /dev/null +++ b/lib/locales/en/finance/credit_card/instapayment.js @@ -0,0 +1,3 @@ +module["exports"] = [ + "63[7-9]#-####-####-###L" +]; diff --git a/lib/locales/en/finance/credit_card/jcb.js b/lib/locales/en/finance/credit_card/jcb.js new file mode 100644 index 00000000..da396ebd --- /dev/null +++ b/lib/locales/en/finance/credit_card/jcb.js @@ -0,0 +1,5 @@ +module["exports"] = [ + "3528-####-####-###L", + "3529-####-####-###L", + "35[3-8]#-####-####-###L" +]; diff --git a/lib/locales/en/finance/credit_card/laser.js b/lib/locales/en/finance/credit_card/laser.js new file mode 100644 index 00000000..822c7b35 --- /dev/null +++ b/lib/locales/en/finance/credit_card/laser.js @@ -0,0 +1,10 @@ +module["exports"] = [ + "6304###########L", + "6706###########L", + "6771###########L", + "6709###########L", + "6304#########{5,6}L", + "6706#########{5,6}L", + "6771#########{5,6}L", + "6709#########{5,6}L" +]; diff --git a/lib/locales/en/finance/credit_card/maestro.js b/lib/locales/en/finance/credit_card/maestro.js new file mode 100644 index 00000000..f652dd76 --- /dev/null +++ b/lib/locales/en/finance/credit_card/maestro.js @@ -0,0 +1,18 @@ +module["exports"] = [ + "5018-#{4}-#{4}-#{3}L", + "5020-#{4}-#{4}-#{3}L", + "5038-#{4}-#{4}-#{3}L", + "5893-#{4}-#{4}-#{3}L", + "6304-#{4}-#{4}-#{3}L", + "6759-#{4}-#{4}-#{3}L", + "676[1-3]-####-####-###L", + "5018#{11,15}L", + "5020#{11,15}L", + "5038#{11,15}L", + "5893#{11,15}L", + "6304#{11,15}L", + "6759#{11,15}L", + "676[1-3]#{11,15}L", +]; + +// 5018 xxxx xxxx xxxx xxL diff --git a/lib/locales/en/finance/credit_card/mastercard.js b/lib/locales/en/finance/credit_card/mastercard.js new file mode 100644 index 00000000..81502c01 --- /dev/null +++ b/lib/locales/en/finance/credit_card/mastercard.js @@ -0,0 +1,4 @@ +module["exports"] = [ + "5[1-5]##-####-####-###L", + "6771-89##-####-###L" +]; diff --git a/lib/locales/en/finance/credit_card/solo.js b/lib/locales/en/finance/credit_card/solo.js new file mode 100644 index 00000000..47d0d119 --- /dev/null +++ b/lib/locales/en/finance/credit_card/solo.js @@ -0,0 +1,5 @@ +module["exports"] = [ + "6767-####-####-###L", + "6767-####-####-####-#L", + "6767-####-####-####-##L" +]; diff --git a/lib/locales/en/finance/credit_card/switch.js b/lib/locales/en/finance/credit_card/switch.js new file mode 100644 index 00000000..1bae8278 --- /dev/null +++ b/lib/locales/en/finance/credit_card/switch.js @@ -0,0 +1,5 @@ +module["exports"] = [ + "6759-####-####-###L", + "6759-####-####-####-#L", + "6759-####-####-####-##L" +]; diff --git a/lib/locales/en/finance/credit_card/visa.js b/lib/locales/en/finance/credit_card/visa.js new file mode 100644 index 00000000..760f921d --- /dev/null +++ b/lib/locales/en/finance/credit_card/visa.js @@ -0,0 +1,4 @@ +module["exports"] = [ + "4###########L", + "4###-####-####-###L" +]; diff --git a/lib/locales/en/finance/index.js b/lib/locales/en/finance/index.js index 4020b1d0..9a5b00d9 100644 --- a/lib/locales/en/finance/index.js +++ b/lib/locales/en/finance/index.js @@ -3,3 +3,4 @@ module['exports'] = finance; finance.account_type = require("./account_type"); finance.transaction_type = require("./transaction_type"); finance.currency = require("./currency"); +finance.credit_card = require("./credit_card"); diff --git a/lib/locales/en/index.js b/lib/locales/en/index.js index 5cc575f9..04ce4e49 100644 --- a/lib/locales/en/index.js +++ b/lib/locales/en/index.js @@ -3,7 +3,6 @@ module['exports'] = en; en.title = "English"; en.separator = " & "; en.address = require("./address"); -en.credit_card = require("./credit_card"); en.company = require("./company"); en.internet = require("./internet"); en.database = require("./database"); |
