@captinbv/tin-validator 0.0.0-stage → 0.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/LICENSE +201 -0
- package/README.md +100 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/validators/countries/AustriaTinValidator.d.ts +2 -0
- package/dist/validators/countries/AustriaTinValidator.js +23 -0
- package/dist/validators/countries/BelgiumTinValidator.d.ts +2 -0
- package/dist/validators/countries/BelgiumTinValidator.js +22 -0
- package/dist/validators/countries/BulgariaTinValidator.d.ts +2 -0
- package/dist/validators/countries/BulgariaTinValidator.js +24 -0
- package/dist/validators/countries/CanadaTinValidator.d.ts +2 -0
- package/dist/validators/countries/CanadaTinValidator.js +33 -0
- package/dist/validators/countries/CroatiaTinValidator.d.ts +2 -0
- package/dist/validators/countries/CroatiaTinValidator.js +29 -0
- package/dist/validators/countries/CyprusTinValidator.d.ts +2 -0
- package/dist/validators/countries/CyprusTinValidator.js +31 -0
- package/dist/validators/countries/CzechiaTinValidator.d.ts +2 -0
- package/dist/validators/countries/CzechiaTinValidator.js +29 -0
- package/dist/validators/countries/DenmarkTinValidator.d.ts +2 -0
- package/dist/validators/countries/DenmarkTinValidator.js +50 -0
- package/dist/validators/countries/EstoniaTinValidator.d.ts +2 -0
- package/dist/validators/countries/EstoniaTinValidator.js +34 -0
- package/dist/validators/countries/FinlandTinValidator.d.ts +2 -0
- package/dist/validators/countries/FinlandTinValidator.js +59 -0
- package/dist/validators/countries/FranceTinValidator.d.ts +2 -0
- package/dist/validators/countries/FranceTinValidator.js +24 -0
- package/dist/validators/countries/GermanyTinValidator.d.ts +2 -0
- package/dist/validators/countries/GermanyTinValidator.js +28 -0
- package/dist/validators/countries/GreeceTinValidator.d.ts +2 -0
- package/dist/validators/countries/GreeceTinValidator.js +18 -0
- package/dist/validators/countries/ItalyTinValidator.d.ts +2 -0
- package/dist/validators/countries/ItalyTinValidator.js +80 -0
- package/dist/validators/countries/NetherlandsTinValidator.d.ts +2 -0
- package/dist/validators/countries/NetherlandsTinValidator.js +28 -0
- package/dist/validators/countries/PortugalTinValidator.d.ts +2 -0
- package/dist/validators/countries/PortugalTinValidator.js +30 -0
- package/dist/validators/countries/RomaniaTinValidator.d.ts +2 -0
- package/dist/validators/countries/RomaniaTinValidator.js +33 -0
- package/dist/validators/countries/SloveniaTinValidator.d.ts +2 -0
- package/dist/validators/countries/SloveniaTinValidator.js +27 -0
- package/dist/validators/countries/SpainTinValidator.d.ts +2 -0
- package/dist/validators/countries/SpainTinValidator.js +117 -0
- package/dist/validators/countries/UkTinValidator.d.ts +2 -0
- package/dist/validators/countries/UkTinValidator.js +34 -0
- package/dist/validators/validator-util.d.ts +1 -0
- package/dist/validators/validator-util.js +11 -0
- package/dist/validators/validator.d.ts +1 -0
- package/dist/validators/validator.js +50 -0
- package/package.json +25 -3
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FinlandTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^[0-9]{6}[-+A][0-9]{3}[0-9A-Z]$/;
|
|
6
|
+
const CHECKSUM_MAP = "0123456789ABCDEFHJKLMNPRSTUVWXY";
|
|
7
|
+
const FINLAND_INVALID_CHARS = /[^0-9A-Za-z+-]/g;
|
|
8
|
+
exports.FinlandTinValidator = {
|
|
9
|
+
isCountrySupported: (countryCode) => {
|
|
10
|
+
return new Set(["FI", "FIN", "246"]).has(countryCode.toUpperCase());
|
|
11
|
+
},
|
|
12
|
+
validate: (tin) => {
|
|
13
|
+
if (tin == null) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (/[a-z]$/.test(tin)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
tin = (0, validator_util_1.sanitise)(tin, "FI", FINLAND_INVALID_CHARS);
|
|
20
|
+
if (tin == null || !TIN_REGEX.test(tin)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const date = tin.substring(0, 6);
|
|
24
|
+
const separator = tin.charAt(6);
|
|
25
|
+
const individualNumber = tin.substring(7, 10);
|
|
26
|
+
const control = tin.charAt(10);
|
|
27
|
+
if (!isValidDate(date, separator)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const checksum = Number(date + individualNumber) % 31;
|
|
31
|
+
return CHECKSUM_MAP.charAt(checksum) === control;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
function isValidDate(date, separator) {
|
|
35
|
+
const day = Number(date.substring(0, 2));
|
|
36
|
+
const month = Number(date.substring(2, 4));
|
|
37
|
+
const year = Number(date.substring(4, 6));
|
|
38
|
+
let fullYear;
|
|
39
|
+
switch (separator) {
|
|
40
|
+
case "+":
|
|
41
|
+
fullYear = 1800 + year;
|
|
42
|
+
break;
|
|
43
|
+
case "-":
|
|
44
|
+
fullYear = 1900 + year;
|
|
45
|
+
break;
|
|
46
|
+
case "A":
|
|
47
|
+
fullYear = 2000 + year;
|
|
48
|
+
break;
|
|
49
|
+
default:
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (month < 1 || month > 12 || day < 1) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const parsedDate = new Date(Date.UTC(fullYear, month - 1, day));
|
|
56
|
+
return (parsedDate.getUTCFullYear() === fullYear &&
|
|
57
|
+
parsedDate.getUTCMonth() === month - 1 &&
|
|
58
|
+
parsedDate.getUTCDate() === day);
|
|
59
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FranceTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^[0-9]{13}$/;
|
|
6
|
+
const FRANCE_INVALID_CHARS = /[^0-9]/g;
|
|
7
|
+
exports.FranceTinValidator = {
|
|
8
|
+
isCountrySupported: (countryCode) => {
|
|
9
|
+
return new Set(["FR", "FRA", "250"]).has(countryCode.toUpperCase());
|
|
10
|
+
},
|
|
11
|
+
validate: (tin) => {
|
|
12
|
+
if (tin == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
tin = (0, validator_util_1.sanitise)(tin, "FR", FRANCE_INVALID_CHARS);
|
|
16
|
+
if (tin == null || !TIN_REGEX.test(tin)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const value = Number.parseInt(tin.substring(0, 10), 10);
|
|
20
|
+
const control = Number.parseInt(tin.substring(10), 10);
|
|
21
|
+
const checksum = value % 511;
|
|
22
|
+
return checksum === control;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GermanyTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^[1-9][0-9]{10}$/;
|
|
6
|
+
const GERMANY_INVALID_CHARS = /[^0-9]/g;
|
|
7
|
+
exports.GermanyTinValidator = {
|
|
8
|
+
isCountrySupported: (countryCode) => {
|
|
9
|
+
return new Set(["DE", "DEU", "276"]).has(countryCode.toUpperCase());
|
|
10
|
+
},
|
|
11
|
+
validate: (tin) => {
|
|
12
|
+
if (tin == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
tin = (0, validator_util_1.sanitise)(tin, "DE", GERMANY_INVALID_CHARS);
|
|
16
|
+
if (tin == null || !TIN_REGEX.test(tin)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
let sum = 0;
|
|
20
|
+
for (let i = 0; i < 10; i++) {
|
|
21
|
+
const c = Number(tin.charAt(i));
|
|
22
|
+
const tmp = (sum + c) % 10;
|
|
23
|
+
sum = tmp === 0 ? 9 : (2 * tmp) % 11;
|
|
24
|
+
}
|
|
25
|
+
const checksum = (11 - sum) % 10;
|
|
26
|
+
return checksum === Number(tin.charAt(10));
|
|
27
|
+
},
|
|
28
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GreeceTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^[0-9]{9}$/;
|
|
6
|
+
const GREECE_INVALID_CHARS = /[^0-9]/g;
|
|
7
|
+
exports.GreeceTinValidator = {
|
|
8
|
+
isCountrySupported: (countryCode) => {
|
|
9
|
+
return new Set(["GR", "GRC", "300"]).has(countryCode.toUpperCase());
|
|
10
|
+
},
|
|
11
|
+
validate: (tin) => {
|
|
12
|
+
if (tin == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
tin = (0, validator_util_1.sanitise)(tin, "GR", GREECE_INVALID_CHARS);
|
|
16
|
+
return tin !== null && TIN_REGEX.test(tin);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ItalyTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const CODE_REGEX = /^[A-Z]{6}[0-9]{2}[ABCDEHLMPRST][0-9]{2}[A-Z][0-9]{3}[A-Z]$/;
|
|
6
|
+
const CARD_REGEX = /^[0-9]{11}$/;
|
|
7
|
+
const ITALY_INVALID_CHARS = /[^A-Z0-9]/gi;
|
|
8
|
+
const NUMBER_MAP = [
|
|
9
|
+
[1, 0, 5, 7, 9, 13, 15, 17, 19, 21],
|
|
10
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
11
|
+
];
|
|
12
|
+
const CHARACTER_MAP = [
|
|
13
|
+
[
|
|
14
|
+
1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18,
|
|
15
|
+
20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23,
|
|
16
|
+
],
|
|
17
|
+
[
|
|
18
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
|
19
|
+
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
|
20
|
+
],
|
|
21
|
+
];
|
|
22
|
+
exports.ItalyTinValidator = {
|
|
23
|
+
isCountrySupported: (countryCode) => {
|
|
24
|
+
return ["IT", "ITA", "380"].includes(countryCode.toUpperCase());
|
|
25
|
+
},
|
|
26
|
+
validate: (tin) => {
|
|
27
|
+
if (tin == null) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
tin = (0, validator_util_1.sanitise)(tin, "IT", ITALY_INVALID_CHARS);
|
|
31
|
+
if (tin == null) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
tin = tin.toUpperCase();
|
|
35
|
+
if (CODE_REGEX.test(tin)) {
|
|
36
|
+
return isValidCode(tin);
|
|
37
|
+
}
|
|
38
|
+
if (CARD_REGEX.test(tin)) {
|
|
39
|
+
return isValidCard(tin);
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
function isValidCode(tin) {
|
|
45
|
+
let sum = 0;
|
|
46
|
+
for (let i = 0; i < 15; i++) {
|
|
47
|
+
let charCode = tin.charCodeAt(i);
|
|
48
|
+
let map;
|
|
49
|
+
if (charCode >= 48 && charCode <= 57) {
|
|
50
|
+
map = NUMBER_MAP;
|
|
51
|
+
charCode -= 48;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
map = CHARACTER_MAP;
|
|
55
|
+
charCode -= 65;
|
|
56
|
+
}
|
|
57
|
+
sum += map[i % 2][charCode];
|
|
58
|
+
}
|
|
59
|
+
const control = String.fromCharCode((sum % 26) + 65);
|
|
60
|
+
return tin.charAt(15) === control;
|
|
61
|
+
}
|
|
62
|
+
function isValidCard(tin) {
|
|
63
|
+
if (tin.substring(0, 7) === "0000000") {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const suffix = parseInt(tin.substring(7, 10), 10);
|
|
67
|
+
if (suffix === 0 ||
|
|
68
|
+
(suffix > 100 && suffix !== 120 && suffix !== 121)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
let sum = 0;
|
|
72
|
+
for (let i = 0; i < 10; i++) {
|
|
73
|
+
const digit = Number(tin.charAt(i));
|
|
74
|
+
const value = digit * (i % 2 + 1);
|
|
75
|
+
sum += Math.floor(value / 10) + (value % 10);
|
|
76
|
+
}
|
|
77
|
+
const checksum = (10 - (sum % 10)) % 10;
|
|
78
|
+
const control = Number(tin.charAt(10));
|
|
79
|
+
return checksum === control;
|
|
80
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NetherlandsTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^[0-9]{9}$/;
|
|
6
|
+
exports.NetherlandsTinValidator = {
|
|
7
|
+
isCountrySupported(countryCode) {
|
|
8
|
+
return ["NL", "NLD", "528"].includes(countryCode);
|
|
9
|
+
},
|
|
10
|
+
validate(tin) {
|
|
11
|
+
if (tin === null) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const value = (0, validator_util_1.sanitise)(tin, "NL");
|
|
15
|
+
if (value === null || !TIN_REGEX.test(value)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
let sum = 0;
|
|
19
|
+
for (let i = 0; i < 8; i++) {
|
|
20
|
+
sum += Number(value.charAt(i)) * (9 - i);
|
|
21
|
+
}
|
|
22
|
+
const remainder = sum % 11;
|
|
23
|
+
if (remainder === 10) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return Number(value.charAt(8)) === remainder;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortugalTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^(?:[123568][0-9]{8}|(?:45|7[0124579]|9[0189])[0-9]{7})$/;
|
|
6
|
+
const PORTUGAL_INVALID_CHARS = /[^0-9]/gi;
|
|
7
|
+
exports.PortugalTinValidator = {
|
|
8
|
+
isCountrySupported: (countryCode) => {
|
|
9
|
+
return ["PT", "PRT", "620"].includes(countryCode.toUpperCase());
|
|
10
|
+
},
|
|
11
|
+
validate: (tin) => {
|
|
12
|
+
if (tin == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
tin = (0, validator_util_1.sanitise)(tin, "PT", PORTUGAL_INVALID_CHARS);
|
|
16
|
+
if (tin == null || !TIN_REGEX.test(tin)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return isValidTin(tin);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
function isValidTin(tin) {
|
|
23
|
+
let total = 0;
|
|
24
|
+
for (let i = 0; i < 8; i++) {
|
|
25
|
+
total += Number(tin.charAt(i)) * (9 - i);
|
|
26
|
+
}
|
|
27
|
+
const mod11 = total % 11;
|
|
28
|
+
const control = mod11 < 2 ? 0 : 11 - mod11;
|
|
29
|
+
return control === Number(tin.charAt(8));
|
|
30
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RomaniaTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const CNP_REGEX = /^[0-9]{13}$/;
|
|
6
|
+
const ROMANIA_INVALID_CHARS = /[^0-9]/gi;
|
|
7
|
+
const WEIGHTS = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9];
|
|
8
|
+
exports.RomaniaTinValidator = {
|
|
9
|
+
isCountrySupported: (countryCode) => {
|
|
10
|
+
return ["RO", "ROU", "642"].includes(countryCode.toUpperCase());
|
|
11
|
+
},
|
|
12
|
+
validate: (tin) => {
|
|
13
|
+
if (tin == null) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
tin = (0, validator_util_1.sanitise)(tin, "RO", ROMANIA_INVALID_CHARS);
|
|
17
|
+
if (tin == null || !CNP_REGEX.test(tin)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return isValidCnp(tin);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
function isValidCnp(tin) {
|
|
24
|
+
let sum = 0;
|
|
25
|
+
for (let i = 0; i < 12; i++) {
|
|
26
|
+
sum += Number(tin.charAt(i)) * WEIGHTS[i];
|
|
27
|
+
}
|
|
28
|
+
let checksum = sum % 11;
|
|
29
|
+
if (checksum === 10) {
|
|
30
|
+
checksum = 1;
|
|
31
|
+
}
|
|
32
|
+
return checksum === Number(tin.charAt(12));
|
|
33
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SloveniaTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^[1-9][0-9]{7}$/;
|
|
6
|
+
const WEIGHTS = [2, 4, 8, 5, 10, 9, 7, 3, 6];
|
|
7
|
+
exports.SloveniaTinValidator = {
|
|
8
|
+
isCountrySupported: (countryCode) => {
|
|
9
|
+
return ["SI", "SVN", "705"].includes(countryCode.toUpperCase());
|
|
10
|
+
},
|
|
11
|
+
validate: (tin) => {
|
|
12
|
+
if (tin == null) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
tin = (0, validator_util_1.sanitise)(tin, "SI");
|
|
16
|
+
if (tin == null || !TIN_REGEX.test(tin)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
let sum = 0;
|
|
20
|
+
for (let i = 0; i < 9; i++) {
|
|
21
|
+
sum += (tin.charAt(i).charCodeAt(0) - 48) * WEIGHTS[i];
|
|
22
|
+
}
|
|
23
|
+
const checksum = (sum % 11) % 10;
|
|
24
|
+
const control = tin.charAt(9).charCodeAt(0) - 48;
|
|
25
|
+
return control === checksum;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SpainTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const CIF_REGEX = /^([ABCDEFGHJNPQRSUVW])([0-9]{7})([A-J0-9])$/;
|
|
6
|
+
const NIF_REGEX = /^([0-9KLMXYZ])([0-9]{7,8})([TRWAGMYFPDXBNJZSQVHLCKE])$/;
|
|
7
|
+
const NIF_DIGITS = "TRWAGMYFPDXBNJZSQVHLCKE";
|
|
8
|
+
const CIF_DIGITS = ["0123456789", "JABCDEFGHI"];
|
|
9
|
+
exports.SpainTinValidator = {
|
|
10
|
+
isCountrySupported: (countryCode) => {
|
|
11
|
+
return ["ES", "ESP", "724"].includes(countryCode.toUpperCase());
|
|
12
|
+
},
|
|
13
|
+
validate: (tin) => {
|
|
14
|
+
if (tin == null) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
tin = (0, validator_util_1.sanitise)(tin, "ES");
|
|
18
|
+
if (tin == null || tin.length === 0) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
switch (tin.charAt(0)) {
|
|
22
|
+
case "0":
|
|
23
|
+
case "1":
|
|
24
|
+
case "2":
|
|
25
|
+
case "3":
|
|
26
|
+
case "4":
|
|
27
|
+
case "5":
|
|
28
|
+
case "6":
|
|
29
|
+
case "7":
|
|
30
|
+
case "8":
|
|
31
|
+
case "9":
|
|
32
|
+
case "K":
|
|
33
|
+
case "L":
|
|
34
|
+
case "M":
|
|
35
|
+
case "X":
|
|
36
|
+
case "Y":
|
|
37
|
+
case "Z":
|
|
38
|
+
return isValidNif(tin);
|
|
39
|
+
case "A":
|
|
40
|
+
case "B":
|
|
41
|
+
case "C":
|
|
42
|
+
case "D":
|
|
43
|
+
case "E":
|
|
44
|
+
case "F":
|
|
45
|
+
case "G":
|
|
46
|
+
case "H":
|
|
47
|
+
case "J":
|
|
48
|
+
case "P":
|
|
49
|
+
case "Q":
|
|
50
|
+
case "R":
|
|
51
|
+
case "S":
|
|
52
|
+
case "U":
|
|
53
|
+
case "V":
|
|
54
|
+
case "N":
|
|
55
|
+
case "W":
|
|
56
|
+
return isValidCif(tin);
|
|
57
|
+
default:
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
function isValidCif(nif) {
|
|
63
|
+
const match = nif.match(CIF_REGEX);
|
|
64
|
+
if (match) {
|
|
65
|
+
const num = match[2];
|
|
66
|
+
let sum = 0;
|
|
67
|
+
const len = num.length;
|
|
68
|
+
for (let i = 0; i < len; i++) {
|
|
69
|
+
const tmp = (2 - i % 2) * (Number(num.charAt(i)));
|
|
70
|
+
sum += Math.floor(tmp / 10) + (tmp % 10);
|
|
71
|
+
}
|
|
72
|
+
const index = 10 - (sum % 10);
|
|
73
|
+
const type = match[1].charAt(0);
|
|
74
|
+
const control = match[3].charAt(0);
|
|
75
|
+
switch (type) {
|
|
76
|
+
case "A":
|
|
77
|
+
case "B":
|
|
78
|
+
case "E":
|
|
79
|
+
case "H":
|
|
80
|
+
return control === CIF_DIGITS[0].charAt(index);
|
|
81
|
+
case "K":
|
|
82
|
+
case "P":
|
|
83
|
+
case "Q":
|
|
84
|
+
case "S":
|
|
85
|
+
return control === CIF_DIGITS[1].charAt(index);
|
|
86
|
+
default:
|
|
87
|
+
return control ===
|
|
88
|
+
CIF_DIGITS[control >= "0" && control <= "9" ? 0 : 1].charAt(index);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
function isValidNif(nif) {
|
|
94
|
+
const match = nif.match(NIF_REGEX);
|
|
95
|
+
if (match) {
|
|
96
|
+
const number = parseNifNumber(match[1].charAt(0), match[2]);
|
|
97
|
+
const control = match[3].charAt(0);
|
|
98
|
+
return control === NIF_DIGITS.charAt(number % NIF_DIGITS.length);
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
function parseNifNumber(type, num) {
|
|
103
|
+
let number;
|
|
104
|
+
if (type >= "0" && type <= "9") {
|
|
105
|
+
number = type + num;
|
|
106
|
+
}
|
|
107
|
+
else if (type === "Y") {
|
|
108
|
+
number = "1" + num;
|
|
109
|
+
}
|
|
110
|
+
else if (type === "Z") {
|
|
111
|
+
number = "2" + num;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
number = num;
|
|
115
|
+
}
|
|
116
|
+
return parseInt(number, 10);
|
|
117
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UkTinValidator = void 0;
|
|
4
|
+
const validator_util_1 = require("../validator-util");
|
|
5
|
+
const TIN_REGEX = /^(?:[0-9]{10}|[A-CEF-HJ-PW-Z][A-CEF-HJ-NPW-Z][0-9]{6}[ABCD]?)$/;
|
|
6
|
+
const INVALID_PREFIXES = new Set([
|
|
7
|
+
"GB",
|
|
8
|
+
"NK",
|
|
9
|
+
"TN",
|
|
10
|
+
"ZZ",
|
|
11
|
+
]);
|
|
12
|
+
exports.UkTinValidator = {
|
|
13
|
+
isCountrySupported: (countryCode) => {
|
|
14
|
+
return ["GB", "GBR", "826"].includes(countryCode.toUpperCase());
|
|
15
|
+
},
|
|
16
|
+
validate: (tin) => {
|
|
17
|
+
if (tin == null || tin.trim() === "") {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
// Reject whitespace and special characters.
|
|
21
|
+
if (/[^a-zA-Z0-9]/.test(tin)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
tin = (0, validator_util_1.sanitise)(tin, "GB");
|
|
25
|
+
if (tin == null || !TIN_REGEX.test(tin)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (/^[0-9]{10}$/.test(tin)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
const prefix = tin.substring(0, 2);
|
|
32
|
+
return !INVALID_PREFIXES.has(prefix);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sanitise(tin: string | null, countryCode: string, invalidChars?: RegExp): string | null;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanitise = sanitise;
|
|
4
|
+
const INVALID_CHARS = /[^0-9A-Za-z]+/;
|
|
5
|
+
function sanitise(tin, countryCode, invalidChars = INVALID_CHARS) {
|
|
6
|
+
let sanitised = tin == null ? null : tin.replace(invalidChars, "").toUpperCase();
|
|
7
|
+
if (sanitised != null && countryCode != null && sanitised.startsWith(countryCode)) {
|
|
8
|
+
sanitised = sanitised.substring(2);
|
|
9
|
+
}
|
|
10
|
+
return sanitised;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function validateTin(tin: string, countryCode: string): boolean;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateTin = validateTin;
|
|
4
|
+
const AustriaTinValidator_1 = require("./countries/AustriaTinValidator");
|
|
5
|
+
const BelgiumTinValidator_1 = require("./countries/BelgiumTinValidator");
|
|
6
|
+
const BulgariaTinValidator_1 = require("./countries/BulgariaTinValidator");
|
|
7
|
+
const CanadaTinValidator_1 = require("./countries/CanadaTinValidator");
|
|
8
|
+
const CroatiaTinValidator_1 = require("./countries/CroatiaTinValidator");
|
|
9
|
+
const CyprusTinValidator_1 = require("./countries/CyprusTinValidator");
|
|
10
|
+
const CzechiaTinValidator_1 = require("./countries/CzechiaTinValidator");
|
|
11
|
+
const DenmarkTinValidator_1 = require("./countries/DenmarkTinValidator");
|
|
12
|
+
const EstoniaTinValidator_1 = require("./countries/EstoniaTinValidator");
|
|
13
|
+
const FinlandTinValidator_1 = require("./countries/FinlandTinValidator");
|
|
14
|
+
const FranceTinValidator_1 = require("./countries/FranceTinValidator");
|
|
15
|
+
const GermanyTinValidator_1 = require("./countries/GermanyTinValidator");
|
|
16
|
+
const GreeceTinValidator_1 = require("./countries/GreeceTinValidator");
|
|
17
|
+
const ItalyTinValidator_1 = require("./countries/ItalyTinValidator");
|
|
18
|
+
const PortugalTinValidator_1 = require("./countries/PortugalTinValidator");
|
|
19
|
+
const RomaniaTinValidator_1 = require("./countries/RomaniaTinValidator");
|
|
20
|
+
const SloveniaTinValidator_1 = require("./countries/SloveniaTinValidator");
|
|
21
|
+
const SpainTinValidator_1 = require("./countries/SpainTinValidator");
|
|
22
|
+
const UkTinValidator_1 = require("./countries/UkTinValidator");
|
|
23
|
+
const COUNTRIES = [
|
|
24
|
+
AustriaTinValidator_1.AustriaTinValidator,
|
|
25
|
+
BelgiumTinValidator_1.BelgiumTinValidator,
|
|
26
|
+
BulgariaTinValidator_1.BulgariaTinValidator,
|
|
27
|
+
CanadaTinValidator_1.CanadaTinValidator,
|
|
28
|
+
CroatiaTinValidator_1.CroatiaTinValidator,
|
|
29
|
+
CyprusTinValidator_1.CyprusTinValidator,
|
|
30
|
+
CzechiaTinValidator_1.CzechiaTinValidator,
|
|
31
|
+
DenmarkTinValidator_1.DenmarkTinValidator,
|
|
32
|
+
EstoniaTinValidator_1.EstoniaTinValidator,
|
|
33
|
+
FinlandTinValidator_1.FinlandTinValidator,
|
|
34
|
+
FranceTinValidator_1.FranceTinValidator,
|
|
35
|
+
GermanyTinValidator_1.GermanyTinValidator,
|
|
36
|
+
GreeceTinValidator_1.GreeceTinValidator,
|
|
37
|
+
ItalyTinValidator_1.ItalyTinValidator,
|
|
38
|
+
PortugalTinValidator_1.PortugalTinValidator,
|
|
39
|
+
RomaniaTinValidator_1.RomaniaTinValidator,
|
|
40
|
+
SloveniaTinValidator_1.SloveniaTinValidator,
|
|
41
|
+
SpainTinValidator_1.SpainTinValidator,
|
|
42
|
+
UkTinValidator_1.UkTinValidator
|
|
43
|
+
];
|
|
44
|
+
function validateTin(tin, countryCode) {
|
|
45
|
+
const countryValidator = COUNTRIES.find(validator => validator.isCountrySupported(countryCode));
|
|
46
|
+
if (!countryValidator) {
|
|
47
|
+
throw new Error(`No validator found for country code: ${countryCode}`);
|
|
48
|
+
}
|
|
49
|
+
return countryValidator.validate(tin);
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@captinbv/tin-validator",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "TIN validator javascript",
|
|
5
|
+
"author": "Captin BV",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"test": "cross-env TZ=UTC jest",
|
|
17
|
+
"publish": "npm publish --access public"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/jest": "^30.0.0",
|
|
21
|
+
"cross-env": "^10.1.0",
|
|
22
|
+
"jest": "^30.2.0",
|
|
23
|
+
"jest-junit": "^16.0.0",
|
|
24
|
+
"ts-jest": "^29.4.6",
|
|
25
|
+
"ts-node": "^10.9.2",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
}
|
|
6
28
|
}
|