@indodev/toolkit 0.4.0 → 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.
@@ -0,0 +1,542 @@
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
+ * Indonesian mobile operator names.
86
+ *
87
+ * @public
88
+ */
89
+ type OperatorName = 'Telkomsel' | 'XL' | 'Indosat' | 'Smartfren' | 'Axis';
90
+ /**
91
+ * Format types for Indonesian phone numbers.
92
+ *
93
+ * - `international`: +62 format with spaces (e.g., '+62 812-3456-7890')
94
+ * - `national`: 08xx format with dashes (e.g., '0812-3456-7890')
95
+ * - `e164`: International format without spaces (e.g., '6281234567890')
96
+ * - `display`: Formatted for display with separators (same as national)
97
+ *
98
+ * @public
99
+ */
100
+ type PhoneFormat = 'international' | 'national' | 'e164' | 'display';
101
+ /**
102
+ * Information extracted from a valid Indonesian phone number.
103
+ *
104
+ * Contains parsed data including country code, operator, formatted variants,
105
+ * and validation status.
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const info: PhoneInfo = {
110
+ * countryCode: '62',
111
+ * operator: 'Telkomsel',
112
+ * number: '81234567890',
113
+ * formatted: {
114
+ * international: '+62 812-3456-7890',
115
+ * national: '0812-3456-7890',
116
+ * e164: '6281234567890',
117
+ * },
118
+ * isValid: true,
119
+ * isMobile: true,
120
+ * isLandline: false,
121
+ * };
122
+ * ```
123
+ *
124
+ * @public
125
+ */
126
+ interface PhoneInfo {
127
+ /**
128
+ * Country code (always '62' for Indonesia).
129
+ */
130
+ countryCode: string;
131
+ /**
132
+ * Mobile operator name, or `null` if not detected.
133
+ *
134
+ * Possible values: 'Telkomsel', 'XL', 'Indosat', 'Smartfren', 'Axis'
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * 'Telkomsel' // for 0812, 0813, 0821, 0822, 0851, 0852, 0853
139
+ * 'XL' // for 0817, 0818, 0819, 0859, 0877, 0878
140
+ * null // if operator cannot be determined
141
+ * ```
142
+ */
143
+ operator: OperatorName | null;
144
+ /**
145
+ * Raw phone number without country code or leading zero.
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * '81234567890' // from '081234567890' or '+6281234567890'
150
+ * ```
151
+ */
152
+ number: string;
153
+ /**
154
+ * Phone number in various formatted styles.
155
+ */
156
+ formatted: {
157
+ /** International format with country code: '+62 812-3456-7890' */
158
+ international: string;
159
+ /** National format with leading zero: '0812-3456-7890' */
160
+ national: string;
161
+ /** E.164 format (no spaces/dashes): '6281234567890' */
162
+ e164: string;
163
+ };
164
+ /**
165
+ * Whether the phone number passed validation checks.
166
+ */
167
+ isValid: boolean;
168
+ /**
169
+ * Whether this is a mobile number (08xx).
170
+ */
171
+ isMobile: boolean;
172
+ /**
173
+ * Whether this is a landline number (02x, 04x, etc).
174
+ */
175
+ isLandline: boolean;
176
+ /**
177
+ * Region name for landline numbers, or `null` for mobile.
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * 'Jakarta' // for 021 area code
182
+ * 'Bandung' // for 022 area code
183
+ * null // for mobile numbers
184
+ * ```
185
+ */
186
+ region?: string | null;
187
+ }
188
+ /**
189
+ * Options for masking phone numbers.
190
+ *
191
+ * Controls how many digits to show at the start and end,
192
+ * what character to use for masking, and optional separators.
193
+ *
194
+ * @example
195
+ * Default masking:
196
+ * ```typescript
197
+ * { start: 4, end: 4, char: '*' }
198
+ * // '0812****7890'
199
+ * ```
200
+ *
201
+ * @example
202
+ * With separator:
203
+ * ```typescript
204
+ * { start: 4, end: 4, char: '*', separator: '-' }
205
+ * // '0812-****-7890'
206
+ * ```
207
+ *
208
+ * @public
209
+ */
210
+ interface MaskOptions {
211
+ /**
212
+ * Number of digits to show at the start.
213
+ *
214
+ * @defaultValue 4
215
+ */
216
+ start?: number;
217
+ /**
218
+ * Number of digits to show at the end.
219
+ *
220
+ * @defaultValue 4
221
+ */
222
+ end?: number;
223
+ /**
224
+ * Character to use for masking hidden digits.
225
+ *
226
+ * @defaultValue '*'
227
+ */
228
+ char?: string;
229
+ /**
230
+ * Optional separator to add between groups of digits.
231
+ *
232
+ * @defaultValue undefined
233
+ */
234
+ separator?: string;
235
+ }
236
+
237
+ /**
238
+ * Formats an Indonesian phone number to the specified format.
239
+ *
240
+ * Accepts various input formats and converts to the desired output format.
241
+ * Automatically adds appropriate separators for readability.
242
+ *
243
+ * @param phone - The phone number to format
244
+ * @param format - Target format ('international', 'national', 'e164', 'display')
245
+ * @returns Formatted phone number, or original string if invalid
246
+ *
247
+ * @example
248
+ * International format:
249
+ * ```typescript
250
+ * formatPhoneNumber('081234567890', 'international');
251
+ * // '+62 812-3456-7890'
252
+ * ```
253
+ *
254
+ * @example
255
+ * National format:
256
+ * ```typescript
257
+ * formatPhoneNumber('+6281234567890', 'national');
258
+ * // '0812-3456-7890'
259
+ * ```
260
+ *
261
+ * @example
262
+ * E.164 format (no spaces/dashes):
263
+ * ```typescript
264
+ * formatPhoneNumber('0812-3456-7890', 'e164');
265
+ * // '6281234567890'
266
+ * ```
267
+ *
268
+ * @public
269
+ */
270
+ declare function formatPhoneNumber(phone: string, format?: PhoneFormat): string;
271
+ /**
272
+ * Converts a phone number to international format (+62 xxx-xxxx-xxxx).
273
+ *
274
+ * @param phone - The phone number to convert
275
+ * @returns Phone number in international format with separators
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * toInternational('081234567890');
280
+ * // '+62 812-3456-7890'
281
+ * ```
282
+ *
283
+ * @example
284
+ * Already international:
285
+ * ```typescript
286
+ * toInternational('+6281234567890');
287
+ * // '+62 812-3456-7890'
288
+ * ```
289
+ *
290
+ * @public
291
+ */
292
+ declare function toInternational(phone: string): string;
293
+ /**
294
+ * Converts a phone number to national format (08xx-xxxx-xxxx).
295
+ *
296
+ * @param phone - The phone number to convert
297
+ * @returns Phone number in national format with dashes
298
+ *
299
+ * @example
300
+ * ```typescript
301
+ * toNational('+6281234567890');
302
+ * // '0812-3456-7890'
303
+ * ```
304
+ *
305
+ * @example
306
+ * Already national:
307
+ * ```typescript
308
+ * toNational('081234567890');
309
+ * // '0812-3456-7890'
310
+ * ```
311
+ *
312
+ * @public
313
+ */
314
+ declare function toNational(phone: string): string;
315
+ /**
316
+ * Converts a phone number to E.164 format (6281234567890).
317
+ *
318
+ * E.164 is the international standard format without spaces or dashes.
319
+ * Suitable for API calls and database storage.
320
+ *
321
+ * @param phone - The phone number to convert
322
+ * @returns Phone number in E.164 format
323
+ *
324
+ * @example
325
+ * ```typescript
326
+ * toE164('0812-3456-7890');
327
+ * // '6281234567890'
328
+ * ```
329
+ *
330
+ * @example
331
+ * From international format:
332
+ * ```typescript
333
+ * toE164('+62 812-3456-7890');
334
+ * // '6281234567890'
335
+ * ```
336
+ *
337
+ * @public
338
+ */
339
+ declare function toE164(phone: string): string;
340
+ /**
341
+ * Removes all non-digit characters from a phone number, preserving leading +.
342
+ *
343
+ * @param phone - The phone number to clean
344
+ * @returns Cleaned phone number with only digits (and optional leading +)
345
+ *
346
+ * @example
347
+ * ```typescript
348
+ * cleanPhoneNumber('0812-3456-7890');
349
+ * // '081234567890'
350
+ * ```
351
+ *
352
+ * @example
353
+ * ```typescript
354
+ * cleanPhoneNumber('+62 812 3456 7890');
355
+ * // '+6281234567890'
356
+ * ```
357
+ *
358
+ * @public
359
+ */
360
+ declare function cleanPhoneNumber(phone: string): string;
361
+ /**
362
+ * Masks a phone number for privacy protection.
363
+ *
364
+ * By default, shows the first 4 and last 4 digits, masking the middle digits.
365
+ * Optionally formats with separators.
366
+ *
367
+ * @param phone - The phone number to mask
368
+ * @param options - Masking configuration options
369
+ * @returns Masked phone number, or original string if invalid
370
+ *
371
+ * @example
372
+ * Default masking:
373
+ * ```typescript
374
+ * maskPhoneNumber('081234567890');
375
+ * // '0812****7890'
376
+ * ```
377
+ *
378
+ * @example
379
+ * Custom mask character:
380
+ * ```typescript
381
+ * maskPhoneNumber('081234567890', { char: 'X' });
382
+ * // '0812XXXX7890'
383
+ * ```
384
+ *
385
+ * @example
386
+ * With separator:
387
+ * ```typescript
388
+ * maskPhoneNumber('081234567890', { separator: '-' });
389
+ * // '0812-****-7890'
390
+ * ```
391
+ *
392
+ * @public
393
+ */
394
+ declare function maskPhoneNumber(phone: string, options?: MaskOptions): string;
395
+
396
+ /**
397
+ * Generates a WhatsApp click-to-chat link.
398
+ *
399
+ * @param phone - The Indonesian phone number
400
+ * @param message - Optional pre-filled message
401
+ * @returns WhatsApp link, or empty string if phone is invalid
402
+ *
403
+ * @example
404
+ * ```typescript
405
+ * generateWALink('081234567890', 'Halo!');
406
+ * // 'https://wa.me/6281234567890?text=Halo%21'
407
+ * ```
408
+ *
409
+ * @public
410
+ */
411
+ declare function generateWALink(phone: string, message?: string): string;
412
+ /**
413
+ * Generates an SMS link (sms:).
414
+ *
415
+ * @param phone - The Indonesian phone number
416
+ * @param body - Optional SMS body
417
+ * @returns SMS link, or empty string if phone is invalid
418
+ *
419
+ * @example
420
+ * ```typescript
421
+ * generateSmsLink('081234567890', 'Pesan ini');
422
+ * // 'sms:+6281234567890?body=Pesan%20ini'
423
+ * ```
424
+ *
425
+ * @public
426
+ */
427
+ declare function generateSmsLink(phone: string, body?: string): string;
428
+ /**
429
+ * Generates a telephone link (tel:).
430
+ *
431
+ * @param phone - The Indonesian phone number
432
+ * @returns Tel link, or empty string if phone is invalid
433
+ *
434
+ * @example
435
+ * ```typescript
436
+ * generateTelLink('081234567890');
437
+ * // 'tel:+6281234567890'
438
+ * ```
439
+ *
440
+ * @public
441
+ */
442
+ declare function generateTelLink(phone: string): string;
443
+
444
+ /**
445
+ * Parses an Indonesian phone number and extracts all information.
446
+ *
447
+ * Extracts country code, operator, formatted variants, and determines
448
+ * if it's a mobile or landline number.
449
+ *
450
+ * @param phone - The phone number to parse
451
+ * @returns Parsed phone information, or `null` if invalid
452
+ *
453
+ * @example
454
+ * Parse a mobile number:
455
+ * ```typescript
456
+ * const info = parsePhoneNumber('081234567890');
457
+ * console.log(info);
458
+ * // {
459
+ * // countryCode: '62',
460
+ * // operator: 'Telkomsel',
461
+ * // number: '81234567890',
462
+ * // formatted: {
463
+ * // international: '+62 812-3456-7890',
464
+ * // national: '0812-3456-7890',
465
+ * // e164: '6281234567890'
466
+ * // },
467
+ * // isValid: true,
468
+ * // isMobile: true,
469
+ * // isLandline: false
470
+ * // }
471
+ * ```
472
+ *
473
+ * @example
474
+ * Parse with different input format:
475
+ * ```typescript
476
+ * const info = parsePhoneNumber('+62 812-3456-7890');
477
+ * console.log(info.operator); // 'Telkomsel'
478
+ * console.log(info.formatted.national); // '0812-3456-7890'
479
+ * ```
480
+ *
481
+ * @example
482
+ * Parse a landline:
483
+ * ```typescript
484
+ * const info = parsePhoneNumber('0212345678');
485
+ * console.log(info.region); // 'Jakarta'
486
+ * console.log(info.isLandline); // true
487
+ * ```
488
+ *
489
+ * @public
490
+ */
491
+ declare function parsePhoneNumber(phone: string): PhoneInfo | null;
492
+ /**
493
+ * Detects the mobile operator from a phone number.
494
+ *
495
+ * Identifies the operator based on the phone number prefix.
496
+ * Returns `null` if the operator cannot be determined or if it's not a mobile number.
497
+ *
498
+ * @param phone - The phone number to check
499
+ * @returns Operator name, or `null` if not detected
500
+ *
501
+ * @example
502
+ * Telkomsel numbers:
503
+ * ```typescript
504
+ * getOperator('081234567890'); // 'Telkomsel'
505
+ * getOperator('0812-3456-7890'); // 'Telkomsel'
506
+ * getOperator('+6281234567890'); // 'Telkomsel'
507
+ * ```
508
+ *
509
+ * @example
510
+ * XL numbers:
511
+ * ```typescript
512
+ * getOperator('081734567890'); // 'XL'
513
+ * ```
514
+ *
515
+ * @example
516
+ * Non-mobile or invalid:
517
+ * ```typescript
518
+ * getOperator('0212345678'); // null (landline)
519
+ * getOperator('1234'); // null (invalid)
520
+ * ```
521
+ *
522
+ * @public
523
+ */
524
+ declare function getOperator(phone: string): OperatorName | null;
525
+ /**
526
+ * Checks if a phone number belongs to a specific provider.
527
+ *
528
+ * @param phone - The phone number to check
529
+ * @param providerName - The provider name to match (case-insensitive)
530
+ * @returns `true` if it matches, `false` otherwise
531
+ *
532
+ * @example
533
+ * ```typescript
534
+ * isProvider('081234567890', 'Telkomsel'); // true
535
+ * isProvider('081734567890', 'xl'); // true
536
+ * ```
537
+ *
538
+ * @public
539
+ */
540
+ declare function isProvider(phone: string, providerName: string): boolean;
541
+
542
+ export { type MaskOptions as M, type OperatorName as O, type PhoneFormat as P, isLandlineNumber as a, toNational as b, toE164 as c, cleanPhoneNumber as d, generateWALink as e, formatPhoneNumber as f, getOperator as g, generateSmsLink as h, isMobileNumber as i, generateTelLink as j, isProvider as k, type PhoneInfo as l, maskPhoneNumber as m, parsePhoneNumber as p, toInternational as t, validatePhoneNumber as v };