@office1/validator-uic 0.1.2

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/LICENSE ADDED
File without changes
package/index.js ADDED
File without changes
package/index.ts ADDED
@@ -0,0 +1,62 @@
1
+ const UIC9_WEIGHTS_FIRST: number[] = [1, 2, 3, 4, 5, 6, 7, 8];
2
+ const UIC9_WEIGHTS_SECOND: number[] = [3, 4, 5, 6, 7, 8, 9, 10];
3
+
4
+ const UIC13_WEIGHTS_FIRST: number[] = [2, 7, 3, 5];
5
+ const UIC13_WEIGHTS_SECOND: number[] = [4, 9, 5, 7];
6
+
7
+ const extractDigitsFromString = (value: string) => Array.from(value)
8
+ .filter(char => /\d/.test(char))
9
+ .map(Number);
10
+
11
+ const calculateWeightedSum = (digits: number[], weights: number[]): number => {
12
+ return weights.reduce(
13
+ (sum: number, weight: number, index: number) => {
14
+ return sum + (digits[index] * weight)
15
+ }, 0
16
+ );
17
+ };
18
+
19
+ const checkUIC = (digits: number[], checkDigit: number, UIC_WEIGHTS_FIRST: number[], UIC_WEIGHTS_SECOND: number[]): boolean => {
20
+ let remainder: number = calculateWeightedSum(digits, UIC_WEIGHTS_FIRST) % 11;
21
+
22
+ if (remainder === 10)
23
+ {
24
+ remainder = calculateWeightedSum(digits, UIC_WEIGHTS_SECOND) % 11;
25
+ if (remainder === 10) remainder = 0;
26
+ }
27
+
28
+ return remainder === checkDigit;
29
+ };
30
+
31
+ const validateUIC9Digits = (value: string): boolean => {
32
+ const extractedDigits: number[] = extractDigitsFromString(value.slice(0,9));
33
+ const [digits, checkDigit]: [number[], number] = [
34
+ extractedDigits.slice(0,8),
35
+ extractedDigits[8],
36
+ ];
37
+
38
+ return checkUIC(digits, checkDigit, UIC9_WEIGHTS_FIRST, UIC9_WEIGHTS_SECOND);
39
+ };
40
+
41
+ const validateUIC13Digits = (value: string): boolean => {
42
+ const [part1, part2]: string[] = [value.slice(0, 9), value.slice(8, 13)];
43
+
44
+ if (!validateUIC9Digits(part1)) return false;
45
+
46
+ const extractedDigits: number[] = extractDigitsFromString(part2);
47
+ const [digits, checkDigit]: [number[], number] = [
48
+ extractedDigits.slice(0, 4),
49
+ extractedDigits[extractedDigits.length - 1]
50
+ ];
51
+
52
+ return checkUIC(digits, checkDigit, UIC13_WEIGHTS_FIRST, UIC13_WEIGHTS_SECOND);
53
+ };
54
+
55
+ export const isValidUIC = (value: string): boolean => {
56
+ const uicLength = value.length;
57
+
58
+ if (uicLength === 9) return validateUIC9Digits(value);
59
+ if (uicLength === 13) return validateUIC13Digits(value);
60
+
61
+ return false;
62
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@office1/validator-uic",
3
+ "version": "0.1.2",
4
+ "description": "Bulgarian UIC validator",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "github.com/office1/validator-uic"
12
+ },
13
+ "keywords": [
14
+ "uic",
15
+ "validator"
16
+ ],
17
+ "author": "Mykola Kharchenko <mykola.kharchenko@office1.bg>",
18
+ "license": "MIT"
19
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2016",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "skipLibCheck": true
9
+ }
10
+ }