@indodev/toolkit 0.5.0 → 0.6.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.
@@ -0,0 +1,495 @@
1
+ /**
2
+ * Validates a NIK (Nomor Induk Kependudukan) format.
3
+ *
4
+ * A valid NIK must:
5
+ * - Be exactly 16 digits
6
+ * - Have a valid province code (positions 1-2)
7
+ * - Have a valid date (positions 7-12)
8
+ * - Not be in the future
9
+ * - Not be before 1900
10
+ *
11
+ * For female NIKs, the day is encoded as (actual day + 40).
12
+ * For example, a female born on the 15th would have day = 55.
13
+ *
14
+ * @param nik - The 16-digit NIK string to validate
15
+ * @returns `true` if the NIK is valid, `false` otherwise
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * validateNIK('3201234567890123'); // true - valid NIK
20
+ * validateNIK('1234'); // false - wrong length
21
+ * validateNIK('9912345678901234'); // false - invalid province
22
+ * ```
23
+ *
24
+ * @public
25
+ */
26
+ declare function validateNIK(nik: string): boolean;
27
+
28
+ /**
29
+ * Information extracted from a valid NIK.
30
+ *
31
+ * Contains parsed data including location codes, birth date, gender,
32
+ * and serial number from a 16-digit NIK string.
33
+ *
34
+ * @public
35
+ */
36
+ interface NIKInfo {
37
+ /**
38
+ * Province information extracted from positions 1-2 of the NIK.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * { code: '32', name: 'Jawa Barat' }
43
+ * ```
44
+ */
45
+ province: {
46
+ /** Two-digit province code (e.g., '32') */
47
+ code: string;
48
+ /** Full province name (e.g., 'Jawa Barat') */
49
+ name: string;
50
+ };
51
+ /**
52
+ * Regency (Kabupaten/Kota) information extracted from positions 3-4 of the NIK.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * { code: '01', name: 'Kab. Bogor' }
57
+ * ```
58
+ */
59
+ regency: {
60
+ /** Two-digit regency code (e.g., '01') */
61
+ code: string;
62
+ /** Full regency name (e.g., 'Kab. Bogor') */
63
+ name: string;
64
+ };
65
+ /**
66
+ * District (Kecamatan) information extracted from positions 5-6 of the NIK.
67
+ * May be `null` if district data is not available.
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * { code: '23', name: 'Ciawi' }
72
+ * ```
73
+ */
74
+ district: {
75
+ /** Two-digit district code (e.g., '23') */
76
+ code: string;
77
+ /** Full district name, or `null` if data unavailable */
78
+ name: string | null;
79
+ };
80
+ /**
81
+ * Birth date extracted from positions 7-12 of the NIK.
82
+ * For females, the day is encoded as (actual day + 40).
83
+ * Returns `null` if the date is invalid.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * new Date(1989, 0, 31) // January 31, 1989
88
+ * ```
89
+ */
90
+ birthDate: Date | null;
91
+ /**
92
+ * Gender derived from the day encoding in the NIK.
93
+ * - 'male': day is 1-31
94
+ * - 'female': day is 41-71 (actual day + 40)
95
+ * - `null`: if unable to determine
96
+ */
97
+ gender: 'male' | 'female' | null;
98
+ /**
99
+ * Serial number from positions 13-16 of the NIK.
100
+ * Uniquely identifies individuals with the same location and birth date.
101
+ * Returns `null` if unable to extract.
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * '0123'
106
+ * ```
107
+ */
108
+ serialNumber: string | null;
109
+ /**
110
+ * Whether the NIK passed validation checks.
111
+ * If `false`, other fields may be `null` or contain partial data.
112
+ */
113
+ isValid: boolean;
114
+ }
115
+ /**
116
+ * Options for masking a NIK to protect privacy.
117
+ *
118
+ * Controls how many characters to show at the start and end,
119
+ * what character to use for masking, and optional separators.
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * // Default: shows first 4 and last 4 digits
124
+ * { start: 4, end: 4, char: '*' }
125
+ * // Result: '3201********0123'
126
+ *
127
+ * // With separator
128
+ * { start: 4, end: 4, char: '*', separator: '-' }
129
+ * // Result: '3201-****-****-0123'
130
+ * ```
131
+ *
132
+ * @public
133
+ */
134
+ interface MaskOptions {
135
+ /**
136
+ * Number of characters to show at the start.
137
+ *
138
+ * @defaultValue 4
139
+ */
140
+ start?: number;
141
+ /**
142
+ * Number of characters to show at the end.
143
+ *
144
+ * @defaultValue 4
145
+ */
146
+ end?: number;
147
+ /**
148
+ * Character to use for masking hidden digits.
149
+ *
150
+ * @defaultValue '*'
151
+ */
152
+ char?: string;
153
+ /**
154
+ * Optional separator to add between groups of digits.
155
+ * If provided, the NIK will be formatted with separators.
156
+ *
157
+ * @defaultValue undefined (no separator)
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * '-' // Results in format: '3201-****-****-0123'
162
+ * ' ' // Results in format: '3201 **** **** 0123'
163
+ * ```
164
+ */
165
+ separator?: string;
166
+ }
167
+ /**
168
+ * Error thrown when an invalid NIK is provided to a function.
169
+ * Extends native Error with a `code` property for programmatic error handling.
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * try {
174
+ * requireNIK('invalid');
175
+ * } catch (error) {
176
+ * if (error instanceof InvalidNIKError) {
177
+ * console.log(error.code); // 'INVALID_NIK'
178
+ * }
179
+ * }
180
+ * ```
181
+ *
182
+ * @public
183
+ */
184
+ declare class InvalidNIKError extends Error {
185
+ /** Error code for programmatic identification */
186
+ readonly code: "INVALID_NIK";
187
+ constructor(message?: string);
188
+ }
189
+ /**
190
+ * Error codes for detailed NIK validation.
191
+ *
192
+ * @public
193
+ */
194
+ type NIKErrorCode = 'INVALID_FORMAT' | 'INVALID_PROVINCE' | 'INVALID_MONTH' | 'INVALID_DAY' | 'INVALID_DATE' | 'FUTURE_DATE';
195
+ /**
196
+ * A single validation error with code and message.
197
+ *
198
+ * @public
199
+ */
200
+ interface NIKValidationError {
201
+ /** Error code for programmatic handling */
202
+ code: NIKErrorCode;
203
+ /** Human-readable error message */
204
+ message: string;
205
+ }
206
+ /**
207
+ * Detailed validation result for NIK with structured error reporting.
208
+ *
209
+ * Use this for form validation where you need to show specific error
210
+ * messages per field.
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * const result = validateNIKDetailed('1234');
215
+ * if (!result.isValid) {
216
+ * result.errors.forEach(err => {
217
+ * console.log(err.code, err.message);
218
+ * });
219
+ * }
220
+ * ```
221
+ *
222
+ * @public
223
+ */
224
+ interface NIKValidationResult {
225
+ /** Whether the NIK is valid */
226
+ isValid: boolean;
227
+ /** List of validation errors (empty if valid) */
228
+ errors: NIKValidationError[];
229
+ /** Cleaned 16-digit NIK if valid, null otherwise */
230
+ nik: string | null;
231
+ }
232
+ /**
233
+ * Options for getAge function.
234
+ *
235
+ * @public
236
+ */
237
+ interface GetAgeOptions {
238
+ /** Date to calculate age from (default: current date) */
239
+ referenceDate?: Date;
240
+ /** Return formatted Indonesian string instead of object */
241
+ asString?: boolean;
242
+ }
243
+ /**
244
+ * Age result object with years, months, and days.
245
+ *
246
+ * @public
247
+ */
248
+ interface Age {
249
+ /** Full years */
250
+ years: number;
251
+ /** Remaining months after full years */
252
+ months: number;
253
+ /** Remaining days after full months */
254
+ days: number;
255
+ }
256
+
257
+ /**
258
+ * Parses a NIK and extracts all embedded information.
259
+ *
260
+ * Extracts province, regency, district codes, birth date, gender,
261
+ * and serial number from a 16-digit NIK string.
262
+ *
263
+ * @param nik - The 16-digit NIK string to parse
264
+ * @returns Parsed NIK information, or `null` if the NIK format is invalid
265
+ *
266
+ * @example
267
+ * Parse a valid male NIK:
268
+ * ```typescript
269
+ * const info = parseNIK('3201018901310123');
270
+ * console.log(info);
271
+ * // {
272
+ * // province: { code: '32', name: 'Jawa Barat' },
273
+ * // regency: { code: '01', name: 'Kab. Bogor' },
274
+ * // district: { code: '01', name: null },
275
+ * // birthDate: Date(1989, 0, 31), // Jan 31, 1989
276
+ * // gender: 'male',
277
+ * // serialNumber: '0123',
278
+ * // isValid: true
279
+ * // }
280
+ * ```
281
+ *
282
+ * @example
283
+ * Parse a female NIK (day + 40):
284
+ * ```typescript
285
+ * const info = parseNIK('3201019508550123');
286
+ * console.log(info.gender); // 'female'
287
+ * console.log(info.birthDate); // Date(1995, 7, 15) - Aug 15, 1995
288
+ * ```
289
+ *
290
+ * @example
291
+ * Invalid NIK returns null:
292
+ * ```typescript
293
+ * const info = parseNIK('invalid');
294
+ * console.log(info); // null
295
+ * ```
296
+ *
297
+ * @public
298
+ */
299
+ declare function parseNIK(nik: string): NIKInfo | null;
300
+
301
+ /**
302
+ * Formats a NIK with separators for better readability.
303
+ *
304
+ * Groups the NIK into logical segments: province, regency, district,
305
+ * year, month, day, and serial number.
306
+ *
307
+ * @param nik - The 16-digit NIK string to format
308
+ * @param separator - Character to use as separator
309
+ * @returns Formatted NIK string, or original string if invalid format
310
+ *
311
+ * @example
312
+ * Default separator (dash):
313
+ * ```typescript
314
+ * formatNIK('3201234567890123');
315
+ * // '32-01-23-45-67-89-0123'
316
+ * ```
317
+ *
318
+ * @example
319
+ * Custom separator:
320
+ * ```typescript
321
+ * formatNIK('3201234567890123', ' ');
322
+ * // '32 01 23 45 67 89 0123'
323
+ * ```
324
+ *
325
+ * @example
326
+ * Invalid NIK returns as-is:
327
+ * ```typescript
328
+ * formatNIK('1234');
329
+ * // '1234'
330
+ * ```
331
+ *
332
+ * @public
333
+ */
334
+ declare function formatNIK(nik: string, separator?: string): string;
335
+ /**
336
+ * Masks a NIK to protect privacy while keeping partial visibility.
337
+ *
338
+ * By default, shows the first 4 and last 4 digits, masking the middle 8.
339
+ * Optionally formats the masked NIK with separators.
340
+ *
341
+ * @param nik - The 16-digit NIK string to mask
342
+ * @param options - Masking configuration options
343
+ * @returns Masked NIK string, or original string if invalid format
344
+ *
345
+ * @example
346
+ * Default masking (first 4, last 4):
347
+ * ```typescript
348
+ * maskNIK('3201234567890123');
349
+ * // '3201********0123'
350
+ * ```
351
+ *
352
+ * @example
353
+ * Custom mask character:
354
+ * ```typescript
355
+ * maskNIK('3201234567890123', { char: 'X' });
356
+ * // '3201XXXXXXXX0123'
357
+ * ```
358
+ *
359
+ * @example
360
+ * With separator:
361
+ * ```typescript
362
+ * maskNIK('3201234567890123', { separator: '-' });
363
+ * // '32-01-**-**-**-**-0123'
364
+ * ```
365
+ *
366
+ * @example
367
+ * Custom start and end:
368
+ * ```typescript
369
+ * maskNIK('3201234567890123', { start: 6, end: 4 });
370
+ * // '320123******0123'
371
+ * ```
372
+ *
373
+ * @public
374
+ */
375
+ declare function maskNIK(nik: string, options?: MaskOptions): string;
376
+
377
+ /**
378
+ * Calculates the age of a person based on their NIK.
379
+ *
380
+ * Returns detailed age breakdown with years, months, and days, or a
381
+ * formatted Indonesian string. This is consistent with `datetime.getAge()`.
382
+ *
383
+ * @param nik - The 16-digit NIK string
384
+ * @param options - Options object with `referenceDate` and `asString`
385
+ * @returns Age object `{ years, months, days }`, formatted string, or null if invalid
386
+ *
387
+ * @example
388
+ * ```typescript
389
+ * // Returns object by default
390
+ * getAge('3201018901310123');
391
+ * // { years: 35, months: 2, days: 6 } (as of 2026-04-06)
392
+ *
393
+ * // Returns formatted string
394
+ * getAge('3201018901310123', { asString: true });
395
+ * // '35 Tahun 2 Bulan 6 Hari'
396
+ *
397
+ * // Custom reference date
398
+ * getAge('3201018901310123', { referenceDate: new Date('2025-01-01') });
399
+ * // { years: 35, months: 11, days: 1 }
400
+ * ```
401
+ *
402
+ * @public
403
+ */
404
+ declare function getAge(nik: string, options?: GetAgeOptions): Age | string | null;
405
+ /**
406
+ * Compares two NIKs to check if they belong to the same person.
407
+ *
408
+ * Two NIKs are considered the same person if they have identical:
409
+ * - Province code (positions 1-2)
410
+ * - Regency code (positions 3-4)
411
+ * - District code (positions 5-6)
412
+ * - Birth date (year + month + day)
413
+ * - Gender (derived from day encoding)
414
+ * - Serial number (positions 13-16)
415
+ *
416
+ * @param nik1 - First NIK (accepts any format)
417
+ * @param nik2 - Second NIK (accepts any format)
418
+ * @returns True if both NIKs belong to the same person, false otherwise
419
+ *
420
+ * @example
421
+ * ```typescript
422
+ * // Same person, same format
423
+ * compareNIK('3201018901310123', '3201018901310123'); // true
424
+ *
425
+ * // Same person, different format
426
+ * compareNIK('3201018901310123', '3201-01-89-01-31-0123'); // true
427
+ *
428
+ * // Different serial number
429
+ * compareNIK('3201018901310123', '3201018901310124'); // false
430
+ *
431
+ * // Invalid NIK
432
+ * compareNIK('invalid', '3201018901310123'); // false
433
+ * ```
434
+ *
435
+ * @public
436
+ */
437
+ declare function compareNIK(nik1: string, nik2: string): boolean;
438
+ /**
439
+ * Checks if a person is an adult based on their NIK.
440
+ *
441
+ * By default, uses 17 years as the threshold (Indonesian KTP eligibility age).
442
+ * Indonesian law allows KTP at age 17, or upon marriage, or already married.
443
+ *
444
+ * @param nik - The 16-digit NIK string
445
+ * @param minAge - Minimum age threshold (default: 17)
446
+ * @returns True if the person is at least minAge years old, false otherwise
447
+ *
448
+ * @example
449
+ * ```typescript
450
+ * // Born 1995-01-01, reference 2026-04-06 (age 31)
451
+ * isAdult('3201950101950123'); // true (31 >= 17)
452
+ * isAdult('3201950101950123', 21); // true (31 >= 21)
453
+ *
454
+ * // Born 2010-01-01, reference 2026-04-06 (age 16)
455
+ * isAdult('3210010101950123'); // false (16 < 17)
456
+ * isAdult('3210010101950123', 16); // true (16 >= 16)
457
+ *
458
+ * // Invalid NIK
459
+ * isAdult('invalid'); // false
460
+ * ```
461
+ *
462
+ * @public
463
+ */
464
+ declare function isAdult(nik: string, minAge?: number): boolean;
465
+ /**
466
+ * Formats the birth date from a NIK into a human-readable string.
467
+ *
468
+ * @param nik - The 16-digit NIK string
469
+ * @param locale - The locale to use for formatting (default: 'id-ID')
470
+ * @returns Formatted birth date string, or null if invalid
471
+ *
472
+ * @example
473
+ * ```typescript
474
+ * formatBirthDate('3201018901310123'); // '31 Januari 1989'
475
+ * ```
476
+ */
477
+ declare function formatBirthDate(nik: string): string | null;
478
+ /**
479
+ * Checks if a NIK matches a specific gender.
480
+ *
481
+ * @param nik - The 16-digit NIK string
482
+ * @param gender - The gender to check ('male' | 'female')
483
+ * @returns True if the NIK matches the gender, false otherwise
484
+ */
485
+ declare function isValidForGender(nik: string, gender: 'male' | 'female'): boolean;
486
+ /**
487
+ * Checks if a NIK matches a specific birth date.
488
+ *
489
+ * @param nik - The 16-digit NIK string
490
+ * @param birthDate - The birth date to check
491
+ * @returns True if the NIK matches the birth date, false otherwise
492
+ */
493
+ declare function isValidForBirthDate(nik: string, birthDate: Date): boolean;
494
+
495
+ export { type Age as A, type GetAgeOptions as G, InvalidNIKError as I, type MaskOptions as M, type NIKInfo as N, formatBirthDate as a, isValidForBirthDate as b, type NIKValidationResult as c, compareNIK as d, isAdult as e, formatNIK as f, getAge as g, type NIKValidationError as h, isValidForGender as i, type NIKErrorCode as j, maskNIK as m, parseNIK as p, validateNIK as v };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indodev/toolkit",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Indonesian developer utilities for validation, formatting, and more",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",