forked from ramoona/banks-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 837 Bytes
/
index.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
var type = require('./type');
var data = require('./banks/index');
var banks = [];
data.forEach(function (item) {
banks = banks.concat(item);
});
banks.forEach(function (bank) {
bank.code = bank.country + '-' + bank.name;
});
var prefixes = {};
for (var i = 0; i < banks.length; i++) {
for (var j = 0; j < banks[i].prefixes.length; j++) {
prefixes[banks[i].prefixes[j]] = banks[i];
}
}
module.exports = function findBank(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.data = banks;