sl-nic-validator-and-information-extractor 1.0.3
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 +21 -0
- package/README.md +79 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +158 -0
- package/dist/index.mjs +130 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jayaraj Rohan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Sri Lankan National Identity Card (NIC) Validator and Information Extractor
|
|
2
|
+
|
|
3
|
+
This package provides functions for validating Sri Lankan National Identity Card (NIC) numbers and extracting details such as date of birth, age, and gender.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package using npm:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install sl-nic-validator-and-information-extractor
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
or yarn:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
yarn add sl-nic-validator-and-information-extractor
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
1. Validating NIC Numbers:
|
|
22
|
+
|
|
23
|
+
The isValidSlNic function validates the format and structure of a given NIC number.
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import { isValidSlNic } from "sl-nic-validator";
|
|
27
|
+
|
|
28
|
+
const nic = "971902662V"; // Example NIC
|
|
29
|
+
const isValid = isValidSlNic(nic);
|
|
30
|
+
|
|
31
|
+
if (isValid) {
|
|
32
|
+
console.log("Valid NIC!");
|
|
33
|
+
} else {
|
|
34
|
+
console.log("Invalid NIC.");
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
2. Extracting Details:
|
|
39
|
+
|
|
40
|
+
The extractDetailsFromSlNic function takes a valid NIC number and returns an object containing the extracted information:
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
import { extractDetailsFromSlNic } from "sl-nic-validator";
|
|
44
|
+
|
|
45
|
+
const nic = "971902662V"; // Example NIC
|
|
46
|
+
const nicDetails = extractDetailsFromSlNic(nic);
|
|
47
|
+
|
|
48
|
+
if (nicDetails) {
|
|
49
|
+
console.log(nicDetails);
|
|
50
|
+
// Output:
|
|
51
|
+
// { dateOfBirth: '1997-07-08', age: 26, gender: 'Male' }
|
|
52
|
+
} else {
|
|
53
|
+
console.log("Invalid NIC, cannot extract details.");
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The returned object (nicDetails) has the following properties:
|
|
58
|
+
|
|
59
|
+
- dateOfBirth: A string in the format YYYY-MM-DD representing the date of birth extracted from the NIC.
|
|
60
|
+
- age: An integer representing the calculated age based on the date of birth and the current date.
|
|
61
|
+
- gender: A string indicating "Male" or "Female" based on the NIC encoding.
|
|
62
|
+
|
|
63
|
+
## Functionality
|
|
64
|
+
|
|
65
|
+
- Validates both 10-digit and 12-digit Sri Lankan NICs.
|
|
66
|
+
- Extracts date of birth, age, and gender from valid NICs.
|
|
67
|
+
- Returns undefined for invalid NICs.
|
|
68
|
+
|
|
69
|
+
## Contributing
|
|
70
|
+
|
|
71
|
+
Contributions are welcome! If you find any bugs or want to add new features, please open an issue or submit a pull request on the [GitHub repository](https://github.com/jayarajrohan/sl-nic-validator-and-information-extractor).
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
76
|
+
|
|
77
|
+
## Contact
|
|
78
|
+
|
|
79
|
+
For questions or feedback, you can reach out to the project maintainer at rohanonfirealways@gmail.com.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface INicDetails {
|
|
2
|
+
dateOfBirth: string;
|
|
3
|
+
age: number;
|
|
4
|
+
gender: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare function isValidSlNic(nic: string): boolean | undefined;
|
|
8
|
+
declare function extractDetailsFromSlNic(nic: string): INicDetails | undefined;
|
|
9
|
+
|
|
10
|
+
export { type INicDetails, extractDetailsFromSlNic, isValidSlNic };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface INicDetails {
|
|
2
|
+
dateOfBirth: string;
|
|
3
|
+
age: number;
|
|
4
|
+
gender: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare function isValidSlNic(nic: string): boolean | undefined;
|
|
8
|
+
declare function extractDetailsFromSlNic(nic: string): INicDetails | undefined;
|
|
9
|
+
|
|
10
|
+
export { type INicDetails, extractDetailsFromSlNic, isValidSlNic };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
extractDetailsFromSlNic: () => extractDetailsFromSlNic,
|
|
24
|
+
isValidSlNic: () => isValidSlNic
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/functions.ts
|
|
29
|
+
function isValidSlNic(nic) {
|
|
30
|
+
const length = nic.length;
|
|
31
|
+
const firstDigit = nic.charAt(0);
|
|
32
|
+
const secondDigit = nic.charAt(1);
|
|
33
|
+
const thirdDigit = nic.charAt(2);
|
|
34
|
+
const fourthDigit = nic.charAt(3);
|
|
35
|
+
const fifthDigit = nic.charAt(4);
|
|
36
|
+
const sixthDigit = nic.charAt(5);
|
|
37
|
+
const seventhDigit = nic.charAt(6);
|
|
38
|
+
const tenthDigit = nic.charAt(9);
|
|
39
|
+
if (length !== 10 && length !== 12) {
|
|
40
|
+
return false;
|
|
41
|
+
} else if (length === 10) {
|
|
42
|
+
if (firstDigit !== "2" && firstDigit !== "3" && firstDigit !== "4" && firstDigit !== "5" && firstDigit !== "6" && firstDigit !== "7" && firstDigit !== "8" && firstDigit !== "9") {
|
|
43
|
+
return false;
|
|
44
|
+
} else if (thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "4" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8" && thirdDigit !== "9" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "6" && fourthDigit !== "7" && fourthDigit !== "8" && fourthDigit !== "9" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && fifthDigit !== "9") {
|
|
45
|
+
return false;
|
|
46
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8") {
|
|
47
|
+
return false;
|
|
48
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && fourthDigit !== "0" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "6") {
|
|
49
|
+
return false;
|
|
50
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && fourthDigit !== "0" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "7" && fourthDigit !== "8" && fourthDigit !== "9" && fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6") {
|
|
51
|
+
return false;
|
|
52
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "6" && fourthDigit !== "7" && fourthDigit !== "8" && fourthDigit !== "9" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && fifthDigit !== "9") {
|
|
53
|
+
return false;
|
|
54
|
+
} else if (tenthDigit !== "x" && tenthDigit !== "X" && tenthDigit !== "v" && tenthDigit !== "V") {
|
|
55
|
+
return false;
|
|
56
|
+
} else {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
} else if (length === 12) {
|
|
60
|
+
if (firstDigit !== "1" && firstDigit !== "2") {
|
|
61
|
+
return false;
|
|
62
|
+
} else if (firstDigit === "1" && secondDigit !== "9") {
|
|
63
|
+
return false;
|
|
64
|
+
} else if (firstDigit === "2" && secondDigit !== "0") {
|
|
65
|
+
return false;
|
|
66
|
+
} else if (firstDigit !== "2" && secondDigit !== "0" && secondDigit !== "1" && secondDigit !== "2" && secondDigit !== "3" && secondDigit !== "4" && secondDigit !== "5" && secondDigit !== "6" && secondDigit !== "7" && secondDigit !== "8" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "4" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8" && thirdDigit !== "9") {
|
|
67
|
+
return false;
|
|
68
|
+
} else if (firstDigit !== "1" && secondDigit !== "1" && secondDigit !== "2" && secondDigit !== "3" && secondDigit !== "4" && secondDigit !== "5" && secondDigit !== "6" && secondDigit !== "7" && secondDigit !== "8" && secondDigit !== "9" && thirdDigit !== "0") {
|
|
69
|
+
return false;
|
|
70
|
+
} else if (fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && fifthDigit !== "9" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "6" && sixthDigit !== "7" && sixthDigit !== "8" && sixthDigit !== "9" && seventhDigit !== "1" && seventhDigit !== "2" && seventhDigit !== "3" && seventhDigit !== "4" && seventhDigit !== "5" && seventhDigit !== "6" && seventhDigit !== "7" && seventhDigit !== "8" && seventhDigit !== "9") {
|
|
71
|
+
return false;
|
|
72
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8") {
|
|
73
|
+
return false;
|
|
74
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && sixthDigit !== "0" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "6") {
|
|
75
|
+
return false;
|
|
76
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && sixthDigit !== "0" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "7" && sixthDigit !== "8" && sixthDigit !== "9" && seventhDigit !== "0" && seventhDigit !== "1" && seventhDigit !== "2" && seventhDigit !== "3" && seventhDigit !== "4" && seventhDigit !== "5" && seventhDigit !== "6") {
|
|
77
|
+
return false;
|
|
78
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "6" && sixthDigit !== "7" && sixthDigit !== "8" && sixthDigit !== "9" && seventhDigit !== "1" && seventhDigit !== "2" && seventhDigit !== "3" && seventhDigit !== "4" && seventhDigit !== "5" && seventhDigit !== "6" && seventhDigit !== "7" && seventhDigit !== "8" && seventhDigit !== "9") {
|
|
79
|
+
return false;
|
|
80
|
+
} else {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function extractDetailsFromSlNic(nic) {
|
|
86
|
+
let year;
|
|
87
|
+
let genderValue;
|
|
88
|
+
let monthAndDayInDays;
|
|
89
|
+
if (!isValidSlNic(nic)) {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
if (nic.length === 10) {
|
|
93
|
+
genderValue = +nic.slice(2, 5);
|
|
94
|
+
monthAndDayInDays = genderValue >= 500 ? genderValue - 500 : genderValue;
|
|
95
|
+
year = `19${nic.slice(0, 2)}`;
|
|
96
|
+
} else {
|
|
97
|
+
genderValue = +nic.slice(4, 7);
|
|
98
|
+
monthAndDayInDays = genderValue >= 500 ? genderValue - 500 : genderValue;
|
|
99
|
+
year = `${nic.slice(0, 4)}`;
|
|
100
|
+
}
|
|
101
|
+
let day;
|
|
102
|
+
let month;
|
|
103
|
+
if (monthAndDayInDays <= 31) {
|
|
104
|
+
month = "01";
|
|
105
|
+
day = monthAndDayInDays;
|
|
106
|
+
} else if (monthAndDayInDays <= 60) {
|
|
107
|
+
month = "02";
|
|
108
|
+
day = monthAndDayInDays - 31;
|
|
109
|
+
} else if (monthAndDayInDays <= 91) {
|
|
110
|
+
month = "03";
|
|
111
|
+
day = monthAndDayInDays - 60;
|
|
112
|
+
} else if (monthAndDayInDays <= 121) {
|
|
113
|
+
month = "04";
|
|
114
|
+
day = monthAndDayInDays - 91;
|
|
115
|
+
} else if (monthAndDayInDays <= 152) {
|
|
116
|
+
month = "05";
|
|
117
|
+
day = monthAndDayInDays - 121;
|
|
118
|
+
} else if (monthAndDayInDays <= 182) {
|
|
119
|
+
month = "06";
|
|
120
|
+
day = monthAndDayInDays - 152;
|
|
121
|
+
} else if (monthAndDayInDays <= 213) {
|
|
122
|
+
month = "07";
|
|
123
|
+
day = monthAndDayInDays - 182;
|
|
124
|
+
} else if (monthAndDayInDays <= 244) {
|
|
125
|
+
month = "08";
|
|
126
|
+
day = monthAndDayInDays - 213;
|
|
127
|
+
} else if (monthAndDayInDays <= 274) {
|
|
128
|
+
month = "09";
|
|
129
|
+
day = monthAndDayInDays - 244;
|
|
130
|
+
} else if (monthAndDayInDays <= 305) {
|
|
131
|
+
month = "10";
|
|
132
|
+
day = monthAndDayInDays - 274;
|
|
133
|
+
} else if (monthAndDayInDays <= 335) {
|
|
134
|
+
month = "11";
|
|
135
|
+
day = monthAndDayInDays - 305;
|
|
136
|
+
} else {
|
|
137
|
+
month = "12";
|
|
138
|
+
day = monthAndDayInDays - 335;
|
|
139
|
+
}
|
|
140
|
+
const dateOfBirth = `${year.toString()}-${month}-${day < 10 ? `0${day}` : day}`;
|
|
141
|
+
const today = /* @__PURE__ */ new Date();
|
|
142
|
+
const birthDate = new Date(dateOfBirth);
|
|
143
|
+
let age = today.getFullYear() - birthDate.getFullYear();
|
|
144
|
+
const monthDiff = today.getMonth() - birthDate.getMonth();
|
|
145
|
+
if (monthDiff < 0 || monthDiff === 0 && today.getDate() < birthDate.getDate()) {
|
|
146
|
+
age--;
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
dateOfBirth,
|
|
150
|
+
age,
|
|
151
|
+
gender: genderValue < 500 ? "Male" : "Female"
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
155
|
+
0 && (module.exports = {
|
|
156
|
+
extractDetailsFromSlNic,
|
|
157
|
+
isValidSlNic
|
|
158
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// src/functions.ts
|
|
2
|
+
function isValidSlNic(nic) {
|
|
3
|
+
const length = nic.length;
|
|
4
|
+
const firstDigit = nic.charAt(0);
|
|
5
|
+
const secondDigit = nic.charAt(1);
|
|
6
|
+
const thirdDigit = nic.charAt(2);
|
|
7
|
+
const fourthDigit = nic.charAt(3);
|
|
8
|
+
const fifthDigit = nic.charAt(4);
|
|
9
|
+
const sixthDigit = nic.charAt(5);
|
|
10
|
+
const seventhDigit = nic.charAt(6);
|
|
11
|
+
const tenthDigit = nic.charAt(9);
|
|
12
|
+
if (length !== 10 && length !== 12) {
|
|
13
|
+
return false;
|
|
14
|
+
} else if (length === 10) {
|
|
15
|
+
if (firstDigit !== "2" && firstDigit !== "3" && firstDigit !== "4" && firstDigit !== "5" && firstDigit !== "6" && firstDigit !== "7" && firstDigit !== "8" && firstDigit !== "9") {
|
|
16
|
+
return false;
|
|
17
|
+
} else if (thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "4" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8" && thirdDigit !== "9" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "6" && fourthDigit !== "7" && fourthDigit !== "8" && fourthDigit !== "9" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && fifthDigit !== "9") {
|
|
18
|
+
return false;
|
|
19
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8") {
|
|
20
|
+
return false;
|
|
21
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && fourthDigit !== "0" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "6") {
|
|
22
|
+
return false;
|
|
23
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && fourthDigit !== "0" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "7" && fourthDigit !== "8" && fourthDigit !== "9" && fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6") {
|
|
24
|
+
return false;
|
|
25
|
+
} else if (thirdDigit !== "0" && thirdDigit !== "1" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8" && fourthDigit !== "1" && fourthDigit !== "2" && fourthDigit !== "3" && fourthDigit !== "4" && fourthDigit !== "5" && fourthDigit !== "6" && fourthDigit !== "7" && fourthDigit !== "8" && fourthDigit !== "9" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && fifthDigit !== "9") {
|
|
26
|
+
return false;
|
|
27
|
+
} else if (tenthDigit !== "x" && tenthDigit !== "X" && tenthDigit !== "v" && tenthDigit !== "V") {
|
|
28
|
+
return false;
|
|
29
|
+
} else {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
} else if (length === 12) {
|
|
33
|
+
if (firstDigit !== "1" && firstDigit !== "2") {
|
|
34
|
+
return false;
|
|
35
|
+
} else if (firstDigit === "1" && secondDigit !== "9") {
|
|
36
|
+
return false;
|
|
37
|
+
} else if (firstDigit === "2" && secondDigit !== "0") {
|
|
38
|
+
return false;
|
|
39
|
+
} else if (firstDigit !== "2" && secondDigit !== "0" && secondDigit !== "1" && secondDigit !== "2" && secondDigit !== "3" && secondDigit !== "4" && secondDigit !== "5" && secondDigit !== "6" && secondDigit !== "7" && secondDigit !== "8" && thirdDigit !== "2" && thirdDigit !== "3" && thirdDigit !== "4" && thirdDigit !== "5" && thirdDigit !== "6" && thirdDigit !== "7" && thirdDigit !== "8" && thirdDigit !== "9") {
|
|
40
|
+
return false;
|
|
41
|
+
} else if (firstDigit !== "1" && secondDigit !== "1" && secondDigit !== "2" && secondDigit !== "3" && secondDigit !== "4" && secondDigit !== "5" && secondDigit !== "6" && secondDigit !== "7" && secondDigit !== "8" && secondDigit !== "9" && thirdDigit !== "0") {
|
|
42
|
+
return false;
|
|
43
|
+
} else if (fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "4" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && fifthDigit !== "9" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "6" && sixthDigit !== "7" && sixthDigit !== "8" && sixthDigit !== "9" && seventhDigit !== "1" && seventhDigit !== "2" && seventhDigit !== "3" && seventhDigit !== "4" && seventhDigit !== "5" && seventhDigit !== "6" && seventhDigit !== "7" && seventhDigit !== "8" && seventhDigit !== "9") {
|
|
44
|
+
return false;
|
|
45
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8") {
|
|
46
|
+
return false;
|
|
47
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && sixthDigit !== "0" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "6") {
|
|
48
|
+
return false;
|
|
49
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "5" && fifthDigit !== "6" && fifthDigit !== "7" && sixthDigit !== "0" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "7" && sixthDigit !== "8" && sixthDigit !== "9" && seventhDigit !== "0" && seventhDigit !== "1" && seventhDigit !== "2" && seventhDigit !== "3" && seventhDigit !== "4" && seventhDigit !== "5" && seventhDigit !== "6") {
|
|
50
|
+
return false;
|
|
51
|
+
} else if (fifthDigit !== "0" && fifthDigit !== "1" && fifthDigit !== "2" && fifthDigit !== "3" && fifthDigit !== "6" && fifthDigit !== "7" && fifthDigit !== "8" && sixthDigit !== "1" && sixthDigit !== "2" && sixthDigit !== "3" && sixthDigit !== "4" && sixthDigit !== "5" && sixthDigit !== "6" && sixthDigit !== "7" && sixthDigit !== "8" && sixthDigit !== "9" && seventhDigit !== "1" && seventhDigit !== "2" && seventhDigit !== "3" && seventhDigit !== "4" && seventhDigit !== "5" && seventhDigit !== "6" && seventhDigit !== "7" && seventhDigit !== "8" && seventhDigit !== "9") {
|
|
52
|
+
return false;
|
|
53
|
+
} else {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function extractDetailsFromSlNic(nic) {
|
|
59
|
+
let year;
|
|
60
|
+
let genderValue;
|
|
61
|
+
let monthAndDayInDays;
|
|
62
|
+
if (!isValidSlNic(nic)) {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
if (nic.length === 10) {
|
|
66
|
+
genderValue = +nic.slice(2, 5);
|
|
67
|
+
monthAndDayInDays = genderValue >= 500 ? genderValue - 500 : genderValue;
|
|
68
|
+
year = `19${nic.slice(0, 2)}`;
|
|
69
|
+
} else {
|
|
70
|
+
genderValue = +nic.slice(4, 7);
|
|
71
|
+
monthAndDayInDays = genderValue >= 500 ? genderValue - 500 : genderValue;
|
|
72
|
+
year = `${nic.slice(0, 4)}`;
|
|
73
|
+
}
|
|
74
|
+
let day;
|
|
75
|
+
let month;
|
|
76
|
+
if (monthAndDayInDays <= 31) {
|
|
77
|
+
month = "01";
|
|
78
|
+
day = monthAndDayInDays;
|
|
79
|
+
} else if (monthAndDayInDays <= 60) {
|
|
80
|
+
month = "02";
|
|
81
|
+
day = monthAndDayInDays - 31;
|
|
82
|
+
} else if (monthAndDayInDays <= 91) {
|
|
83
|
+
month = "03";
|
|
84
|
+
day = monthAndDayInDays - 60;
|
|
85
|
+
} else if (monthAndDayInDays <= 121) {
|
|
86
|
+
month = "04";
|
|
87
|
+
day = monthAndDayInDays - 91;
|
|
88
|
+
} else if (monthAndDayInDays <= 152) {
|
|
89
|
+
month = "05";
|
|
90
|
+
day = monthAndDayInDays - 121;
|
|
91
|
+
} else if (monthAndDayInDays <= 182) {
|
|
92
|
+
month = "06";
|
|
93
|
+
day = monthAndDayInDays - 152;
|
|
94
|
+
} else if (monthAndDayInDays <= 213) {
|
|
95
|
+
month = "07";
|
|
96
|
+
day = monthAndDayInDays - 182;
|
|
97
|
+
} else if (monthAndDayInDays <= 244) {
|
|
98
|
+
month = "08";
|
|
99
|
+
day = monthAndDayInDays - 213;
|
|
100
|
+
} else if (monthAndDayInDays <= 274) {
|
|
101
|
+
month = "09";
|
|
102
|
+
day = monthAndDayInDays - 244;
|
|
103
|
+
} else if (monthAndDayInDays <= 305) {
|
|
104
|
+
month = "10";
|
|
105
|
+
day = monthAndDayInDays - 274;
|
|
106
|
+
} else if (monthAndDayInDays <= 335) {
|
|
107
|
+
month = "11";
|
|
108
|
+
day = monthAndDayInDays - 305;
|
|
109
|
+
} else {
|
|
110
|
+
month = "12";
|
|
111
|
+
day = monthAndDayInDays - 335;
|
|
112
|
+
}
|
|
113
|
+
const dateOfBirth = `${year.toString()}-${month}-${day < 10 ? `0${day}` : day}`;
|
|
114
|
+
const today = /* @__PURE__ */ new Date();
|
|
115
|
+
const birthDate = new Date(dateOfBirth);
|
|
116
|
+
let age = today.getFullYear() - birthDate.getFullYear();
|
|
117
|
+
const monthDiff = today.getMonth() - birthDate.getMonth();
|
|
118
|
+
if (monthDiff < 0 || monthDiff === 0 && today.getDate() < birthDate.getDate()) {
|
|
119
|
+
age--;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
dateOfBirth,
|
|
123
|
+
age,
|
|
124
|
+
gender: genderValue < 500 ? "Male" : "Female"
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
export {
|
|
128
|
+
extractDetailsFromSlNic,
|
|
129
|
+
isValidSlNic
|
|
130
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sl-nic-validator-and-information-extractor",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "A function to validate new and old NIC of Sri Lanka and a function to extract information from the NIC",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/jayarajrohan/sl-nic-validator-and-information-extractor.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"SL NIC Validation",
|
|
20
|
+
"Validation",
|
|
21
|
+
"SL NIC Information Extractor",
|
|
22
|
+
"SL NIC Information",
|
|
23
|
+
"NIC"
|
|
24
|
+
],
|
|
25
|
+
"author": "rohan_jayaraj",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/jayarajrohan/sl-nic-validator-and-information-extractor/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/jayarajrohan/sl-nic-validator-and-information-extractor#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"typescript": "^5.4.2",
|
|
33
|
+
"tsup": "^8.0.2"
|
|
34
|
+
}
|
|
35
|
+
}
|