-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
29 lines (25 loc) · 1022 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
const ibantools = require('ibantools');
const datasets = require('./datasets');
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[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[country][bankCode];
},
};