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/dist/index.js CHANGED
@@ -17,37 +17,37 @@ var o = (n2, s2, c2) => {
17
17
  if (!r(s2)) return false;
18
18
  if (Array.isArray(n2)) {
19
19
  if (!Array.isArray(s2)) return false;
20
- let t2 = [], r2 = [], a = [];
20
+ let t2 = [], r2 = [], u = [];
21
21
  for (const o2 of n2.keys()) {
22
22
  const s3 = n2[o2];
23
- i(s3) && s3[e] ? a.push(s3) : a.length ? r2.push(s3) : t2.push(s3);
23
+ i(s3) && s3[e] ? u.push(s3) : u.length ? r2.push(s3) : t2.push(s3);
24
24
  }
25
- if (a.length) {
26
- if (a.length > 1) throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");
25
+ if (u.length) {
26
+ if (u.length > 1) throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");
27
27
  if (s2.length < t2.length + r2.length) return false;
28
28
  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);
29
- return t2.every((t3, n4) => o(t3, e2[n4], c2)) && r2.every((t3, e3) => o(t3, n3[e3], c2)) && (0 === a.length || o(a[0], i2, c2));
29
+ 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));
30
30
  }
31
31
  return n2.length === s2.length && n2.every((t3, e2) => o(t3, s2[e2], c2));
32
32
  }
33
33
  return Reflect.ownKeys(n2).every((e2) => {
34
34
  const r2 = n2[e2];
35
- return (e2 in s2 || i(a = r2) && "optional" === a[t]().matcherType) && o(r2, s2[e2], c2);
36
- var a;
35
+ return (e2 in s2 || i(u = r2) && "optional" === u[t]().matcherType) && o(r2, s2[e2], c2);
36
+ var u;
37
37
  });
38
38
  }
39
39
  return Object.is(s2, n2);
40
40
  };
41
41
  var s = (e2) => {
42
- var n2, o2, a;
43
- return r(e2) ? i(e2) ? null != (n2 = null == (o2 = (a = e2[t]()).getSelectionKeys) ? void 0 : o2.call(a)) ? n2 : [] : Array.isArray(e2) ? c(e2, s) : c(Object.values(e2), s) : [];
42
+ var n2, o2, u;
43
+ 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) : [];
44
44
  };
45
45
  var c = (t2, e2) => t2.reduce((t3, n2) => t3.concat(e2(n2)), []);
46
- function u(t2) {
47
- return Object.assign(t2, { optional: () => h(t2), and: (e2) => m(t2, e2), or: (e2) => d(t2, e2), select: (e2) => void 0 === e2 ? y(t2) : y(e2, t2) });
46
+ function a(t2) {
47
+ 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) });
48
48
  }
