@luftborn/custom-elements 2.1.1 → 2.2.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/demo/index.html +22 -0
- package/demo/index.js +344 -61
- package/demo/index.min.js +343 -60
- package/demo/index.min.js.map +1 -1
- package/dist/elements/Elements.d.ts +3 -1
- package/dist/elements/Elements.js +5 -1
- package/dist/elements/Elements.js.map +1 -1
- package/dist/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.d.ts +14 -0
- package/dist/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.js +83 -0
- package/dist/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.js.map +1 -0
- package/dist/elements/SEPersonalNumberElement/SEPersonalNumberElement.d.ts +14 -0
- package/dist/elements/SEPersonalNumberElement/SEPersonalNumberElement.js +83 -0
- package/dist/elements/SEPersonalNumberElement/SEPersonalNumberElement.js.map +1 -0
- package/dist/framework/Validation/Validators/SECompanyRegistration.d.ts +9 -0
- package/dist/framework/Validation/Validators/SECompanyRegistration.js +38 -0
- package/dist/framework/Validation/Validators/SECompanyRegistration.js.map +1 -0
- package/dist/framework/Validation/Validators/SEPersonalNumber.d.ts +6 -0
- package/dist/framework/Validation/Validators/SEPersonalNumber.js +71 -0
- package/dist/framework/Validation/Validators/SEPersonalNumber.js.map +1 -0
- package/package.json +2 -2
- package/src/elements/Elements.ts +5 -1
- package/src/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.ts +73 -0
- package/src/elements/SEPersonalNumberElement/SEPersonalNumberElement.ts +73 -0
- package/src/framework/Validation/Validators/SECompanyRegistration.ts +40 -0
- package/src/framework/Validation/Validators/SEPersonalNumber.ts +80 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import IValidation from '../IValidator.interface';
|
|
2
|
+
|
|
3
|
+
export default class SEPersonalNumberValidator implements IValidation {
|
|
4
|
+
private regex: RegExp = new RegExp(
|
|
5
|
+
'^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\+\-]?)((?!000)\d{3})(\d)$'
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
isSatisfiedBy(input: string, allowEmpty: boolean = true): boolean {
|
|
10
|
+
if (allowEmpty && !input) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const matches = this.regex.exec(input);
|
|
15
|
+
if (!matches || matches.length < 7) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const groups = matches;
|
|
20
|
+
const decade = groups[2];
|
|
21
|
+
let century;
|
|
22
|
+
|
|
23
|
+
if (groups[1]) {
|
|
24
|
+
century = groups[1];
|
|
25
|
+
} else {
|
|
26
|
+
let born = new Date().getFullYear() - parseInt(decade, 10);
|
|
27
|
+
if (groups[5] && groups[5] === '+') {
|
|
28
|
+
born -= 100;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
century = born.toString().substring(0, 2);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Create date time object.
|
|
35
|
+
let day = parseInt(groups[4], 10);
|
|
36
|
+
const isCoordinationNumber = day > 60;
|
|
37
|
+
day = isCoordinationNumber ? day - 60 : day;
|
|
38
|
+
|
|
39
|
+
const realDay = day;
|
|
40
|
+
const year = decade;
|
|
41
|
+
const fullYear = century + decade;
|
|
42
|
+
const month = groups[3];
|
|
43
|
+
const numbers = groups[6] + groups[7];
|
|
44
|
+
const controlNumber = groups[7];
|
|
45
|
+
|
|
46
|
+
// Try parse date-time to make sure it's actually a real date.
|
|
47
|
+
if (!Date.parse(`${fullYear}-${month}-${realDay}`)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (this.luhn(`${year}${month}${day}${groups[6]}`) !== parseInt(controlNumber, 10)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private luhn(value: string): number {
|
|
59
|
+
// Luhm algorithm doubles every other number in the value.
|
|
60
|
+
// To get the correct checksum digit we aught to append a 0 on the sequence.
|
|
61
|
+
// If the result becomes a two digit number, subtract 9 from the value.
|
|
62
|
+
// If the total sum is not a 0, the last checksum value should be subtracted from 10.
|
|
63
|
+
// The resulting value is the check value that we use as control number.
|
|
64
|
+
|
|
65
|
+
// The value passed is a string, so we aught to get the actual integer value from each char (i.e., subtract '0' which is 48).
|
|
66
|
+
const t: number[] = value.split('').map((d) => parseInt(d, 10));
|
|
67
|
+
let sum = 0;
|
|
68
|
+
let temp;
|
|
69
|
+
for (let i = 0; i < t.length; i++) {
|
|
70
|
+
temp = t[i];
|
|
71
|
+
temp *= 2 - (i % 2);
|
|
72
|
+
if (temp > 9) {
|
|
73
|
+
temp -= 9;
|
|
74
|
+
}
|
|
75
|
+
sum += temp;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return Math.ceil(sum / 10.0) * 10 - sum;
|
|
79
|
+
}
|
|
80
|
+
}
|