-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·66 lines (56 loc) · 1.53 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
const qrcode = require('qrcode-terminal');
const clipboardy = require('clipboardy');
let text = clipboardy.readSync();
const remittanceParser = /[\+\*]{3}[0-9\/\s]+[\+\*]{3}/;
const ibanParser = /[A-Z]{2}[0-9 ]{2,}/;
const amountParser = /(EUR|euro|€)?\s*(\s?\.?,?\d\.?,?\s?)+\s*(EUR|euro|€)?/;
let remittance = '';
let amount = 0.01;
if (!ibanParser.test(text)) {
throw Error('Unable to locate an IBAN account number on clipboard.');
}
let [iban] = ibanParser.exec(text);
text = text.replace(iban, '');
iban = iban.replace(/[^A-Z0-9]/g, '');
console.log(`IBAN: ${iban}`);
if (amountParser.test(text)) {
[amount] = amountParser.exec(text);
text = text.replace(amount, '');
if (/\./.test(amount) && /,/.test(amount)) {
amount = amount.replace(/\./, '');
}
amount = parseFloat(amount.replace(/[^0-9.,]/g, '').replace(/,/, '.'));
}
console.log(`Amount: EUR${amount}`);
if (remittanceParser.test(text)) {
[remittance] = remittanceParser.exec(text);
text = text.replace(remittance, '');
remittance = remittance.replace(/\s/g, '');
}
console.log(`Remittance Reference: ${remittance}`);
const serviceTag = 'BCD';
const version = '002';
const characterSet = 1;
const identification = 'SCT';
const bic = '';
const name = '';
const purpose = '';
const information = '';
qrcode.setErrorLevel('M');
qrcode.generate(
[
serviceTag,
version,
characterSet,
identification,
bic,
name,
iban,
`EUR${amount}`,
purpose,
remittance,
information
].join('\n'),
{small: true}
);