49
49
  function h(e2) {
50
- return u({ [t]: () => ({ match: (t2) => {
50
+ return a({ [t]: () => ({ match: (t2) => {
51
51
  let n2 = {};
52
52
  const r2 = (t3, e3) => {
53
53
  n2[t3] = e3;
@@ -55,8 +55,8 @@ function h(e2) {
55
55
  return void 0 === t2 ? (s(e2).forEach((t3) => r2(t3, void 0)), { matched: true, selections: n2 }) : { matched: o(e2, t2, r2), selections: n2 };
56
56
  }, getSelectionKeys: () => s(e2), matcherType: "optional" }) });
57
57
  }
58
- function m(...e2) {
59
- return u({ [t]: () => ({ match: (t2) => {
58
+ function d(...e2) {
59
+ return a({ [t]: () => ({ match: (t2) => {
60
60
  let n2 = {};
61
61
  const r2 = (t3, e3) => {
62
62
  n2[t3] = e3;
@@ -64,8 +64,8 @@ function m(...e2) {
64
64
  return { matched: e2.every((e3) => o(e3, t2, r2)), selections: n2 };
65
65
  }, getSelectionKeys: () => c(e2, s), matcherType: "and" }) });
66
66
  }
67
- function d(...e2) {
68
- return u({ [t]: () => ({ match: (t2) => {
67
+ function y(...e2) {
68
+ return a({ [t]: () => ({ match: (t2) => {
69
69
  let n2 = {};
70
70
  const r2 = (t3, e3) => {
71
71
  n2[t3] = e3;
@@ -76,58 +76,60 @@ function d(...e2) {
76
76
  function p(e2) {
77
77
  return { [t]: () => ({ match: (t2) => ({ matched: Boolean(e2(t2)) }) }) };
78
78
  }
79
- function y(...e2) {
79
+ function v(...e2) {
80
80
  const r2 = "string" == typeof e2[0] ? e2[0] : void 0, i2 = 2 === e2.length ? e2[1] : "string" == typeof e2[0] ? void 0 : e2[0];
81
- return u({ [t]: () => ({ match: (t2) => {
81
+ return a({ [t]: () => ({ match: (t2) => {
82
82
  let e3 = { [null != r2 ? r2 : n]: t2 };
83
83
  return { matched: void 0 === i2 || o(i2, t2, (t3, n2) => {
84
84
  e3[t3] = n2;
85
85
  }), selections: e3 };
86
86
  }, getSelectionKeys: () => [null != r2 ? r2 : n].concat(void 0 === i2 ? [] : s(i2)) }) });
87
87
  }
88
- function v(t2) {
88
+ function b(t2) {
89
+ return true;
90
+ }
91
+ function w(t2) {
89
92
  return "number" == typeof t2;
90
93
  }
91
- function b(t2) {
94
+ function S(t2) {
92
95
  return "string" == typeof t2;
93
96
  }
94
- function w(t2) {
97
+ function j(t2) {
95
98
  return "bigint" == typeof t2;
96
99
  }
97
- var S = u(p(function(t2) {
98
- return true;
99
- }));
100
- var j = (t2) => Object.assign(u(t2), { startsWith: (e2) => {
101
- return j(m(t2, (n2 = e2, p((t3) => b(t3) && t3.startsWith(n2)))));
100
+ var K = a(p(b));
101
+ var O = a(p(b));
102
+ var x = (t2) => Object.assign(a(t2), { startsWith: (e2) => {
103
+ return x(d(t2, (n2 = e2, p((t3) => S(t3) && t3.startsWith(n2)))));
102
104
  var n2;
103
105
  }, endsWith: (e2) => {
104
- return j(m(t2, (n2 = e2, p((t3) => b(t3) && t3.endsWith(n2)))));
106
+ return x(d(t2, (n2 = e2, p((t3) => S(t3) && t3.endsWith(n2)))));
105
107
  var n2;
106
- }, minLength: (e2) => j(m(t2, ((t3) => p((e3) => b(e3) && e3.length >= t3))(e2))), length: (e2) => j(m(t2, ((t3) => p((e3) => b(e3) && e3.length === t3))(e2))), maxLength: (e2) => j(m(t2, ((t3) => p((e3) => b(e3) && e3.length <= t3))(e2))), includes: (e2) => {
107
- return j(m(t2, (n2 = e2, p((t3) => b(t3) && t3.includes(n2)))));
108
+ }, 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) => {
109
+ return x(d(t2, (n2 = e2, p((t3) => S(t3) && t3.includes(n2)))));
108
110
  var n2;
109
111
  }, regex: (e2) => {
110
- return j(m(t2, (n2 = e2, p((t3) => b(t3) && Boolean(t3.match(n2))))));
112
+ return x(d(t2, (n2 = e2, p((t3) => S(t3) && Boolean(t3.match(n2))))));
111
113
  var n2;
112
114
  } });
113
- var K = j(p(b));
114
- var x = (t2) => Object.assign(u(t2), { between: (e2, n2) => x(m(t2, ((t3, e3) => p((n3) => v(n3) && t3 <= n3 && e3 >= n3))(e2, n2))), lt: (e2) => x(m(t2, ((t3) => p((e3) => v(e3) && e3 < t3))(e2))), gt: (e2) => x(m(t2, ((t3) => p((e3) => v(e3) && e3 > t3))(e2))), lte: (e2) => x(m(t2, ((t3) => p((e3) => v(e3) && e3 <= t3))(e2))), gte: (e2) => x(m(t2, ((t3) => p((e3) => v(e3) && e3 >= t3))(e2))), int: () => x(m(t2, p((t3) => v(t3) && Number.isInteger(t3)))), finite: () => x(m(t2, p((t3) => v(t3) && Number.isFinite(t3)))), positive: () => x(m(t2, p((t3) => v(t3) && t3 > 0))), negative: () => x(m(t2, p((t3) => v(t3) && t3 < 0))) });
115
- var E = x(p(v));
116
- var A = (t2) => Object.assign(u(t2), { between: (e2, n2) => A(m(t2, ((t3, e3) => p((n3) => w(n3) && t3 <= n3 && e3 >= n3))(e2, n2))), lt: (e2) => A(m(t2, ((t3) => p((e3) => w(e3) && e3 < t3))(e2))), gt: (e2) => A(m(t2, ((t3) => p((e3) => w(e3) && e3 > t3))(e2))), lte: (e2) => A(m(t2, ((t3) => p((e3) => w(e3) && e3 <= t3))(e2))), gte: (e2) => A(m(t2, ((t3) => p((e3) => w(e3) && e3 >= t3))(e2))), positive: () => A(m(t2, p((t3) => w(t3) && t3 > 0))), negative: () => A(m(t2, p((t3) => w(t3) && t3 < 0))) });
117
- var P = A(p(w));
118
- var T = u(p(function(t2) {
115
+ var A = x(p(S));
116
+ 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))) });
117
+ var P = N(p(w));
118
+ 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))) });
119
+ var T = k(p(j));
120
+ var B = a(p(function(t2) {
119
121
  return "boolean" == typeof t2;
120
122
  }));
121
- var B = u(p(function(t2) {
123
+ var _ = a(p(function(t2) {
122
124
  return "symbol" == typeof t2;
123
125
  }));
124
- var _ = u(p(function(t2) {
126
+ var W = a(p(function(t2) {
125
127
  return null == t2;
126
128
  }));
127
- var k = u(p(function(t2) {
129
+ var $ = a(p(function(t2) {
128
130
  return null != t2;
129
131
  }));
130
- var W = class extends Error {
132
+ var I = class extends Error {
131
133
  constructor(t2) {
132
134
  let e2;
133
135
  try {
@@ -138,11 +140,11 @@ var W = class extends Error {
138
140
  super(`Pattern matching error: no pattern matches value ${e2}`), this.input = void 0, this.input = t2;
139
141
  }
140
142
  };
141
- var $ = { matched: false, value: void 0 };
142
- function z(t2) {
143
- return new I(t2, $);
143
+ var L = { matched: false, value: void 0 };
144
+ function M(t2) {
145
+ return new R(t2, L);
144
146
  }
145
- var I = class _I {
147
+ var R = class _R {
146
148
  constructor(t2, e2) {
147
149
  this.input = void 0, this.state = void 0, this.input = t2, this.state = e2;
148
150
  }
@@ -152,20 +154,20 @@ var I = class _I {
152
154
  let i2;
153
155
  3 === t2.length && "function" == typeof t2[1] ? i2 = t2[1] : t2.length > 2 && r2.push(...t2.slice(1, t2.length - 1));
154
156
  let s2 = false, c2 = {};
155
- const a = (t3, e3) => {
157
+ const u = (t3, e3) => {
156
158
  s2 = true, c2[t3] = e3;
157
- }, u2 = !r2.some((t3) => o(t3, this.input, a)) || i2 && !Boolean(i2(this.input)) ? $ : { matched: true, value: e2(s2 ? n in c2 ? c2[n] : c2 : this.input, this.input) };
158
- return new _I(this.input, u2);
159
+ }, 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) };
160
+ return new _R(this.input, a2);
159
161
  }
160
162
  when(t2, e2) {
161
163
  if (this.state.matched) return this;
162
164
  const n2 = Boolean(t2(this.input));
163
- return new _I(this.input, n2 ? { matched: true, value: e2(this.input, this.input) } : $);
165
+ return new _R(this.input, n2 ? { matched: true, value: e2(this.input, this.input) } : L);
164
166
  }
165
167
  otherwise(t2) {
166
168
  return this.state.matched ? this.state.value : t2(this.input);
167
169
  }
168
- exhaustive(t2 = L) {
170
+ exhaustive(t2 = F) {
169
171
  return this.state.matched ? this.state.value : t2(this.input);
170
172
  }
171
173
  run() {
@@ -174,9 +176,12 @@ var I = class _I {
174
176
  returnType() {
175
177
  return this;
176
178
  }
179
+ narrow() {
180
+ return this;
181
+ }
177
182
  };
178
- function L(t2) {
179
- throw new W(t2);
183
+ function F(t2) {
184
+ throw new I(t2);
180
185
  }
181
186
 
182
187
  // src/utils.ts
@@ -205,7 +210,7 @@ function getChecksumChar({
205
210
  const weights = [2, 7, 6, 5, 4, 3, 2];
206
211
  const st = ["J", "Z", "I", "H", "G", "F", "E", "D", "C", "B", "A"];
207
212
  const fg = ["X", "W", "U", "T", "R", "Q", "P", "N", "M", "L", "K"];
208
- const m2 = ["K", "L", "J", "N", "P", "Q", "R", "T", "U", "W", "X"];
213
+ const m = ["K", "L", "J", "N", "P", "Q", "R", "T", "U", "W", "X"];
209
214
  const sum = idNumbers.reduce(
210
215
  (acc, digit, idx) => acc + digit * weights[idx],
211
216
  0
@@ -225,7 +230,7 @@ function getChecksumChar({
225
230
  if (prefix === "F" || prefix === "G") {
226
231
  expectedChar = fg[checksumIndex];
227
232
  } else if (prefix === "M") {
228
- expectedChar = m2[checksumIndex];
233
+ expectedChar = m[checksumIndex];
229
234
  }
230
235
  }
231
236
  return expectedChar;
@@ -245,22 +250,34 @@ function generateByType(type) {
245
250
  return randomPrefix + randomDigits + checksum;
246
251
  }
247
252
  function generateID({ type }) {
248
- return z(type).with("NRIC", () => generateByType("NRIC")).with("FIN", () => generateByType("FIN")).with("BOTH", () => generateByType(Math.random() < 0.5 ? "NRIC" : "FIN")).exhaustive();
253
+ return M(type).with("NRIC", () => generateByType("NRIC")).with("FIN", () => generateByType("FIN")).with("BOTH", () => generateByType(Math.random() < 0.5 ? "NRIC" : "FIN")).exhaustive();
249
254
  }
250
255
 
251
256
  // src/validator.ts
252
257
  var Validator = class {
253
258
  // method to check if given string is a valid NRIC
254
259
  static isValidNRIC(value) {
255
- return validate({ inputString: value, type: "NRIC" });
260
+ return validate({ inputString: value?.trim(), type: "NRIC" });
256
261
  }
257
262
  // method to check if given string is a valid FIN
258
263
  static isValidFIN(value) {
259
- return validate({ inputString: value, type: "FIN" });
264
+ return validate({ inputString: value?.trim(), type: "FIN" });
260
265
  }
261
266
  // method to check if given string is a valid NRIC / FIN. Useful for those fields that checks for generic IDs
262
267
  static isValidId(value) {
263
- return validate({ inputString: value, type: "BOTH" });
268
+ return validate({ inputString: value?.trim(), type: "BOTH" });
269
+ }
270
+ // checks whether the value has the expected NRIC structure, without validating its checksum
271
+ static isLikelyNRIC(value) {
272
+ return typeof value === "string" && /^[ST]\d{7}[A-Z]$/i.test(value.trim());
273
+ }
274
+ // checks whether the value has the expected FIN structure, without validating its checksum
275
+ static isLikelyFIN(value) {
276
+ return typeof value === "string" && /^[FGM]\d{7}[A-Z]$/i.test(value.trim());
277
+ }
278
+ // checks whether the value has the expected NRIC / FIN structure, without validating its checksum
279
+ static isLikelyId(value) {
280
+ return typeof value === "string" && /^[STFGM]\d{7}[A-Z]$/i.test(value.trim());
264
281
  }
265
282
  // method to generate random NRIC
266
283
  static generateNRIC() {