-
Notifications
You must be signed in to change notification settings - Fork 108
/
core.js
49 lines (40 loc) · 1.08 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var type = require('./type');
function BanksDB(data) {
var banksData = [];
var prefixes = {};
if (Array.isArray(data)) {
banksData = data;
} else if (data) {
banksData.push(data);
} else {
console.warn('Empty or invalid arguments of Banks DB Core call. Check Banks DB docs for details.');
}
this.data = banksData.reduce(function (acc, current) {
return acc.concat(current);
}, []);
this.data.forEach(function (bank) {
bank.code = bank.country + '-' + bank.name;
bank.prefixes.forEach(function (prefix) {
prefixes[prefix] = bank;
});
});
this.findBank = function (cardNumber) {
cardNumber = cardNumber || '';
var card = cardNumber.toString().replace(/[^\d]/g, '');
var first5 = card.substr(0, 5);
var first6 = card.substr(0, 6);
var bank = prefixes[first6] || prefixes[first5];
var result = {
type: type(card)
};
if (bank) {
for (var el in bank) {
result[el] = bank[el];
}
}
return result;
};
}
module.exports = function banksDBCore(data) {
return new BanksDB(data);
};