upn-library-v2 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/upn-qr.es.js +74 -0
- package/package.json +15 -0
- package/src/index.js +87 -0
- package/vite.config.js +15 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import e from "qrcode";
|
|
2
|
+
//#region src/index.js
|
|
3
|
+
function t(e) {
|
|
4
|
+
return !e || typeof e !== Date ? "" : String(e.getDate()).padStart(2, "0") + "." + String(e.getMonth() + 1).padStart(2, "0") + "." + e.getFullYear();
|
|
5
|
+
}
|
|
6
|
+
function n(e) {
|
|
7
|
+
return (e * 100 + "").padStart(11, "0");
|
|
8
|
+
}
|
|
9
|
+
function r(e) {
|
|
10
|
+
return e.replace(/\s/g, "");
|
|
11
|
+
}
|
|
12
|
+
function i(e) {
|
|
13
|
+
return 19 + e.reduce((e, t) => e + t.length, 0);
|
|
14
|
+
}
|
|
15
|
+
function a(e) {
|
|
16
|
+
return e.reduce((e, t) => e + t + "\n", "") + i(e) + "";
|
|
17
|
+
}
|
|
18
|
+
function o(e, t, n = "") {
|
|
19
|
+
if (e[t] === void 0 || e[t] === null) throw Error(`${n} ${t} is not defined, define it using options.${n ? n + "." : ""}${t}`);
|
|
20
|
+
}
|
|
21
|
+
function s(e) {
|
|
22
|
+
if (!e) throw Error("Options are not defined");
|
|
23
|
+
for (let t of [
|
|
24
|
+
"payer",
|
|
25
|
+
"payee",
|
|
26
|
+
"price",
|
|
27
|
+
"payment_date",
|
|
28
|
+
"title_code",
|
|
29
|
+
"title",
|
|
30
|
+
"due_date",
|
|
31
|
+
"reference"
|
|
32
|
+
]) o(e, t);
|
|
33
|
+
for (let t of [
|
|
34
|
+
"name",
|
|
35
|
+
"surname",
|
|
36
|
+
"address",
|
|
37
|
+
"zip",
|
|
38
|
+
"city"
|
|
39
|
+
]) o(e.payer, t, "Payer");
|
|
40
|
+
for (let t of [
|
|
41
|
+
"iban",
|
|
42
|
+
"title",
|
|
43
|
+
"address",
|
|
44
|
+
"zip",
|
|
45
|
+
"city"
|
|
46
|
+
]) o(e.payee, t, "Payee");
|
|
47
|
+
}
|
|
48
|
+
async function c(i, o = 300) {
|
|
49
|
+
s(i);
|
|
50
|
+
let c = a([
|
|
51
|
+
"UPNQR",
|
|
52
|
+
r(i.payer.iban ?? ""),
|
|
53
|
+
"",
|
|
54
|
+
"",
|
|
55
|
+
"",
|
|
56
|
+
i.payer.name + " " + i.payer.surname,
|
|
57
|
+
i.payer.address + "",
|
|
58
|
+
i.payer.zip + " " + i.payer.city,
|
|
59
|
+
n(i.price) + "",
|
|
60
|
+
t(i.payment_date),
|
|
61
|
+
"",
|
|
62
|
+
i.title_code ?? "COST",
|
|
63
|
+
i.title + "",
|
|
64
|
+
t(i.due_date) + "",
|
|
65
|
+
r(i.payee.iban) + "",
|
|
66
|
+
i.reference + "",
|
|
67
|
+
i.payee.title + "",
|
|
68
|
+
i.payee.address + "",
|
|
69
|
+
i.payee.zip + " " + i.payee.city
|
|
70
|
+
]);
|
|
71
|
+
return e.toDataURL(c, { width: o });
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { c as generateUPNQR };
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "upn-library-v2",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "src/index.js",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "Tomaž Čede",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"description": "",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"qrcode": "^1.5.4"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"vite": "^8.0.3"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import QRCode from "qrcode";
|
|
2
|
+
|
|
3
|
+
function formatDate(date) {
|
|
4
|
+
if(!date || typeof date !== Date)
|
|
5
|
+
return ""
|
|
6
|
+
|
|
7
|
+
return String(date.getDate()).padStart(2, '0') + '.' +
|
|
8
|
+
String(date.getMonth() + 1).padStart(2, '0') + '.' +
|
|
9
|
+
date.getFullYear();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function formatPrice(price) {
|
|
13
|
+
return ((price * 100) + "").padStart(11, "0")
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function formatIban(iban) {
|
|
17
|
+
return iban.replace(/\s/g, '')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function calculateControlSum(fields) {
|
|
21
|
+
return 19 + fields.reduce((total, str) => {
|
|
22
|
+
return total + str.length
|
|
23
|
+
}, 0);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function generateText(fields) {
|
|
27
|
+
return fields.reduce((final, str) => {
|
|
28
|
+
return final + str + "\n"
|
|
29
|
+
}, "") + calculateControlSum(fields) + "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function validateOption(object, option, level = '') {
|
|
33
|
+
if (object[option] === undefined || object[option] === null) {
|
|
34
|
+
throw new Error(`${level} ${option} is not defined, define it using options.${level ? level + "." : ''}${option}`)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function validateOptions(options) {
|
|
39
|
+
if (!options) throw new Error('Options are not defined');
|
|
40
|
+
|
|
41
|
+
const requiredTopLevel = ['payer', 'payee', 'price', 'payment_date', 'title_code', 'title', 'due_date', 'reference'];
|
|
42
|
+
for (const field of requiredTopLevel) {
|
|
43
|
+
validateOption(options, field)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const payerFields = ['name', 'surname', 'address', 'zip', 'city'];
|
|
47
|
+
for (const field of payerFields) {
|
|
48
|
+
validateOption(options.payer, field, 'Payer')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const receiverFields = ['iban', 'title', 'address', 'zip', 'city'];
|
|
52
|
+
for (const field of receiverFields) {
|
|
53
|
+
validateOption(options.payee, field, 'Payee')
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function generateUPNQR(options, size = 300) {
|
|
58
|
+
validateOptions(options)
|
|
59
|
+
|
|
60
|
+
let fields = [
|
|
61
|
+
"UPNQR",
|
|
62
|
+
formatIban(options.payer.iban ?? ""),
|
|
63
|
+
"",
|
|
64
|
+
"",
|
|
65
|
+
"",
|
|
66
|
+
options.payer.name + " " + options.payer.surname + "",
|
|
67
|
+
options.payer.address + "",
|
|
68
|
+
options.payer.zip + " " + options.payer.city + "",
|
|
69
|
+
formatPrice(options.price) + "",
|
|
70
|
+
formatDate(options.payment_date),
|
|
71
|
+
"",
|
|
72
|
+
options.title_code ?? 'COST',
|
|
73
|
+
options.title + "",
|
|
74
|
+
formatDate(options.due_date) + "",
|
|
75
|
+
formatIban(options.payee.iban) + "",
|
|
76
|
+
options.reference + "",
|
|
77
|
+
options.payee.title + "",
|
|
78
|
+
options.payee.address + "",
|
|
79
|
+
options.payee.zip + " " + options.payee.city + "",
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
let text = generateText(fields)
|
|
83
|
+
|
|
84
|
+
return QRCode.toDataURL(text, {
|
|
85
|
+
width: size,
|
|
86
|
+
});
|
|
87
|
+
}
|
package/vite.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineConfig } from "vite";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
build: {
|
|
5
|
+
lib: {
|
|
6
|
+
entry: "src/index.js",
|
|
7
|
+
name: "UPNQR",
|
|
8
|
+
formats: ["es"],
|
|
9
|
+
fileName: () => "upn-qr.es.js",
|
|
10
|
+
},
|
|
11
|
+
rollupOptions: {
|
|
12
|
+
external: ["qrcode"],
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|