forked from sigalor/iban-to-bic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (30 loc) · 1.14 KB
/
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
const ibantools = require('ibantools');
const datasets = require('./datasets');
const generateFiles = require('./src/generate');
module.exports = {
ibanIsValid(iban) {
iban = ibantools.electronicFormatIBAN(iban);
return ibantools.isValidIBAN(iban);
},
ibanToBic(iban) {
iban = ibantools.electronicFormatIBAN(iban);
if (!ibantools.isValidIBAN(iban)) return;
const country = iban.slice(0, 2);
if (!datasets.hasCountry(country)) return;
// see https://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country
let bankCode;
if (country === 'AT') bankCode = iban.substr(4, 5);
else if (country === 'BE') bankCode = iban.substr(4, 3);
else if (country === 'DE') bankCode = iban.substr(4, 8);
else if (country === 'ES') bankCode = iban.substr(4, 4);
else if (country === 'FR') bankCode = iban.substr(4, 5);
else if (country === 'LU') bankCode = iban.substr(4, 3);
else if (country === 'NL') bankCode = iban.substr(4, 4);
if (!bankCode) return;
return datasets.getData(country, bankCode);
},
async generate() {
await generateFiles();
await datasets.reload();
}
};