-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
375 lines (284 loc) · 8.94 KB
/
utils.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* Checks whether the given iban is a QR-IBAN or not.
*
* @param iban The IBAN to be checked.
* @returns `true` if the given IBAN is a QR-IBAN and `false` otherwise.
*/
export function isQRIBAN(iban: string): boolean {
iban = iban.replace(/ /g, "");
const QRIID = iban.substring(4, 9);
return +QRIID >= 30000 && +QRIID <= 31999;
}
/**
* Validates the given IBAN.
*
* @param iban The IBAN to be checked.
* @returns `true` if the checksum of the given IBAN is valid and `false` otherwise.
*/
export function isIBANValid(iban: string): boolean {
iban = iban
.replace(/ /g, "")
.toUpperCase();
// Move country code + checksum to end
iban = iban.substring(4) + iban.substring(0, 4);
// Calculate mod97
return mod97(iban) === 1;
}
/**
* Formats the given IBAN according the specifications to be easily readable.
*
* @param iban The IBAN to be formatted.
* @returns The formatted IBAN.
*/
export function formatIBAN(iban: string): string {
const ibanArray = iban
.replace(/ /g, "")
.match(/.{1,4}/g);
return ibanArray?.join(" ") ?? iban;
}
/**
* Checks whether the given reference is a QR-Reference or not.
*
* @param reference The Reference to be checked.
* @returns `true` if the given reference is a QR-Reference and `false` otherwise.
* @remarks The QR-Reference is a 27 digits long string containing only digits. The last digit is the checksum.
*/
export function isQRReference(reference: string): boolean {
reference = reference.replace(/ /g, "");
if(reference.length !== 27){
return false;
}
if(!/^\d+$/.test(reference)){
return false;
}
return true;
}
/**
* Validates the given QR-Reference.
*
* @param reference The reference to be checked.
* @returns `true` if the given reference is valid and `false` otherwise.
*/
export function isQRReferenceValid(reference: string): boolean {
reference = reference.replace(/ /g, "");
if(!isQRReference(reference)){
return false;
}
const ref = reference.substring(0, 26);
const checksum = reference.substring(26, 27);
const calculatedChecksum = calculateQRReferenceChecksum(ref);
return calculatedChecksum === checksum;
}
/**
* Checks whether the given reference is a SCOR-Reference or not.
*
* @param reference The Reference to be checked.
* @returns `true` if the given reference is a SCOR-Reference and `false` otherwise.
* @remarks The SCOR-Reference is an alphanumeric string beginning with 'RF' and containing a 2 digit checksum and a max 21 digits long reference.
*/
export function isSCORReference(reference: string): boolean {
reference = reference.replace(/ /g, "").toUpperCase();
if(!reference.startsWith("RF")){
return false;
}
if(reference.length < 5 || reference.length > 25){
return false;
}
if(!/^[\dA-Z]+$/.test(reference)){
return false;
}
return true;
}
/**
* Validates the given SCOR-Reference.
*
* @param reference The reference to be checked.
* @returns `true` if the given reference is valid and `false` otherwise.
*/
export function isSCORReferenceValid(reference: string): boolean {
reference = reference.replace(/ /g, "");
if(!isSCORReference(reference)){
return false;
}
const ref = reference.substring(4);
if(Number.isNaN(reference)){
return false;
}
const checksum = reference.substring(2, 4);
if(Number.isNaN(checksum)){
return false;
}
const calculatedChecksum = calculateSCORReferenceChecksum(ref);
return calculatedChecksum === checksum;
}
/**
* Calculates the checksum according to the ISO 11649 standard.
*
* @param reference The max 21 digits long reference (without the "RF" and the 2 digit checksum) whose checksum should be calculated.
* @returns The calculated checksum as 2 digit string.
*/
export function calculateSCORReferenceChecksum(reference: string): string {
reference = reference.replace(/ /g, "");
const checksum = 98 - mod97(`${reference}RF00`);
return `${checksum}`.padStart(2, "0");
}
/**
* Calculates the checksum according the specifications.
*
* @param reference The 26 digits long reference (without the checksum) whose checksum should be calculated.
* @returns The calculated checksum.
*/
export function calculateQRReferenceChecksum(reference: string): string {
return mod10(reference);
}
/**
* Formats the given QR-Reference according the specifications to be easily readable.
*
* @param reference The QR-Reference to be formatted.
* @returns The formatted QR-Reference.
*/
export function formatQRReference(reference: string): string {
const trimmedReference = reference.replace(/ /g, "");
const match = trimmedReference
.substring(2)
.match(/.{1,5}/g);
return match
? `${trimmedReference.substring(0, 2)} ${match.join(" ")}`
: reference;
}
/**
* Formats the given SCOR-Reference according the specifications to be easily readable.
*
* @param reference The SCOR-Reference to be formatted.
* @returns The formatted SCOR-Reference.
*/
export function formatSCORReference(reference: string): string {
const trimmedReference = reference.replace(/ /g, "");
const match = trimmedReference.match(/.{1,4}/g);
return match?.join(" ") ?? reference;
}
/**
* Detects the type of the given reference and formats it according the specifications to be easily readable.
*
* @param reference The reference to be formatted.
* @returns The formatted reference.
*/
export function formatReference(reference: string): string {
const referenceType = getReferenceType(reference);
if(referenceType === "QRR"){
return formatQRReference(reference);
} else if(referenceType === "SCOR"){
return formatSCORReference(reference);
}
return reference;
}
/**
* Formats the given amount according the specifications to be easily readable.
*
* @param amount Containing the amount to be formatted.
* @returns The formatted amount.
*/
export function formatAmount(amount: number): string {
const amountString = amount.toFixed(2);
const amountArray = amountString.split(".");
let formattedAmountWithoutDecimals = "";
for(let x = amountArray[0].length - 1, i = 1; x >= 0; x--, i++){
formattedAmountWithoutDecimals = amountArray[0][x] + formattedAmountWithoutDecimals;
if(i === 3){
formattedAmountWithoutDecimals = ` ${formattedAmountWithoutDecimals}`;
i = 0;
}
}
return `${formattedAmountWithoutDecimals.trim()}.${amountArray[1]}`;
}
/**
* Converts millimeters to points.
*
* @param millimeters The millimeters you want to convert to points.
* @returns The converted millimeters in points.
*/
export function mm2pt(millimeters: number): number {
return millimeters * 2.83465;
}
/**
* Converts points to millimeters.
*
* @param points The points you want to convert to millimeters.
* @returns The converted points in millimeters.
*/
export function pt2mm(points: number): number {
return points / 2.83465;
}
/**
* Converts millimeters to pixels.
*
* @param millimeters The millimeters you want to convert to pixels.
* @returns The converted millimeters in pixels.
*/
export function mm2px(millimeters: number): number {
return millimeters * 960 / 254;
}
/**
* Converts pixels to millimeters.
*
* @param pixels Containing the pixels you want to convert to millimeters.
* @returns The converted pixels in millimeters.
*/
export function px2mm(pixels: number): number {
return pixels * 254 / 960;
}
/**
* Detects the type of the given reference.
*
* @param reference The reference to get the type of.
* @returns The type of the given reference.
*/
export function getReferenceType(reference: string | undefined): "NON" | "QRR" | "SCOR" {
if(typeof reference === "undefined"){
return "NON";
} else if(isQRReference(reference)){
return "QRR";
} else {
return "SCOR";
}
}
/**
* Calculates the checksum according to the ISO 7064 standard.
*
* @param input The input whose checksum should be calculated.
* @returns The calculated checksum.
*/
function mod97(input: string): number {
// Convert letters to numbers (A = 10, B = 11, ..., Z = 35)
const charCodeOfLetterA = "A".charCodeAt(0);
const inputArr = input.split("");
for(let i = 0; i < inputArr.length; i++){
const charCode = inputArr[i].charCodeAt(0);
if(charCode >= charCodeOfLetterA){
inputArr[i] = `${charCode - charCodeOfLetterA + 10}`;
}
}
input = inputArr.join("");
// Apply the mod97 algorithm
let remainder = 0;
for(let i = 0; i < input.length; i++){
const digit = +input[i];
remainder = (10 * remainder + digit) % 97;
}
return remainder;
}
/**
* Calculates the checksum according to the ISO 7812-1 standard.
*
* @param input The input whose checksum should be calculated.
* @returns The calculated checksum.
*/
function mod10(input: string): string {
const trimmedInput = input.replace(/ /g, "");
const table = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5];
let carry = 0;
for(let i = 0; i < trimmedInput.length; i++){
carry = table[(carry + parseInt(trimmedInput.substring(i, i + 1), 10)) % 10];
}
return ((10 - carry) % 10).toString();
}