@luftborn/custom-elements 2.1.2 → 2.3.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.
Files changed (29) hide show
  1. package/demo/index.html +22 -0
  2. package/demo/index.js +352 -66
  3. package/demo/index.min.js +351 -65
  4. package/demo/index.min.js.map +1 -1
  5. package/dist/elements/Elements.d.ts +3 -1
  6. package/dist/elements/Elements.js +5 -1
  7. package/dist/elements/Elements.js.map +1 -1
  8. package/dist/elements/InternationaPhoneNumber/InternationaPhoneNumberElement.d.ts +1 -0
  9. package/dist/elements/InternationaPhoneNumber/InternationaPhoneNumberElement.js +8 -5
  10. package/dist/elements/InternationaPhoneNumber/InternationaPhoneNumberElement.js.map +1 -1
  11. package/dist/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.d.ts +14 -0
  12. package/dist/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.js +83 -0
  13. package/dist/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.js.map +1 -0
  14. package/dist/elements/SEPersonalNumberElement/SEPersonalNumberElement.d.ts +14 -0
  15. package/dist/elements/SEPersonalNumberElement/SEPersonalNumberElement.js +83 -0
  16. package/dist/elements/SEPersonalNumberElement/SEPersonalNumberElement.js.map +1 -0
  17. package/dist/framework/Validation/Validators/SECompanyRegistration.d.ts +9 -0
  18. package/dist/framework/Validation/Validators/SECompanyRegistration.js +38 -0
  19. package/dist/framework/Validation/Validators/SECompanyRegistration.js.map +1 -0
  20. package/dist/framework/Validation/Validators/SEPersonalNumber.d.ts +6 -0
  21. package/dist/framework/Validation/Validators/SEPersonalNumber.js +71 -0
  22. package/dist/framework/Validation/Validators/SEPersonalNumber.js.map +1 -0
  23. package/package.json +1 -1
  24. package/src/elements/Elements.ts +5 -1
  25. package/src/elements/InternationaPhoneNumber/InternationaPhoneNumberElement.ts +7 -5
  26. package/src/elements/SECompanyRegistrationElement/SECompanyRegistrationElement.ts +73 -0
  27. package/src/elements/SEPersonalNumberElement/SEPersonalNumberElement.ts +73 -0
  28. package/src/framework/Validation/Validators/SECompanyRegistration.ts +40 -0
  29. package/src/framework/Validation/Validators/SEPersonalNumber.ts +80 -0
@@ -0,0 +1,73 @@
1
+ import CustomElement from '../../framework/custom-element.decorator';
2
+ import { CustomInputElement } from '../../framework/CustomInputElement';
3
+ import { CustomElementEventArgs } from '../../framework/CustomEvents';
4
+ import SEPersonalNumberValidator from '../../framework/Validation/Validators/SEPersonalNumber';
5
+
6
+ @CustomElement({
7
+ selector: 'se-personal-number-element',
8
+ template: `
9
+ <div class="wrapper">
10
+ <input type="text" id='se-personal-number-field' placeholder='(YY)YYMMDD-SSSS'/>
11
+ </div>`,
12
+ style: `
13
+ :host{
14
+ width:100%;
15
+ }
16
+ .wrapper{
17
+ display:flex;
18
+ }
19
+ input{
20
+ box-sizing: border-box;
21
+ width: 100% !important;
22
+ border: none;
23
+ background-color: #f1f4ff;
24
+ margin: 2px;
25
+ resize: none;
26
+ }`,
27
+ useShadow: true,
28
+ })
29
+ export class SEPersonalNumberElement extends CustomInputElement {
30
+ text: HTMLInputElement;
31
+ validator: SEPersonalNumberValidator = new SEPersonalNumberValidator();
32
+
33
+ constructor() {
34
+ super();
35
+ }
36
+
37
+ get value(): string {
38
+ return `${this.text.value}`;
39
+ }
40
+
41
+ set value(value: string) {
42
+ this.text.value = value;
43
+ }
44
+
45
+ get valid(): boolean {
46
+ return this.validator.isSatisfiedBy(this.value, !this.required);
47
+ }
48
+
49
+ connectedCallback(): void {
50
+ super.connectedCallback();
51
+ }
52
+
53
+ initChildInputs() {
54
+ this.text = super.getChildInput('#se-personal-number-field');
55
+ this.text.addEventListener('change', this.change.bind(this));
56
+ if (this.required) {
57
+ this.text.setAttribute('required', '');
58
+ }
59
+ }
60
+
61
+ // events
62
+ public change($event): void {
63
+ this.touched = true;
64
+ this.onChange.emit(new CustomElementEventArgs(this.value, 'change'));
65
+ }
66
+
67
+ public validate(): void {
68
+ this.valid;
69
+ this.onValidate.emit(
70
+ new CustomElementEventArgs(this.value, 'validate'),
71
+ );
72
+ }
73
+ }
@@ -0,0 +1,40 @@
1
+ import IValidation from '../IValidator.interface';
2
+
3
+ export default class SECompanyRegistrationValidator implements IValidation {
4
+ private ORG_NR_SE_LUHN_WEIGHTS = [2, 1, 2, 1, 2, 1, 2, 1, 2, 1];
5
+ private weighted(coefficients: Array<number>) {
6
+ return (value: number, index: number) => value * coefficients[index];
7
+ }
8
+
9
+ private sum = (a: number, b: number) => a + b;
10
+
11
+ isSatisfiedBy(input: string, allowEmpty: boolean): boolean {
12
+ if (allowEmpty && !input) {
13
+ return true;
14
+ }
15
+
16
+ return this.orgNrSe(input);
17
+ }
18
+
19
+ private orgNrSe(value: string) {
20
+ return this.checkLuhnModulus10(value, this.ORG_NR_SE_LUHN_WEIGHTS);
21
+ }
22
+
23
+ private checkLuhnModulus10 (testValue: string, luhnWeights: number[]) {
24
+ const length = luhnWeights.length;
25
+ const testValue$ = testValue.replace("-", "");
26
+ if (testValue$.length !== length) {
27
+ return false;
28
+ }
29
+
30
+ if (Number.isNaN(parseInt(testValue$, 10))) {
31
+ return false;
32
+ }
33
+
34
+ const digits = testValue$.split("").map(Number);
35
+ const luhnF = (n: number) => (n < length ? n : n - (length - 1));
36
+ const luhnSum = luhnWeights.map(this.weighted(digits)).map(luhnF).reduce(this.sum);
37
+ const controlSum = length - (+luhnSum % length);
38
+ return controlSum % 10 === 0;
39
+ }
40
+ }
@@ -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
+ }