@indodev/toolkit 0.0.1

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,472 @@
1
+ /**
2
+ * Validates an Indonesian phone number format.
3
+ *
4
+ * Accepts multiple input formats:
5
+ * - National format: 08xx-xxxx-xxxx or 08xxxxxxxxxx
6
+ * - International with +: +62 8xx-xxxx-xxxx or +628xxxxxxxxxx
7
+ * - International without +: 62 8xx-xxxx-xxxx or 628xxxxxxxxxx
8
+ *
9
+ * For mobile numbers, validates:
10
+ * - Starts with 08 (national) or 628 (international)
11
+ * - Has valid operator prefix (0811, 0812, 0817, etc.)
12
+ * - Total length is 10-13 digits (after removing non-digits)
13
+ *
14
+ * For landline numbers, validates:
15
+ * - Starts with 0 followed by area code (021, 022, etc.)
16
+ * - Total length is appropriate for landline
17
+ *
18
+ * @param phone - The phone number string to validate
19
+ * @returns `true` if the phone number is valid, `false` otherwise
20
+ *
21
+ * @example
22
+ * Valid mobile number (national):
23
+ * ```typescript
24
+ * validatePhoneNumber('081234567890'); // true
25
+ * ```
26
+ *
27
+ * @example
28
+ * Valid mobile number (international):
29
+ * ```typescript
30
+ * validatePhoneNumber('+6281234567890'); // true
31
+ * validatePhoneNumber('6281234567890'); // true
32
+ * ```
33
+ *
34
+ * @example
35
+ * With separators:
36
+ * ```typescript
37
+ * validatePhoneNumber('0812-3456-7890'); // true
38
+ * validatePhoneNumber('+62 812-3456-7890'); // true
39
+ * ```
40
+ *
41
+ * @example
42
+ * Invalid numbers:
43
+ * ```typescript
44
+ * validatePhoneNumber('1234'); // false - too short
45
+ * validatePhoneNumber('08001234567'); // false - invalid prefix
46
+ * validatePhoneNumber('+1234567890'); // false - wrong country code
47
+ * ```
48
+ *
49
+ * @public
50
+ */
51
+ declare function validatePhoneNumber(phone: string): boolean;
52
+ /**
53
+ * Checks if a phone number is a mobile number.
54
+ *
55
+ * @param phone - The phone number to check
56
+ * @returns `true` if it's a mobile number (08xx), `false` otherwise
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * isMobileNumber('081234567890'); // true
61
+ * isMobileNumber('+6281234567890'); // true
62
+ * isMobileNumber('0212345678'); // false (landline)
63
+ * ```
64
+ *
65
+ * @public
66
+ */
67
+ declare function isMobileNumber(phone: string): boolean;
68
+ /**
69
+ * Checks if a phone number is a landline number.
70
+ *
71
+ * @param phone - The phone number to check
72
+ * @returns `true` if it's a landline number, `false` otherwise
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * isLandlineNumber('0212345678'); // true
77
+ * isLandlineNumber('081234567890'); // false (mobile)
78
+ * ```
79
+ *
80
+ * @public
81
+ */
82
+ declare function isLandlineNumber(phone: string): boolean;
83
+
84
+ /**
85
+ * Format types for Indonesian phone numbers.
86
+ *
87
+ * - `international`: +62 format with spaces (e.g., '+62 812-3456-7890')
88
+ * - `national`: 08xx format with dashes (e.g., '0812-3456-7890')
89
+ * - `e164`: International format without spaces (e.g., '6281234567890')
90
+ * - `display`: Formatted for display with separators (same as national)
91
+ *
92
+ * @public
93
+ */
94
+ type PhoneFormat = 'international' | 'national' | 'e164' | 'display';
95
+ /**
96
+ * Information extracted from a valid Indonesian phone number.
97
+ *
98
+ * Contains parsed data including country code, operator, formatted variants,
99
+ * and validation status.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const info: PhoneInfo = {
104
+ * countryCode: '62',
105
+ * operator: 'Telkomsel',
106
+ * number: '81234567890',
107
+ * formatted: {
108
+ * international: '+62 812-3456-7890',
109
+ * national: '0812-3456-7890',
110
+ * e164: '6281234567890',
111
+ * },
112
+ * isValid: true,
113
+ * isMobile: true,
114
+ * isLandline: false,
115
+ * };
116
+ * ```
117
+ *
118
+ * @public
119
+ */
120
+ interface PhoneInfo {
121
+ /**
122
+ * Country code (always '62' for Indonesia).
123
+ */
124
+ countryCode: string;
125
+ /**
126
+ * Mobile operator name, or `null` if not detected.
127
+ *
128
+ * Possible values: 'Telkomsel', 'XL', 'Indosat', 'Tri', 'Smartfren', 'Axis'
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * 'Telkomsel' // for 0812, 0813, 0821, 0822, 0851, 0852, 0853
133
+ * 'XL' // for 0817, 0818, 0819, 0859, 0877, 0878
134
+ * null // if operator cannot be determined
135
+ * ```
136
+ */
137
+ operator: string | null;
138
+ /**
139
+ * Raw phone number without country code or leading zero.
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * '81234567890' // from '081234567890' or '+6281234567890'
144
+ * ```
145
+ */
146
+ number: string;
147
+ /**
148
+ * Phone number in various formatted styles.
149
+ */
150
+ formatted: {
151
+ /** International format with country code: '+62 812-3456-7890' */
152
+ international: string;
153
+ /** National format with leading zero: '0812-3456-7890' */
154
+ national: string;
155
+ /** E.164 format (no spaces/dashes): '6281234567890' */
156
+ e164: string;
157
+ };
158
+ /**
159
+ * Whether the phone number passed validation checks.
160
+ */
161
+ isValid: boolean;
162
+ /**
163
+ * Whether this is a mobile number (08xx).
164
+ */
165
+ isMobile: boolean;
166
+ /**
167
+ * Whether this is a landline number (02x, 04x, etc).
168
+ */
169
+ isLandline: boolean;
170
+ /**
171
+ * Region name for landline numbers, or `null` for mobile.
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * 'Jakarta' // for 021 area code
176
+ * 'Bandung' // for 022 area code
177
+ * null // for mobile numbers
178
+ * ```
179
+ */
180
+ region?: string | null;
181
+ }
182
+ /**
183
+ * Options for masking phone numbers.
184
+ *
185
+ * Controls how many digits to show at the start and end,
186
+ * what character to use for masking, and optional separators.
187
+ *
188
+ * @example
189
+ * Default masking:
190
+ * ```typescript
191
+ * { start: 4, end: 4, char: '*' }
192
+ * // '0812****7890'
193
+ * ```
194
+ *
195
+ * @example
196
+ * With separator:
197
+ * ```typescript
198
+ * { start: 4, end: 4, char: '*', separator: '-' }
199
+ * // '0812-****-7890'
200
+ * ```
201
+ *
202
+ * @public
203
+ */
204
+ interface MaskOptions {
205
+ /**
206
+ * Number of digits to show at the start.
207
+ *
208
+ * @defaultValue 4
209
+ */
210
+ start?: number;
211
+ /**
212
+ * Number of digits to show at the end.
213
+ *
214
+ * @defaultValue 4
215
+ */
216
+ end?: number;
217
+ /**
218
+ * Character to use for masking hidden digits.
219
+ *
220
+ * @defaultValue '*'
221
+ */
222
+ char?: string;
223
+ /**
224
+ * Optional separator to add between groups of digits.
225
+ *
226
+ * @defaultValue undefined
227
+ */
228
+ separator?: string;
229
+ }
230
+
231
+ /**
232
+ * Formats an Indonesian phone number to the specified format.
233
+ *
234
+ * Accepts various input formats and converts to the desired output format.
235
+ * Automatically adds appropriate separators for readability.
236
+ *
237
+ * @param phone - The phone number to format
238
+ * @param format - Target format ('international', 'national', 'e164', 'display')
239
+ * @returns Formatted phone number, or original string if invalid
240
+ *
241
+ * @example
242
+ * International format:
243
+ * ```typescript
244
+ * formatPhoneNumber('081234567890', 'international');
245
+ * // '+62 812-3456-7890'
246
+ * ```
247
+ *
248
+ * @example
249
+ * National format:
250
+ * ```typescript
251
+ * formatPhoneNumber('+6281234567890', 'national');
252
+ * // '0812-3456-7890'
253
+ * ```
254
+ *
255
+ * @example
256
+ * E.164 format (no spaces/dashes):
257
+ * ```typescript
258
+ * formatPhoneNumber('0812-3456-7890', 'e164');
259
+ * // '6281234567890'
260
+ * ```
261
+ *
262
+ * @public
263
+ */
264
+ declare function formatPhoneNumber(phone: string, format?: PhoneFormat): string;
265
+ /**
266
+ * Converts a phone number to international format (+62 xxx-xxxx-xxxx).
267
+ *
268
+ * @param phone - The phone number to convert
269
+ * @returns Phone number in international format with separators
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * toInternational('081234567890');
274
+ * // '+62 812-3456-7890'
275
+ * ```
276
+ *
277
+ * @example
278
+ * Already international:
279
+ * ```typescript
280
+ * toInternational('+6281234567890');
281
+ * // '+62 812-3456-7890'
282
+ * ```
283
+ *
284
+ * @public
285
+ */
286
+ declare function toInternational(phone: string): string;
287
+ /**
288
+ * Converts a phone number to national format (08xx-xxxx-xxxx).
289
+ *
290
+ * @param phone - The phone number to convert
291
+ * @returns Phone number in national format with dashes
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * toNational('+6281234567890');
296
+ * // '0812-3456-7890'
297
+ * ```
298
+ *
299
+ * @example
300
+ * Already national:
301
+ * ```typescript
302
+ * toNational('081234567890');
303
+ * // '0812-3456-7890'
304
+ * ```
305
+ *
306
+ * @public
307
+ */
308
+ declare function toNational(phone: string): string;
309
+ /**
310
+ * Converts a phone number to E.164 format (6281234567890).
311
+ *
312
+ * E.164 is the international standard format without spaces or dashes.
313
+ * Suitable for API calls and database storage.
314
+ *
315
+ * @param phone - The phone number to convert
316
+ * @returns Phone number in E.164 format
317
+ *
318
+ * @example
319
+ * ```typescript
320
+ * toE164('0812-3456-7890');
321
+ * // '6281234567890'
322
+ * ```
323
+ *
324
+ * @example
325
+ * From international format:
326
+ * ```typescript
327
+ * toE164('+62 812-3456-7890');
328
+ * // '6281234567890'
329
+ * ```
330
+ *
331
+ * @public
332
+ */
333
+ declare function toE164(phone: string): string;
334
+ /**
335
+ * Removes all non-digit characters from a phone number, preserving leading +.
336
+ *
337
+ * @param phone - The phone number to clean
338
+ * @returns Cleaned phone number with only digits (and optional leading +)
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * cleanPhoneNumber('0812-3456-7890');
343
+ * // '081234567890'
344
+ * ```
345
+ *
346
+ * @example
347
+ * ```typescript
348
+ * cleanPhoneNumber('+62 812 3456 7890');
349
+ * // '+6281234567890'
350
+ * ```
351
+ *
352
+ * @public
353
+ */
354
+ declare function cleanPhoneNumber(phone: string): string;
355
+ /**
356
+ * Masks a phone number for privacy protection.
357
+ *
358
+ * By default, shows the first 4 and last 4 digits, masking the middle digits.
359
+ * Optionally formats with separators.
360
+ *
361
+ * @param phone - The phone number to mask
362
+ * @param options - Masking configuration options
363
+ * @returns Masked phone number, or original string if invalid
364
+ *
365
+ * @example
366
+ * Default masking:
367
+ * ```typescript
368
+ * maskPhoneNumber('081234567890');
369
+ * // '0812****7890'
370
+ * ```
371
+ *
372
+ * @example
373
+ * Custom mask character:
374
+ * ```typescript
375
+ * maskPhoneNumber('081234567890', { char: 'X' });
376
+ * // '0812XXXX7890'
377
+ * ```
378
+ *
379
+ * @example
380
+ * With separator:
381
+ * ```typescript
382
+ * maskPhoneNumber('081234567890', { separator: '-' });
383
+ * // '0812-****-7890'
384
+ * ```
385
+ *
386
+ * @public
387
+ */
388
+ declare function maskPhoneNumber(phone: string, options?: MaskOptions): string;
389
+
390
+ /**
391
+ * Parses an Indonesian phone number and extracts all information.
392
+ *
393
+ * Extracts country code, operator, formatted variants, and determines
394
+ * if it's a mobile or landline number.
395
+ *
396
+ * @param phone - The phone number to parse
397
+ * @returns Parsed phone information, or `null` if invalid
398
+ *
399
+ * @example
400
+ * Parse a mobile number:
401
+ * ```typescript
402
+ * const info = parsePhoneNumber('081234567890');
403
+ * console.log(info);
404
+ * // {
405
+ * // countryCode: '62',
406
+ * // operator: 'Telkomsel',
407
+ * // number: '81234567890',
408
+ * // formatted: {
409
+ * // international: '+62 812-3456-7890',
410
+ * // national: '0812-3456-7890',
411
+ * // e164: '6281234567890'
412
+ * // },
413
+ * // isValid: true,
414
+ * // isMobile: true,
415
+ * // isLandline: false
416
+ * // }
417
+ * ```
418
+ *
419
+ * @example
420
+ * Parse with different input format:
421
+ * ```typescript
422
+ * const info = parsePhoneNumber('+62 812-3456-7890');
423
+ * console.log(info.operator); // 'Telkomsel'
424
+ * console.log(info.formatted.national); // '0812-3456-7890'
425
+ * ```
426
+ *
427
+ * @example
428
+ * Parse a landline:
429
+ * ```typescript
430
+ * const info = parsePhoneNumber('0212345678');
431
+ * console.log(info.region); // 'Jakarta'
432
+ * console.log(info.isLandline); // true
433
+ * ```
434
+ *
435
+ * @public
436
+ */
437
+ declare function parsePhoneNumber(phone: string): PhoneInfo | null;
438
+ /**
439
+ * Detects the mobile operator from a phone number.
440
+ *
441
+ * Identifies the operator based on the phone number prefix.
442
+ * Returns `null` if the operator cannot be determined or if it's not a mobile number.
443
+ *
444
+ * @param phone - The phone number to check
445
+ * @returns Operator name, or `null` if not detected
446
+ *
447
+ * @example
448
+ * Telkomsel numbers:
449
+ * ```typescript
450
+ * getOperator('081234567890'); // 'Telkomsel'
451
+ * getOperator('0812-3456-7890'); // 'Telkomsel'
452
+ * getOperator('+6281234567890'); // 'Telkomsel'
453
+ * ```
454
+ *
455
+ * @example
456
+ * XL numbers:
457
+ * ```typescript
458
+ * getOperator('081734567890'); // 'XL'
459
+ * ```
460
+ *
461
+ * @example
462
+ * Non-mobile or invalid:
463
+ * ```typescript
464
+ * getOperator('0212345678'); // null (landline)
465
+ * getOperator('1234'); // null (invalid)
466
+ * ```
467
+ *
468
+ * @public
469
+ */
470
+ declare function getOperator(phone: string): string | null;
471
+
472
+ export { type MaskOptions, type PhoneFormat, type PhoneInfo, cleanPhoneNumber, formatPhoneNumber, getOperator, isLandlineNumber, isMobileNumber, maskPhoneNumber, parsePhoneNumber, toE164, toInternational, toNational, validatePhoneNumber };