From 61c65af05563d10c0bdbecf33be5873a4a621a14 Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Mon, 13 Jun 2016 19:52:53 +0200 Subject: move credit_card into finance + add to definitions --- lib/index.js | 2 +- lib/locales/en/credit_card/american_express.js | 4 ---- lib/locales/en/credit_card/diners_club.js | 4 ---- lib/locales/en/credit_card/discover.js | 8 -------- lib/locales/en/credit_card/index.js | 12 ------------ lib/locales/en/credit_card/jcb.js | 5 ----- lib/locales/en/credit_card/laser.js | 10 ---------- lib/locales/en/credit_card/maestro.js | 5 ----- lib/locales/en/credit_card/mastercard.js | 4 ---- lib/locales/en/credit_card/solo.js | 5 ----- lib/locales/en/credit_card/switch.js | 5 ----- lib/locales/en/credit_card/visa.js | 4 ---- lib/locales/en/finance/credit_card/american_express.js | 4 ++++ lib/locales/en/finance/credit_card/diners_club.js | 4 ++++ lib/locales/en/finance/credit_card/discover.js | 8 ++++++++ lib/locales/en/finance/credit_card/index.js | 12 ++++++++++++ lib/locales/en/finance/credit_card/jcb.js | 5 +++++ lib/locales/en/finance/credit_card/laser.js | 10 ++++++++++ lib/locales/en/finance/credit_card/maestro.js | 5 +++++ lib/locales/en/finance/credit_card/mastercard.js | 4 ++++ lib/locales/en/finance/credit_card/solo.js | 5 +++++ lib/locales/en/finance/credit_card/switch.js | 5 +++++ lib/locales/en/finance/credit_card/visa.js | 4 ++++ lib/locales/en/finance/index.js | 1 + lib/locales/en/index.js | 1 - 25 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 lib/locales/en/credit_card/american_express.js delete mode 100644 lib/locales/en/credit_card/diners_club.js delete mode 100644 lib/locales/en/credit_card/discover.js delete mode 100644 lib/locales/en/credit_card/index.js delete mode 100644 lib/locales/en/credit_card/jcb.js delete mode 100644 lib/locales/en/credit_card/laser.js delete mode 100644 lib/locales/en/credit_card/maestro.js delete mode 100644 lib/locales/en/credit_card/mastercard.js delete mode 100644 lib/locales/en/credit_card/solo.js delete mode 100644 lib/locales/en/credit_card/switch.js delete mode 100644 lib/locales/en/credit_card/visa.js create mode 100644 lib/locales/en/finance/credit_card/american_express.js create mode 100644 lib/locales/en/finance/credit_card/diners_club.js create mode 100644 lib/locales/en/finance/credit_card/discover.js create mode 100644 lib/locales/en/finance/credit_card/index.js create mode 100644 lib/locales/en/finance/credit_card/jcb.js create mode 100644 lib/locales/en/finance/credit_card/laser.js create mode 100644 lib/locales/en/finance/credit_card/maestro.js create mode 100644 lib/locales/en/finance/credit_card/mastercard.js create mode 100644 lib/locales/en/finance/credit_card/solo.js create mode 100644 lib/locales/en/finance/credit_card/switch.js create mode 100644 lib/locales/en/finance/credit_card/visa.js (limited to 'lib') diff --git a/lib/index.js b/lib/index.js index ed0a664b..7818252b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -92,7 +92,7 @@ function Faker (opts) { "lorem": ["words"], "hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb"], "phone_number": ["formats"], - "finance": ["account_type", "transaction_type", "currency"], + "finance": ["account_type", "transaction_type", "currency", "credit_card"], "internet": ["avatar_uri", "domain_suffix", "free_email", "example_email", "password"], "commerce": ["color", "department", "product_name", "price", "categories"], "system": ["mimeTypes"], 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/index.js b/lib/locales/en/credit_card/index.js deleted file mode 100644 index b083e967..00000000 --- a/lib/locales/en/credit_card/index.js +++ /dev/null @@ -1,12 +0,0 @@ -var credit_card = {}; -module['exports'] = credit_card; -credit_card.visa = require("./visa"); -credit_card.mastercard = require("./mastercard"); -credit_card.discover = require("./discover"); -credit_card.american_express = require("./american_express"); -credit_card.diners_club = require("./diners_club"); -credit_card.jcb = require("./jcb"); -credit_card.switch = require("./switch"); -credit_card.solo = require("./solo"); -credit_card.maestro = require("./maestro"); -credit_card.laser = require("./laser"); 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..2a3d782b --- /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..ddc7eb35 --- /dev/null +++ b/lib/locales/en/finance/credit_card/diners_club.js @@ -0,0 +1,4 @@ +module["exports"] = [ + "/30[0-5]#-######-###L/", + "/368#-######-###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..d51aa159 --- /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/finance/credit_card/index.js b/lib/locales/en/finance/credit_card/index.js new file mode 100644 index 00000000..b083e967 --- /dev/null +++ b/lib/locales/en/finance/credit_card/index.js @@ -0,0 +1,12 @@ +var credit_card = {}; +module['exports'] = credit_card; +credit_card.visa = require("./visa"); +credit_card.mastercard = require("./mastercard"); +credit_card.discover = require("./discover"); +credit_card.american_express = require("./american_express"); +credit_card.diners_club = require("./diners_club"); +credit_card.jcb = require("./jcb"); +credit_card.switch = require("./switch"); +credit_card.solo = require("./solo"); +credit_card.maestro = require("./maestro"); +credit_card.laser = require("./laser"); 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..03d83398 --- /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..922da719 --- /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..da575f88 --- /dev/null +++ b/lib/locales/en/finance/credit_card/maestro.js @@ -0,0 +1,5 @@ +module["exports"] = [ + "/50#{9,16}L/", + "/5[6-8]#{9,16}L/", + "/56##{9,16}L/" +]; 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..f6525eb8 --- /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..a577fb43 --- /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..b12a4d07 --- /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..972eba8f --- /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 2513a826..1b24823a 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.lorem = require("./lorem"); -- cgit v1.2.3 From 83f6b59ef11f8f6ff321476c7b464c207b0fac01 Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Mon, 13 Jun 2016 20:46:58 +0200 Subject: add new credit card company --- lib/locales/en/finance/credit_card/index.js | 1 + lib/locales/en/finance/credit_card/instapayment.js | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 lib/locales/en/finance/credit_card/instapayment.js (limited to 'lib') diff --git a/lib/locales/en/finance/credit_card/index.js b/lib/locales/en/finance/credit_card/index.js index b083e967..3afd843e 100644 --- a/lib/locales/en/finance/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" +]; -- cgit v1.2.3 From 937061e89de4c9ad13ff3aff1ba6b2d4e515fc30 Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Mon, 13 Jun 2016 20:47:37 +0200 Subject: remove `/` from card formating --- .../en/finance/credit_card/american_express.js | 4 ++-- lib/locales/en/finance/credit_card/diners_club.js | 5 +++-- lib/locales/en/finance/credit_card/discover.js | 12 ++++++------ lib/locales/en/finance/credit_card/jcb.js | 6 +++--- lib/locales/en/finance/credit_card/laser.js | 16 ++++++++-------- lib/locales/en/finance/credit_card/maestro.js | 19 ++++++++++++++++--- lib/locales/en/finance/credit_card/mastercard.js | 4 ++-- lib/locales/en/finance/credit_card/solo.js | 6 +++--- lib/locales/en/finance/credit_card/switch.js | 6 +++--- lib/locales/en/finance/credit_card/visa.js | 4 ++-- 10 files changed, 48 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/locales/en/finance/credit_card/american_express.js b/lib/locales/en/finance/credit_card/american_express.js index 2a3d782b..c63363ab 100644 --- a/lib/locales/en/finance/credit_card/american_express.js +++ b/lib/locales/en/finance/credit_card/american_express.js @@ -1,4 +1,4 @@ module["exports"] = [ - "/34##-######-####L/", - "/37##-######-####L/" + "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 index ddc7eb35..4fb5d7ca 100644 --- a/lib/locales/en/finance/credit_card/diners_club.js +++ b/lib/locales/en/finance/credit_card/diners_club.js @@ -1,4 +1,5 @@ module["exports"] = [ - "/30[0-5]#-######-###L/", - "/368#-######-###L/" + "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 index d51aa159..cd84e840 100644 --- a/lib/locales/en/finance/credit_card/discover.js +++ b/lib/locales/en/finance/credit_card/discover.js @@ -1,8 +1,8 @@ module["exports"] = [ - "/6011-####-####-###L/", - "/65##-####-####-###L/", - "/64[4-9]#-####-####-###L/", - "/6011-62##-####-####-###L/", - "/65##-62##-####-####-###L/", - "/64[4-9]#-62##-####-####-###L/" + "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/finance/credit_card/jcb.js b/lib/locales/en/finance/credit_card/jcb.js index 03d83398..da396ebd 100644 --- a/lib/locales/en/finance/credit_card/jcb.js +++ b/lib/locales/en/finance/credit_card/jcb.js @@ -1,5 +1,5 @@ module["exports"] = [ - "/3528-####-####-###L/", - "/3529-####-####-###L/", - "/35[3-8]#-####-####-###L/" + "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 index 922da719..822c7b35 100644 --- a/lib/locales/en/finance/credit_card/laser.js +++ b/lib/locales/en/finance/credit_card/laser.js @@ -1,10 +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/" + "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 index da575f88..f652dd76 100644 --- a/lib/locales/en/finance/credit_card/maestro.js +++ b/lib/locales/en/finance/credit_card/maestro.js @@ -1,5 +1,18 @@ module["exports"] = [ - "/50#{9,16}L/", - "/5[6-8]#{9,16}L/", - "/56##{9,16}L/" + "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 index f6525eb8..81502c01 100644 --- a/lib/locales/en/finance/credit_card/mastercard.js +++ b/lib/locales/en/finance/credit_card/mastercard.js @@ -1,4 +1,4 @@ module["exports"] = [ - "/5[1-5]##-####-####-###L/", - "/6771-89##-####-###L/" + "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 index a577fb43..47d0d119 100644 --- a/lib/locales/en/finance/credit_card/solo.js +++ b/lib/locales/en/finance/credit_card/solo.js @@ -1,5 +1,5 @@ module["exports"] = [ - "/6767-####-####-###L/", - "/6767-####-####-####-#L/", - "/6767-####-####-####-##L/" + "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 index b12a4d07..1bae8278 100644 --- a/lib/locales/en/finance/credit_card/switch.js +++ b/lib/locales/en/finance/credit_card/switch.js @@ -1,5 +1,5 @@ module["exports"] = [ - "/6759-####-####-###L/", - "/6759-####-####-####-#L/", - "/6759-####-####-####-##L/" + "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 index 972eba8f..760f921d 100644 --- a/lib/locales/en/finance/credit_card/visa.js +++ b/lib/locales/en/finance/credit_card/visa.js @@ -1,4 +1,4 @@ module["exports"] = [ - "/4###########L/", - "/4###-####-####-###L/" + "4###########L", + "4###-####-####-###L" ]; -- cgit v1.2.3 From e7084554f8f8f5104805eec4d0f17bf7b2443bb6 Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Mon, 13 Jun 2016 20:48:42 +0200 Subject: add credit card function + helpers --- lib/finance.js | 48 +++++++++++++++++++++++++++++- lib/helpers.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) (limited to 'lib') 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 @@ -76,6 +76,99 @@ var Helpers = function (faker) { return str; }; + /** + * 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 * -- cgit v1.2.3 From d6c20353360ef1eefb56128eeb50ece4f5845372 Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Mon, 13 Jun 2016 22:21:47 +0200 Subject: add String.prototype.repeat in older versions of node - see PR #382 --- lib/helpers.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/helpers.js b/lib/helpers.js index 832732f5..c7957e08 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,3 +1,18 @@ +/** Define the String.prototype.repeat if it is not implemented.... See PR #382 +*/ +if(typeof String.prototype.repeat === "undefined") { + String.prototype.repeat = function(num) { + if(typeof num ==="undefined") { + num = 0; + } + var text = ""; + for(var i = 0; i < num; i++){ + text += this.toString(); + } + return text; + }; +} + /** * * @namespace faker.helpers -- cgit v1.2.3 From bf646e30e1981e094aaea89b82d68933b575a8df Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Sun, 26 Feb 2017 14:04:31 +0000 Subject: replace String.prototype.repeat with proper helper --- lib/helpers.js | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/helpers.js b/lib/helpers.js index 6fa763c8..8f3b5c71 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,18 +1,3 @@ -/** Define the String.prototype.repeat if it is not implemented.... See PR #382 -*/ -if(typeof String.prototype.repeat === "undefined") { - String.prototype.repeat = function(num) { - if(typeof num ==="undefined") { - num = 0; - } - var text = ""; - for(var i = 0; i < num; i++){ - text += this.toString(); - } - return text; - }; -} - /** * * @namespace faker.helpers @@ -127,6 +112,23 @@ var Helpers = function (faker) { 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 * @@ -153,14 +155,14 @@ var Helpers = function (faker) { 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); + 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)+ token[1].repeat(repetitions) + string.slice(token.index+token[0].length); + 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) -- cgit v1.2.3