identification-validator 3.0.4 → 3.0.6
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/README.md +13 -0
- package/dist/index.cjs +74 -57
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +74 -57
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -36,6 +36,13 @@ console.log(Validator.isValidFIN("F1234567N")) // will return true
|
|
|
36
36
|
|
|
37
37
|
// validation of FIN / NRIC
|
|
38
38
|
console.log(Validator.isValidId("F1234567N")) // will return true
|
|
39
|
+
|
|
40
|
+
// check whether a value has the expected NRIC / FIN format (does not verify its checksum)
|
|
41
|
+
console.log(Validator.isLikelyId("F1234567N")) // will return true
|
|
42
|
+
|
|
43
|
+
// check whether a value has the expected NRIC or FIN format, respectively
|
|
44
|
+
console.log(Validator.isLikelyNRIC("S1234567D")) // will return true
|
|
45
|
+
console.log(Validator.isLikelyFIN("F1234567N")) // will return true
|
|
39
46
|
```
|
|
40
47
|
|
|
41
48
|
#### Generate a random NRIC / FIN
|
|
@@ -49,6 +56,8 @@ console.log(Validator.generateNRIC()); // give a random NRIC, with a random star
|
|
|
49
56
|
// generate a random FIN
|
|
50
57
|
console.log(Validator.generateFIN()) // give a random FIN, with a random starting valid series
|
|
51
58
|
|
|
59
|
+
console.log(Validator.generateId()) // give a random ID (NRIC or FIN), with a random starting valid series
|
|
60
|
+
|
|
52
61
|
```
|
|
53
62
|
|
|
54
63
|
---
|
|
@@ -60,8 +69,12 @@ console.log(Validator.generateFIN()) // give a random FIN, with a random startin
|
|
|
60
69
|
| isValidNRIC | `string` | `Validator.isValidNRIC("S1234567D")` | Checks whether the given string is a valid NRIC and return a boolean response|
|
|
61
70
|
| isValidFIN | `string` | `Validator.isValidFIN("F1234567N")` |Checks whether the given string is a valid NRIC and return a boolean response |
|
|
62
71
|
| isValidId | `string` | `Validator.isValidId("F1234567N")` |Checks whether the given string is a valid NRIC/ FIN and return a boolean response |
|
|
72
|
+
| isLikelyId | `string` | `Validator.isLikelyId("F1234567N")` |Checks whether the string has the expected NRIC / FIN format and prefix; does not verify the checksum |
|
|
73
|
+
| isLikelyNRIC | `string` | `Validator.isLikelyNRIC("S1234567D")` |Checks whether the string has the expected NRIC format and prefix; does not verify the checksum |
|
|
74
|
+
| isLikelyFIN | `string` | `Validator.isLikelyFIN("F1234567N")` |Checks whether the string has the expected FIN format and prefix; does not verify the checksum |
|
|
63
75
|
| generateNRIC | - | `Validator.generateNRIC()` |Generates a random NRIC and returns as a string |
|
|
64
76
|
| generateFIN | - | `Validator.generateFIN()` |Generates a random FIN and returns as a string |
|
|
77
|
+
| generateId | - | `Validator.generateId()` |Generates a random NRIC or FIN and returns as a string
|
|
65
78
|
|
|
66
79
|
|
|
67
80
|
---
|
package/dist/index.cjs
CHANGED
|
@@ -42,37 +42,37 @@ var o = (n2, s2, c2) => {
|
|
|
42
42
|
if (!r(s2)) return false;
|
|
43
43
|
if (Array.isArray(n2)) {
|
|
44
44
|
if (!Array.isArray(s2)) return false;
|
|
45
|
-
let t2 = [], r2 = [],
|
|
45
|
+
let t2 = [], r2 = [], u = [];
|
|
46
46
|
for (const o2 of n2.keys()) {
|
|
47
47
|
const s3 = n2[o2];
|
|
48
|
-
i(s3) && s3[e] ?
|
|
48
|
+
i(s3) && s3[e] ? u.push(s3) : u.length ? r2.push(s3) : t2.push(s3);
|
|
49
49
|
}
|
|
50
|
-
if (
|
|
51
|
-
if (
|
|
50
|
+
if (u.length) {
|
|
51
|
+
if (u.length > 1) throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");
|
|
52
52
|
if (s2.length < t2.length + r2.length) return false;
|
|
53
53
|
const e2 = s2.slice(0, t2.length), n3 = 0 === r2.length ? [] : s2.slice(-r2.length), i2 = s2.slice(t2.length, 0 === r2.length ? Infinity : -r2.length);
|
|
54
|
-
return t2.every((t3, n4) => o(t3, e2[n4], c2)) && r2.every((t3, e3) => o(t3, n3[e3], c2)) && (0 ===
|
|
54
|
+
return t2.every((t3, n4) => o(t3, e2[n4], c2)) && r2.every((t3, e3) => o(t3, n3[e3], c2)) && (0 === u.length || o(u[0], i2, c2));
|
|
55
55
|
}
|
|
56
56
|
return n2.length === s2.length && n2.every((t3, e2) => o(t3, s2[e2], c2));
|
|
57
57
|
}
|
|
58
58
|
return Reflect.ownKeys(n2).every((e2) => {
|
|
59
59
|
const r2 = n2[e2];
|
|
60
|
-
return (e2 in s2 || i(
|
|
61
|
-
var
|
|
60
|
+
return (e2 in s2 || i(u = r2) && "optional" === u[t]().matcherType) && o(r2, s2[e2], c2);
|
|
61
|
+
var u;
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
return Object.is(s2, n2);
|
|
65
65
|
};
|
|
66
66
|
var s = (e2) => {
|
|
67
|
-
var n2, o2,
|
|
68
|
-
return r(e2) ? i(e2) ? null != (n2 = null == (o2 = (
|
|
67
|
+
var n2, o2, u;
|
|
68
|
+
return r(e2) ? i(e2) ? null != (n2 = null == (o2 = (u = e2[t]()).getSelectionKeys) ? void 0 : o2.call(u)) ? n2 : [] : Array.isArray(e2) ? c(e2, s) : c(Object.values(e2), s) : [];
|
|
69
69
|
};
|
|
70
70
|
var c = (t2, e2) => t2.reduce((t3, n2) => t3.concat(e2(n2)), []);
|
|
71
|
-
function
|
|
72
|
-
return Object.assign(t2, { optional: () => h(t2), and: (e2) =>
|
|
71
|
+
function a(t2) {
|
|
72
|
+
return Object.assign(t2, { optional: () => h(t2), and: (e2) => d(t2, e2), or: (e2) => y(t2, e2), select: (e2) => void 0 === e2 ? v(t2) : v(e2, t2) });
|
|
73
73
|
}
|
|
74
74
|
function h(e2) {
|
|
75
|
-
return
|
|
75
|
+
return a({ [t]: () => ({ match: (t2) => {
|
|
76
76
|
let n2 = {};
|
|
77
77
|
const r2 = (t3, e3) => {
|
|
78
78
|
n2[t3] = e3;
|
|
@@ -80,8 +80,8 @@ function h(e2) {
|
|
|
80
80
|
return void 0 === t2 ? (s(e2).forEach((t3) => r2(t3, void 0)), { matched: true, selections: n2 }) : { matched: o(e2, t2, r2), selections: n2 };
|
|
81
81
|
}, getSelectionKeys: () => s(e2), matcherType: "optional" }) });
|
|
82
82
|
}
|
|
83
|
-
function
|
|
84
|
-
return
|
|
83
|
+
function d(...e2) {
|
|
84
|
+
return a({ [t]: () => ({ match: (t2) => {
|
|
85
85
|
let n2 = {};
|
|
86
86
|
const r2 = (t3, e3) => {
|
|
87
87
|
n2[t3] = e3;
|
|
@@ -89,8 +89,8 @@ function m(...e2) {
|
|
|
89
89
|
return { matched: e2.every((e3) => o(e3, t2, r2)), selections: n2 };
|
|
90
90
|
}, getSelectionKeys: () => c(e2, s), matcherType: "and" }) });
|
|
91
91
|
}
|
|
92
|
-
function
|
|
93
|
-
return
|
|
92
|
+
function y(...e2) {
|
|
93
|
+
return a({ [t]: () => ({ match: (t2) => {
|
|
94
94
|
let n2 = {};
|
|
95
95
|
const r2 = (t3, e3) => {
|
|
96
96
|
n2[t3] = e3;
|
|
@@ -101,58 +101,60 @@ function d(...e2) {
|
|
|
101
101
|
function p(e2) {
|
|
102
102
|
return { [t]: () => ({ match: (t2) => ({ matched: Boolean(e2(t2)) }) }) };
|
|
103
103
|
}
|
|
104
|
-
function
|
|
104
|
+
function v(...e2) {
|
|
105
105
|
const r2 = "string" == typeof e2[0] ? e2[0] : void 0, i2 = 2 === e2.length ? e2[1] : "string" == typeof e2[0] ? void 0 : e2[0];
|
|
106
|
-
return
|
|
106
|
+
return a({ [t]: () => ({ match: (t2) => {
|
|
107
107
|
let e3 = { [null != r2 ? r2 : n]: t2 };
|
|
108
108
|
return { matched: void 0 === i2 || o(i2, t2, (t3, n2) => {
|
|
109
109
|
e3[t3] = n2;
|
|
110
110
|
}), selections: e3 };
|
|
111
111
|
}, getSelectionKeys: () => [null != r2 ? r2 : n].concat(void 0 === i2 ? [] : s(i2)) }) });
|
|
112
112
|
}
|
|
113
|
-
function
|
|
113
|
+
function b(t2) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
function w(t2) {
|
|
114
117
|
return "number" == typeof t2;
|
|
115
118
|
}
|
|
116
|
-
function
|
|
119
|
+
function S(t2) {
|
|
117
120
|
return "string" == typeof t2;
|
|
118
121
|
}
|
|
119
|
-
function
|
|
122
|
+
function j(t2) {
|
|
120
123
|
return "bigint" == typeof t2;
|
|
121
124
|
}
|
|
122
|
-
var
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return j(m(t2, (n2 = e2, p((t3) => b(t3) && t3.startsWith(n2)))));
|
|
125
|
+
var K = a(p(b));
|
|
126
|
+
var O = a(p(b));
|
|
127
|
+
var x = (t2) => Object.assign(a(t2), { startsWith: (e2) => {
|
|
128
|
+
return x(d(t2, (n2 = e2, p((t3) => S(t3) && t3.startsWith(n2)))));
|
|
127
129
|
var n2;
|
|
128
130
|
}, endsWith: (e2) => {
|
|
129
|
-
return
|
|
131
|
+
return x(d(t2, (n2 = e2, p((t3) => S(t3) && t3.endsWith(n2)))));
|
|
130
132
|
var n2;
|
|
131
|
-
}, minLength: (e2) =>
|
|
132
|
-
return
|
|
133
|
+
}, minLength: (e2) => x(d(t2, ((t3) => p((e3) => S(e3) && e3.length >= t3))(e2))), length: (e2) => x(d(t2, ((t3) => p((e3) => S(e3) && e3.length === t3))(e2))), maxLength: (e2) => x(d(t2, ((t3) => p((e3) => S(e3) && e3.length <= t3))(e2))), includes: (e2) => {
|
|
134
|
+
return x(d(t2, (n2 = e2, p((t3) => S(t3) && t3.includes(n2)))));
|
|
133
135
|
var n2;
|
|
134
136
|
}, regex: (e2) => {
|
|
135
|
-
return
|
|
137
|
+
return x(d(t2, (n2 = e2, p((t3) => S(t3) && Boolean(t3.match(n2))))));
|
|
136
138
|
var n2;
|
|
137
139
|
} });
|
|
138
|
-
var
|
|
139
|
-
var
|
|
140
|
-
var
|
|
141
|
-
var
|
|
142
|
-
var
|
|
143
|
-
var
|
|
140
|
+
var A = x(p(S));
|
|
141
|
+
var N = (t2) => Object.assign(a(t2), { between: (e2, n2) => N(d(t2, ((t3, e3) => p((n3) => w(n3) && t3 <= n3 && e3 >= n3))(e2, n2))), lt: (e2) => N(d(t2, ((t3) => p((e3) => w(e3) && e3 < t3))(e2))), gt: (e2) => N(d(t2, ((t3) => p((e3) => w(e3) && e3 > t3))(e2))), lte: (e2) => N(d(t2, ((t3) => p((e3) => w(e3) && e3 <= t3))(e2))), gte: (e2) => N(d(t2, ((t3) => p((e3) => w(e3) && e3 >= t3))(e2))), int: () => N(d(t2, p((t3) => w(t3) && Number.isInteger(t3)))), finite: () => N(d(t2, p((t3) => w(t3) && Number.isFinite(t3)))), positive: () => N(d(t2, p((t3) => w(t3) && t3 > 0))), negative: () => N(d(t2, p((t3) => w(t3) && t3 < 0))) });
|
|
142
|
+
var P = N(p(w));
|
|
143
|
+
var k = (t2) => Object.assign(a(t2), { between: (e2, n2) => k(d(t2, ((t3, e3) => p((n3) => j(n3) && t3 <= n3 && e3 >= n3))(e2, n2))), lt: (e2) => k(d(t2, ((t3) => p((e3) => j(e3) && e3 < t3))(e2))), gt: (e2) => k(d(t2, ((t3) => p((e3) => j(e3) && e3 > t3))(e2))), lte: (e2) => k(d(t2, ((t3) => p((e3) => j(e3) && e3 <= t3))(e2))), gte: (e2) => k(d(t2, ((t3) => p((e3) => j(e3) && e3 >= t3))(e2))), positive: () => k(d(t2, p((t3) => j(t3) && t3 > 0))), negative: () => k(d(t2, p((t3) => j(t3) && t3 < 0))) });
|
|
144
|
+
var T = k(p(j));
|
|
145
|
+
var B = a(p(function(t2) {
|
|
144
146
|
return "boolean" == typeof t2;
|
|
145
147
|
}));
|
|
146
|
-
var
|
|
148
|
+
var _ = a(p(function(t2) {
|
|
147
149
|
return "symbol" == typeof t2;
|
|
148
150
|
}));
|
|
149
|
-
var
|
|
151
|
+
var W = a(p(function(t2) {
|
|
150
152
|
return null == t2;
|
|
151
153
|
}));
|
|
152
|
-
var
|
|
154
|
+
var $ = a(p(function(t2) {
|
|
153
155
|
return null != t2;
|
|
154
156
|
}));
|
|
155
|
-
var
|
|
157
|
+
var I = class extends Error {
|
|
156
158
|
constructor(t2) {
|
|
157
159
|
let e2;
|
|
158
160
|
try {
|
|
@@ -163,11 +165,11 @@ var W = class extends Error {
|
|
|
163
165
|
super(`Pattern matching error: no pattern matches value ${e2}`), this.input = void 0, this.input = t2;
|
|
164
166
|
}
|
|
165
167
|
};
|
|
166
|
-
var
|
|
167
|
-
function
|
|
168
|
-
return new
|
|
168
|
+
var L = { matched: false, value: void 0 };
|
|
169
|
+
function M(t2) {
|
|
170
|
+
return new R(t2, L);
|
|
169
171
|
}
|
|
170
|
-
var
|
|
172
|
+
var R = class _R {
|
|
171
173
|
constructor(t2, e2) {
|
|
172
174
|
this.input = void 0, this.state = void 0, this.input = t2, this.state = e2;
|
|
173
175
|
}
|
|
@@ -177,20 +179,20 @@ var I = class _I {
|
|
|
177
179
|
let i2;
|
|
178
180
|
3 === t2.length && "function" == typeof t2[1] ? i2 = t2[1] : t2.length > 2 && r2.push(...t2.slice(1, t2.length - 1));
|
|
179
181
|
let s2 = false, c2 = {};
|
|
180
|
-
const
|
|
182
|
+
const u = (t3, e3) => {
|
|
181
183
|
s2 = true, c2[t3] = e3;
|
|
182
|
-
},
|
|
183
|
-
return new
|
|
184
|
+
}, a2 = !r2.some((t3) => o(t3, this.input, u)) || i2 && !Boolean(i2(this.input)) ? L : { matched: true, value: e2(s2 ? n in c2 ? c2[n] : c2 : this.input, this.input) };
|
|
185
|
+
return new _R(this.input, a2);
|
|
184
186
|
}
|
|
185
187
|
when(t2, e2) {
|
|
186
188
|
if (this.state.matched) return this;
|
|
187
189
|
const n2 = Boolean(t2(this.input));
|
|
188
|
-
return new
|
|
190
|
+
return new _R(this.input, n2 ? { matched: true, value: e2(this.input, this.input) } : L);
|
|
189
191
|
}
|
|
190
192
|
otherwise(t2) {
|
|
191
193
|
return this.state.matched ? this.state.value : t2(this.input);
|
|
192
194
|
}
|
|
193
|
-
exhaustive(t2 =
|
|
195
|
+
exhaustive(t2 = F) {
|
|
194
196
|
return this.state.matched ? this.state.value : t2(this.input);
|
|
195
197
|
}
|
|
196
198
|
run() {
|
|
@@ -199,9 +201,12 @@ var I = class _I {
|
|
|
199
201
|
returnType() {
|
|
200
202
|
return this;
|
|
201
203
|
}
|
|
204
|
+
narrow() {
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
202
207
|
};
|
|
203
|
-
function
|
|
204
|
-
throw new
|
|
208
|
+
function F(t2) {
|
|
209
|
+
throw new I(t2);
|
|
205
210
|
}
|
|
206
211
|
|
|
207
212
|
// src/utils.ts
|
|
@@ -230,7 +235,7 @@ function getChecksumChar({
|
|
|
230
235
|
const weights = [2, 7, 6, 5, 4, 3, 2];
|
|
231
236
|
const st = ["J", "Z", "I", "H", "G", "F", "E", "D", "C", "B", "A"];
|
|
232
237
|
const fg = ["X", "W", "U", "T", "R", "Q", "P", "N", "M", "L", "K"];
|
|
233
|
-
const
|
|
238
|
+
const m = ["K", "L", "J", "N", "P", "Q", "R", "T", "U", "W", "X"];
|
|
234
239
|
const sum = idNumbers.reduce(
|
|
235
240
|
(acc, digit, idx) => acc + digit * weights[idx],
|
|
236
241
|
0
|
|
@@ -250,7 +255,7 @@ function getChecksumChar({
|
|
|
250
255
|
if (prefix === "F" || prefix === "G") {
|
|
251
256
|
expectedChar = fg[checksumIndex];
|
|
252
257
|
} else if (prefix === "M") {
|
|
253
|
-
expectedChar =
|
|
258
|
+
expectedChar = m[checksumIndex];
|
|
254
259
|
}
|
|
255
260
|
}
|
|
256
261
|
return expectedChar;
|
|
@@ -270,22 +275,34 @@ function generateByType(type) {
|
|
|
270
275
|
return randomPrefix + randomDigits + checksum;
|
|
271
276
|
}
|
|
272
277
|
function generateID({ type }) {
|
|
273
|
-
return
|
|
278
|
+
return M(type).with("NRIC", () => generateByType("NRIC")).with("FIN", () => generateByType("FIN")).with("BOTH", () => generateByType(Math.random() < 0.5 ? "NRIC" : "FIN")).exhaustive();
|
|
274
279
|
}
|
|
275
280
|
|
|
276
281
|
// src/validator.ts
|
|
277
282
|
var Validator = class {
|
|
278
283
|
// method to check if given string is a valid NRIC
|
|
279
284
|
static isValidNRIC(value) {
|
|
280
|
-
return validate({ inputString: value, type: "NRIC" });
|
|
285
|
+
return validate({ inputString: value?.trim(), type: "NRIC" });
|
|
281
286
|
}
|
|
282
287
|
// method to check if given string is a valid FIN
|
|
283
288
|
static isValidFIN(value) {
|
|
284
|
-
return validate({ inputString: value, type: "FIN" });
|
|
289
|
+
return validate({ inputString: value?.trim(), type: "FIN" });
|
|
285
290
|
}
|
|
286
291
|
// method to check if given string is a valid NRIC / FIN. Useful for those fields that checks for generic IDs
|
|
287
292
|
static isValidId(value) {
|
|
288
|
-
return validate({ inputString: value, type: "BOTH" });
|
|
293
|
+
return validate({ inputString: value?.trim(), type: "BOTH" });
|
|
294
|
+
}
|
|
295
|
+
// checks whether the value has the expected NRIC structure, without validating its checksum
|
|
296
|
+
static isLikelyNRIC(value) {
|
|
297
|
+
return typeof value === "string" && /^[ST]\d{7}[A-Z]$/i.test(value.trim());
|
|
298
|
+
}
|
|
299
|
+
// checks whether the value has the expected FIN structure, without validating its checksum
|
|
300
|
+
static isLikelyFIN(value) {
|
|
301
|
+
return typeof value === "string" && /^[FGM]\d{7}[A-Z]$/i.test(value.trim());
|
|
302
|
+
}
|
|
303
|
+
// checks whether the value has the expected NRIC / FIN structure, without validating its checksum
|
|
304
|
+
static isLikelyId(value) {
|
|
305
|
+
return typeof value === "string" && /^[STFGM]\d{7}[A-Z]$/i.test(value.trim());
|
|
289
306
|
}
|
|
290
307
|
// method to generate random NRIC
|
|
291
308
|
static generateNRIC() {
|