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 | |
| parent | f4236a85b904394c729c3efb383c09fa5d137c54 (diff) | |
| parent | 52d8cc1471080fe67c8abd8115240b6aa933f85f (diff) | |
| download | faker-b652617fa5f78279cae3a5f64e7930a19369dd38.tar.xz faker-b652617fa5f78279cae3a5f64e7930a19369dd38.zip | |
Merge branch 'creditCard' of https://github.com/zpiman/faker.js
30 files changed, 374 insertions, 59 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"); diff --git a/test/finance.unit.js b/test/finance.unit.js index 61ae7b6c..a879d584 100644 --- a/test/finance.unit.js +++ b/test/finance.unit.js @@ -222,7 +222,7 @@ describe('finance.js', function () { describe("bitcoinAddress()", function(){ it("returns a random bitcoin address", function(){ var bitcoinAddress = faker.finance.bitcoinAddress(); - + assert.ok(bitcoinAddress.match(/^[A-Z0-9.]{27,34}$/)); }); }); @@ -230,11 +230,83 @@ describe('finance.js', function () { describe("ethereumAddress()", function(){ it("returns a random ethereum address", function(){ var ethereumAddress = faker.finance.ethereumAddress(); - assert.ok(ethereumAddress.match(/^(0x)[0-9a-f]{40}$/i)); }); }); + describe("creditCardNumber()", function(){ + var luhnFormula = require("./support/luhnCheck.js"); + + it("returns a random credit card number", function(){ + var number = faker.finance.creditCardNumber(); + number = number.replace(/\D/g,""); // remove formating + console.log("version:", process.version, number, number.length); + assert.ok(number.length >= 13 && number.length <= 20); + assert.ok(number.match(/^[0-9]{13,20}$/)); + assert.ok(luhnFormula(number)); + }); + + it("returns a valid credit card number", function(){ + assert.ok(luhnFormula(faker.finance.creditCardNumber(""))); + assert.ok(luhnFormula(faker.finance.creditCardNumber())); + assert.ok(luhnFormula(faker.finance.creditCardNumber())); + assert.ok(luhnFormula(faker.finance.creditCardNumber("visa"))); + assert.ok(luhnFormula(faker.finance.creditCardNumber("mastercard"))); + assert.ok(luhnFormula(faker.finance.creditCardNumber("discover"))); + assert.ok(luhnFormula(faker.finance.creditCardNumber())); + assert.ok(luhnFormula(faker.finance.creditCardNumber())); + }); + it("returns a correct credit card number when issuer provided", function(){ + //TODO: implement checks for each format with regexp + var visa = faker.finance.creditCardNumber("visa"); + assert.ok(visa.match(/^4(([0-9]){12}|([0-9]){3}(\-([0-9]){4}){3})$/)); + assert.ok(luhnFormula(visa)); + + + var mastercard = faker.finance.creditCardNumber("mastercard"); + assert.ok(mastercard.match(/^(5[1-5]\d{2}|6771)(\-\d{4}){3}$/)); + assert.ok(luhnFormula(mastercard)); + + var discover = faker.finance.creditCardNumber("discover"); + + assert.ok(luhnFormula(discover)); + + var american_express = faker.finance.creditCardNumber("american_express"); + assert.ok(luhnFormula(american_express)); + var diners_club = faker.finance.creditCardNumber("diners_club"); + assert.ok(luhnFormula(diners_club)); + var jcb = faker.finance.creditCardNumber("jcb"); + assert.ok(luhnFormula(jcb)); + var switchC = faker.finance.creditCardNumber("mastercard"); + assert.ok(luhnFormula(switchC)); + var solo = faker.finance.creditCardNumber("solo"); + assert.ok(luhnFormula(solo)); + var maestro = faker.finance.creditCardNumber("maestro"); + assert.ok(luhnFormula(maestro)); + var laser = faker.finance.creditCardNumber("laser"); + assert.ok(luhnFormula(laser)); + var instapayment = faker.finance.creditCardNumber("instapayment"); + assert.ok(luhnFormula(instapayment)); + }); + it("returns custom formated strings",function(){ + var number = faker.finance.creditCardNumber("###-###-##L"); + assert.ok(number.match(/^\d{3}\-\d{3}\-\d{3}$/)); + assert.ok(luhnFormula(number)); + number =faker.finance.creditCardNumber("234[5-9]#{999}L"); + assert.ok(number.match(/^234[5-9]\d{1000}$/)); + assert.ok(luhnFormula(number)); + }); + }); + + describe("creditCardCVV()", function(){ + it("returns a random credit card CVV", function(){ + var cvv = faker.finance.creditCardCVV(); + assert.ok(cvv.length === 3); + assert.ok(cvv.match(/^[0-9]{3}$/)); + }); + }); + + describe("iban()", function () { var ibanLib = require('../lib/iban'); it("returns a random yet formally correct IBAN number", function () { @@ -254,4 +326,4 @@ describe('finance.js', function () { assert.ok(bic.match(expr)); }); }); -});
\ No newline at end of file +}); diff --git a/test/helpers.unit.js b/test/helpers.unit.js index 707575ff..b440f33e 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -67,6 +67,57 @@ describe("helpers.js", function () { }); }); + describe("replaceCreditCardSymbols()", function () { + var luhnCheck = require("./support/luhnCheck.js"); + it("returns a credit card number given a schema", function () { + var number = faker.helpers.replaceCreditCardSymbols("6453-####-####-####-###L"); + assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/)); + assert.ok(luhnCheck(number)); + }); + it("supports different symbols", function () { + var number = faker.helpers.replaceCreditCardSymbols("6453-****-****-****-***L","*"); + assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/)); + assert.ok(luhnCheck(number)); + }); + it("handles regexp style input", function () { + var number = faker.helpers.replaceCreditCardSymbols("6453-*{4}-*{4}-*{4}-*{3}L","*"); + assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/)); + assert.ok(luhnCheck(number)); + number = faker.helpers.replaceCreditCardSymbols("645[5-9]-#{4,6}-#{1,2}-#{4,6}-#{3}L"); + assert.ok(number.match(/^645[5-9]\-([0-9]){4,6}\-([0-9]){1,2}\-([0-9]){4,6}\-([0-9]){4}$/)); + assert.ok(luhnCheck(number)); + }); + }); + + describe("regexpStyleStringParse()", function () { + it("returns an empty string when called without param", function () { + assert.ok(faker.helpers.regexpStyleStringParse() === ""); + }); + it("deals with range repeat", function () { + var string = faker.helpers.regexpStyleStringParse("#{5,10}"); + assert.ok(string.length <= 10 && string.length >= 5); + assert.ok(string.match(/^\#{5,10}$/)); + }); + it("flips the range when min > max", function () { + var string = faker.helpers.regexpStyleStringParse("#{10,5}"); + assert.ok(string.length <= 10 && string.length >= 5); + assert.ok(string.match(/^\#{5,10}$/)); + }); + it("repeats string {n} number of times", function () { + assert.ok(faker.helpers.regexpStyleStringParse("%{10}") === faker.helpers.repeatString("%",10)); + assert.ok(faker.helpers.regexpStyleStringParse("%{30}") === faker.helpers.repeatString("%",30)); + assert.ok(faker.helpers.regexpStyleStringParse("%{5}") === faker.helpers.repeatString("%",5)); + }); + it("creates a numerical range", function () { + var string = faker.helpers.regexpStyleStringParse("Hello[0-9]"); + assert.ok(string.match(/^Hello[0-9]$/)); + }); + it("deals with multiple tokens in one string", function () { + var string = faker.helpers.regexpStyleStringParse("Test#{5}%{2,5}Testing**[1-5]**{10}END"); + assert.ok(string.match(/^Test\#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/)); + }); + }); + describe("createTransaction()", function() { it("should create a random transaction", function() { var transaction = faker.helpers.createTransaction(); diff --git a/test/support/luhnCheck.js b/test/support/luhnCheck.js new file mode 100644 index 00000000..1e195544 --- /dev/null +++ b/test/support/luhnCheck.js @@ -0,0 +1,18 @@ +module.exports = function (number) { + number = number.replace(/\D/g,""); + var split = number.split(""); + split = split.map(function(num){return parseInt(num);}); + var check = split.pop(); + split.reverse(); + split = split.map(function(num, index){ + if(index%2 === 0) { + num *= 2; + if(num>9) { + num -= 9; + } + } + return num; + }); + var sum = split.reduce(function(prev,curr){return prev + curr;}); + return (sum%10 === check); +}; |
