Skip to content

Commit

Permalink
Add IBAN and BIC generator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CoDEmanX committed Apr 7, 2016
1 parent 38e890e commit 94b65cb
Show file tree
Hide file tree
Showing 5 changed files with 1,227 additions and 5 deletions.
66 changes: 66 additions & 0 deletions lib/finance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @namespace faker.finance
*/
var Finance = function (faker) {
var ibanLib = require("./iban");
var Helpers = faker.helpers,
self = this;

Expand Down Expand Up @@ -150,6 +151,71 @@ var Finance = function (faker) {

return address;
}

/**
* iban
*
* @method faker.finance.iban
*/
self.iban = function (formatted) {
var ibanFormat = faker.random.arrayElement(ibanLib.formats);
var s = "";
var count = 0;
for (var b = 0; b < ibanFormat.bban.length; b++) {
var bban = ibanFormat.bban[b];
var c = bban.count;
count += bban.count;
while (c > 0) {
if (bban.type == "a") {
s += faker.random.arrayElement(ibanLib.alpha);
} else if (bban.type == "c") {
if (faker.random.number(100) < 80) {
s += faker.random.number(9);
} else {
s += faker.random.arrayElement(ibanLib.alpha);
}
} else {
if (c >= 3 && faker.random.number(100) < 30) {
if (faker.random.boolean()) {
s += faker.random.arrayElement(ibanLib.pattern100);
c -= 2;
} else {
s += faker.random.arrayElement(ibanLib.pattern10);
c--;
}
} else {
s += faker.random.number(9);
}
}
c--;
}
s = s.substring(0, count);
}
var checksum = 98 - ibanLib.mod97(ibanLib.toDigitString(s + ibanFormat.country + "00"));
if (checksum < 10) {
checksum = "0" + checksum;
}
var iban = ibanFormat.country + checksum + s;
return formatted ? iban.match(/.{1,4}/g).join(" ") : iban;
}

/**
* bic
*
* @method faker.finance.bic
*/
self.bic = function () {
var vowels = ["A", "E", "I", "O", "U"];
var prob = faker.random.number(100);
return Helpers.replaceSymbols("???") +
faker.random.arrayElement(vowels) +
faker.random.arrayElement(ibanLib.iso3166) +
Helpers.replaceSymbols("?") + "1" +
(prob < 10 ?
Helpers.replaceSymbols("?" + faker.random.arrayElement(vowels) + "?") :
prob < 40 ?
Helpers.replaceSymbols("###") : "");
}
}

module['exports'] = Finance;
6 changes: 3 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ var Helpers = function (faker) {
*/
self.replaceSymbols = function (string) {
string = string || "";
var alpha = ['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']
var alpha = ['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']
var str = '';

for (var i = 0; i < string.length; i++) {
if (string.charAt(i) == "#") {
str += faker.random.number(9);
} else if (string.charAt(i) == "?") {
str += faker.random.arrayElement(alpha);
} else if (string.charAt(i) == "?") {
str += faker.random.arrayElement(alpha);
} else {
str += string.charAt(i);
}
Expand Down
Loading

0 comments on commit 94b65cb

Please sign in to comment.