@indodev/toolkit 0.4.1 → 0.4.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/dist/index.cjs +40 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +40 -33
- package/dist/index.js.map +1 -1
- package/dist/nik/index.cjs +51 -33
- package/dist/nik/index.cjs.map +1 -1
- package/dist/nik/index.d.cts +23 -1
- package/dist/nik/index.d.ts +23 -1
- package/dist/nik/index.js +51 -34
- package/dist/nik/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -57,53 +57,66 @@ var REGENCIES = {
|
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
// src/nik/
|
|
61
|
-
function
|
|
62
|
-
if (
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
const provinceCode = nik.substring(0, 2);
|
|
66
|
-
if (!PROVINCES[provinceCode]) {
|
|
67
|
-
return false;
|
|
60
|
+
// src/nik/utils/date.ts
|
|
61
|
+
function parseNIKDate(nik) {
|
|
62
|
+
if (nik.length !== 16) {
|
|
63
|
+
return null;
|
|
68
64
|
}
|
|
69
65
|
const yearStr = nik.substring(6, 8);
|
|
70
66
|
const monthStr = nik.substring(8, 10);
|
|
71
|
-
const
|
|
67
|
+
const dayEncodedStr = nik.substring(10, 12);
|
|
72
68
|
const year = parseInt(yearStr, 10);
|
|
69
|
+
if (isNaN(year)) return null;
|
|
73
70
|
const fullYear = year > 30 ? 1900 + year : 2e3 + year;
|
|
74
71
|
const month = parseInt(monthStr, 10);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
if (isNaN(month)) return null;
|
|
73
|
+
const dayEncoded = parseInt(dayEncodedStr, 10);
|
|
74
|
+
if (isNaN(dayEncoded)) return null;
|
|
75
|
+
const gender = dayEncoded > 40 ? "female" : "male";
|
|
76
|
+
const day = dayEncoded > 40 ? dayEncoded - 40 : dayEncoded;
|
|
77
|
+
return { year, fullYear, month, day, gender, dayEncoded };
|
|
78
|
+
}
|
|
79
|
+
function validateNIKDateComponents(year, month, day) {
|
|
80
|
+
if (month < 1 || month > 12) return false;
|
|
81
|
+
if (day < 1 || day > 31) return false;
|
|
82
|
+
const testDate = new Date(year, month - 1, day);
|
|
83
|
+
return testDate.getFullYear() === year && testDate.getMonth() === month - 1 && testDate.getDate() === day;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/nik/validate.ts
|
|
87
|
+
var NIK_PATTERN = /^\d{16}$/;
|
|
88
|
+
function validateNIK(nik) {
|
|
89
|
+
if (!NIK_PATTERN.test(nik)) {
|
|
90
|
+
return false;
|
|
78
91
|
}
|
|
79
|
-
|
|
92
|
+
const provinceCode = nik.substring(0, 2);
|
|
93
|
+
if (!PROVINCES[provinceCode]) {
|
|
80
94
|
return false;
|
|
81
95
|
}
|
|
82
|
-
|
|
96
|
+
const parsed = parseNIKDate(nik);
|
|
97
|
+
if (!parsed) {
|
|
83
98
|
return false;
|
|
84
99
|
}
|
|
85
|
-
const
|
|
86
|
-
if (
|
|
100
|
+
const { fullYear, month, day } = parsed;
|
|
101
|
+
if (!validateNIKDateComponents(fullYear, month, day)) {
|
|
87
102
|
return false;
|
|
88
103
|
}
|
|
89
104
|
const now = /* @__PURE__ */ new Date();
|
|
90
|
-
if (
|
|
105
|
+
if (new Date(fullYear, month - 1, day) > now || fullYear < 1900) {
|
|
91
106
|
return false;
|
|
92
107
|
}
|
|
93
108
|
return true;
|
|
94
109
|
}
|
|
95
110
|
|
|
96
111
|
// src/nik/parse.ts
|
|
112
|
+
var NIK_PATTERN2 = /^\d{16}$/;
|
|
97
113
|
function parseNIK(nik) {
|
|
98
|
-
if (
|
|
114
|
+
if (!NIK_PATTERN2.test(nik)) {
|
|
99
115
|
return null;
|
|
100
116
|
}
|
|
101
117
|
const provinceCode = nik.substring(0, 2);
|
|
102
118
|
const regencyCode = nik.substring(2, 4);
|
|
103
119
|
const districtCode = nik.substring(4, 6);
|
|
104
|
-
const yearStr = nik.substring(6, 8);
|
|
105
|
-
const monthStr = nik.substring(8, 10);
|
|
106
|
-
const dayStr = nik.substring(10, 12);
|
|
107
120
|
const serialNumber = nik.substring(12, 16);
|
|
108
121
|
const province = PROVINCES[provinceCode];
|
|
109
122
|
if (!province) {
|
|
@@ -111,21 +124,15 @@ function parseNIK(nik) {
|
|
|
111
124
|
}
|
|
112
125
|
const regencies = REGENCIES[provinceCode] || {};
|
|
113
126
|
const regency = regencies[regencyCode] || "Unknown";
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
let gender = null;
|
|
118
|
-
if (day > 40) {
|
|
119
|
-
gender = "female";
|
|
120
|
-
day -= 40;
|
|
121
|
-
} else {
|
|
122
|
-
gender = "male";
|
|
127
|
+
const parsed = parseNIKDate(nik);
|
|
128
|
+
if (!parsed) {
|
|
129
|
+
return null;
|
|
123
130
|
}
|
|
124
|
-
const fullYear
|
|
125
|
-
|
|
126
|
-
if (birthDate.getFullYear() !== fullYear || birthDate.getMonth() !== month - 1 || birthDate.getDate() !== day) {
|
|
131
|
+
const { fullYear, month, day, gender } = parsed;
|
|
132
|
+
if (!validateNIKDateComponents(fullYear, month, day)) {
|
|
127
133
|
return null;
|
|
128
134
|
}
|
|
135
|
+
const birthDate = new Date(fullYear, month - 1, day);
|
|
129
136
|
return {
|
|
130
137
|
province: {
|
|
131
138
|
code: provinceCode,
|