@indodev/toolkit 0.3.2 → 0.3.4

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,485 @@
1
+ /**
2
+ * Currency module types for Indonesian Rupiah utilities.
3
+ *
4
+ * @module currency/types
5
+ * @packageDocumentation
6
+ */
7
+ /**
8
+ * Options for formatting Rupiah currency.
9
+ *
10
+ * @example
11
+ * Default formatting:
12
+ * ```typescript
13
+ * const options: RupiahOptions = {
14
+ * symbol: true,
15
+ * decimal: false,
16
+ * separator: '.',
17
+ * };
18
+ * formatRupiah(1500000, options); // 'Rp 1.500.000'
19
+ * ```
20
+ *
21
+ * @example
22
+ * With decimals:
23
+ * ```typescript
24
+ * const options: RupiahOptions = {
25
+ * symbol: true,
26
+ * decimal: true,
27
+ * precision: 2,
28
+ * };
29
+ * formatRupiah(1500000.50, options); // 'Rp 1.500.000,50'
30
+ * ```
31
+ *
32
+ * @public
33
+ */
34
+ interface RupiahOptions {
35
+ /**
36
+ * Whether to show 'Rp' symbol.
37
+ *
38
+ * @defaultValue true
39
+ */
40
+ symbol?: boolean;
41
+ /**
42
+ * Whether to show decimal places.
43
+ *
44
+ * @defaultValue false
45
+ */
46
+ decimal?: boolean;
47
+ /**
48
+ * Thousands separator character.
49
+ *
50
+ * @defaultValue '.'
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * '.' // Indonesian standard
55
+ * ',' // International standard
56
+ * ' ' // Space separator
57
+ * ```
58
+ */
59
+ separator?: string;
60
+ /**
61
+ * Decimal separator character.
62
+ *
63
+ * @defaultValue ','
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * ',' // Indonesian standard
68
+ * '.' // International standard
69
+ * ```
70
+ */
71
+ decimalSeparator?: string;
72
+ /**
73
+ * Number of decimal places to show.
74
+ *
75
+ * @defaultValue 0
76
+ */
77
+ precision?: number;
78
+ /**
79
+ * Whether to add space after 'Rp' symbol.
80
+ *
81
+ * @defaultValue true
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * true // 'Rp 1.500.000'
86
+ * false // 'Rp1.500.000'
87
+ * ```
88
+ */
89
+ spaceAfterSymbol?: boolean;
90
+ }
91
+ /**
92
+ * Options for converting numbers to Indonesian words (terbilang).
93
+ *
94
+ * @example
95
+ * Default:
96
+ * ```typescript
97
+ * toWords(1500000); // 'satu juta lima ratus ribu rupiah'
98
+ * ```
99
+ *
100
+ * @example
101
+ * Uppercase:
102
+ * ```typescript
103
+ * toWords(1500000, { uppercase: true });
104
+ * // 'Satu juta lima ratus ribu rupiah'
105
+ * ```
106
+ *
107
+ * @example
108
+ * Without currency suffix:
109
+ * ```typescript
110
+ * toWords(1500000, { withCurrency: false });
111
+ * // 'satu juta lima ratus ribu'
112
+ * ```
113
+ *
114
+ * @public
115
+ */
116
+ interface WordOptions {
117
+ /**
118
+ * Whether to capitalize the first letter.
119
+ *
120
+ * @defaultValue false
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * false // 'satu juta'
125
+ * true // 'Satu juta'
126
+ * ```
127
+ */
128
+ uppercase?: boolean;
129
+ /**
130
+ * Whether to add 'rupiah' at the end.
131
+ *
132
+ * @defaultValue true
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * true // 'satu juta rupiah'
137
+ * false // 'satu juta'
138
+ * ```
139
+ */
140
+ withCurrency?: boolean;
141
+ /**
142
+ * Whether to include decimal words with 'koma' separator.
143
+ *
144
+ * @defaultValue false
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * false // 'satu juta lima ratus ribu rupiah'
149
+ * true // 'satu juta lima ratus ribu rupiah koma lima puluh'
150
+ * ```
151
+ */
152
+ withDecimals?: boolean;
153
+ }
154
+ /**
155
+ * Unit for rounding currency amounts.
156
+ *
157
+ * Common Indonesian currency rounding units:
158
+ * - `'ribu'`: Round to thousands (1.000)
159
+ * - `'ratus-ribu'`: Round to hundred thousands (100.000)
160
+ * - `'juta'`: Round to millions (1.000.000)
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * roundToClean(1234567, 'ribu'); // 1235000
165
+ * roundToClean(1234567, 'ratus-ribu'); // 1200000
166
+ * roundToClean(1234567, 'juta'); // 1000000
167
+ * ```
168
+ *
169
+ * @public
170
+ */
171
+ type RoundUnit = 'ribu' | 'ratus-ribu' | 'juta';
172
+ /**
173
+ * Options for compact currency formatting.
174
+ *
175
+ * @example
176
+ * Default:
177
+ * ```typescript
178
+ * formatCompact(1500000); // 'Rp 1,5 juta'
179
+ * ```
180
+ *
181
+ * @example
182
+ * Without symbol:
183
+ * ```typescript
184
+ * formatCompact(1500000, { symbol: false }); // '1,5 juta'
185
+ * ```
186
+ *
187
+ * @public
188
+ */
189
+ interface CompactOptions {
190
+ /**
191
+ * Whether to show 'Rp' symbol.
192
+ *
193
+ * @defaultValue true
194
+ */
195
+ symbol?: boolean;
196
+ /**
197
+ * Whether to add space after 'Rp' symbol.
198
+ *
199
+ * @defaultValue true
200
+ */
201
+ spaceAfterSymbol?: boolean;
202
+ }
203
+ /**
204
+ * Options for splitting an amount into parts.
205
+ *
206
+ * @example
207
+ * Equal split:
208
+ * ```typescript
209
+ * splitAmount(1500000, 3); // [500000, 500000, 500000]
210
+ * ```
211
+ *
212
+ * @example
213
+ * Custom ratios:
214
+ * ```typescript
215
+ * splitAmount(1000000, 2, { ratios: [70, 30] }); // [700000, 300000]
216
+ * ```
217
+ *
218
+ * @public
219
+ */
220
+ interface SplitOptions {
221
+ /**
222
+ * Custom percentage ratios (must sum to 100).
223
+ * Length must match `parts` count.
224
+ */
225
+ ratios?: number[];
226
+ /**
227
+ * Round each part to a clean amount.
228
+ */
229
+ roundTo?: RoundUnit;
230
+ }
231
+
232
+ /**
233
+ * Currency formatting utilities for Indonesian Rupiah.
234
+ *
235
+ * @module currency/format
236
+ * @packageDocumentation
237
+ */
238
+
239
+ /**
240
+ * Formats a number as Indonesian Rupiah currency.
241
+ *
242
+ * Provides flexible formatting options including symbol display,
243
+ * decimal places, and custom separators.
244
+ *
245
+ * @param amount - The amount to format
246
+ * @param options - Formatting options
247
+ * @returns Formatted Rupiah string
248
+ *
249
+ * @example
250
+ * Basic formatting:
251
+ * ```typescript
252
+ * formatRupiah(1500000); // 'Rp 1.500.000'
253
+ * ```
254
+ *
255
+ * @example
256
+ * With decimals:
257
+ * ```typescript
258
+ * formatRupiah(1500000.50, { decimal: true }); // 'Rp 1.500.000,50'
259
+ * ```
260
+ *
261
+ * @example
262
+ * Without symbol:
263
+ * ```typescript
264
+ * formatRupiah(1500000, { symbol: false }); // '1.500.000'
265
+ * ```
266
+ *
267
+ * @example
268
+ * Custom separators:
269
+ * ```typescript
270
+ * formatRupiah(1500000, { separator: ',' }); // 'Rp 1,500,000'
271
+ * ```
272
+ *
273
+ * @public
274
+ */
275
+ declare function formatRupiah(amount: number, options?: RupiahOptions): string;
276
+ /**
277
+ * Formats a number in compact Indonesian format.
278
+ *
279
+ * Uses Indonesian units: ribu, juta, miliar, triliun.
280
+ * Follows Indonesian grammar rules (e.g., "1 juta" not "1,0 juta").
281
+ *
282
+ * @param amount - The amount to format
283
+ * @param options - Compact formatting options
284
+ * @returns Compact formatted string
285
+ *
286
+ * @example
287
+ * Millions:
288
+ * ```typescript
289
+ * formatCompact(1500000); // 'Rp 1,5 juta'
290
+ * formatCompact(1000000); // 'Rp 1 juta'
291
+ * ```
292
+ *
293
+ * @example
294
+ * Thousands:
295
+ * ```typescript
296
+ * formatCompact(500000); // 'Rp 500 ribu'
297
+ * ```
298
+ *
299
+ * @example
300
+ * Small numbers:
301
+ * ```typescript
302
+ * formatCompact(1500); // 'Rp 1.500'
303
+ * ```
304
+ *
305
+ * @example
306
+ * Without symbol:
307
+ * ```typescript
308
+ * formatCompact(1500000, { symbol: false }); // '1,5 juta'
309
+ * ```
310
+ *
311
+ * @public
312
+ */
313
+ declare function formatCompact(amount: number, options?: CompactOptions): string;
314
+
315
+ /**
316
+ * Currency parsing utilities for Indonesian Rupiah.
317
+ *
318
+ * @module currency/parse
319
+ * @packageDocumentation
320
+ */
321
+ /**
322
+ * Parses a formatted Rupiah string back to a number.
323
+ *
324
+ * Handles multiple formats:
325
+ * - Standard: "Rp 1.500.000"
326
+ * - No symbol: "1.500.000"
327
+ * - With decimals: "Rp 1.500.000,50"
328
+ * - Compact: "Rp 1,5 juta", "Rp 500 ribu"
329
+ *
330
+ * @param formatted - The formatted Rupiah string to parse
331
+ * @returns Parsed number, or null if invalid
332
+ *
333
+ * @example
334
+ * Standard format:
335
+ * ```typescript
336
+ * parseRupiah('Rp 1.500.000'); // 1500000
337
+ * ```
338
+ *
339
+ * @example
340
+ * With decimals:
341
+ * ```typescript
342
+ * parseRupiah('Rp 1.500.000,50'); // 1500000.50
343
+ * ```
344
+ *
345
+ * @example
346
+ * Compact format:
347
+ * ```typescript
348
+ * parseRupiah('Rp 1,5 juta'); // 1500000
349
+ * parseRupiah('Rp 500 ribu'); // 500000
350
+ * ```
351
+ *
352
+ * @example
353
+ * Invalid input:
354
+ * ```typescript
355
+ * parseRupiah('invalid'); // null
356
+ * ```
357
+ *
358
+ * @public
359
+ */
360
+ declare function parseRupiah(formatted: string): number | null;
361
+
362
+ /**
363
+ * Convert numbers to Indonesian words (terbilang).
364
+ *
365
+ * @module currency/words
366
+ * @packageDocumentation
367
+ */
368
+
369
+ /**
370
+ * Converts a number to Indonesian words (terbilang).
371
+ *
372
+ * Supports numbers up to trillions (triliun).
373
+ * Follows Indonesian language rules for number pronunciation.
374
+ *
375
+ * Special rules:
376
+ * - 1 = "satu" in most cases, but "se-" for 100, 1000
377
+ * - 11 = "sebelas" (not "satu belas")
378
+ * - 100 = "seratus" (not "satu ratus")
379
+ * - 1000 = "seribu" (not "satu ribu")
380
+ *
381
+ * @param amount - The number to convert
382
+ * @param options - Conversion options
383
+ * @returns Indonesian words representation
384
+ *
385
+ * @example
386
+ * Basic numbers:
387
+ * ```typescript
388
+ * toWords(123); // 'seratus dua puluh tiga rupiah'
389
+ * ```
390
+ *
391
+ * @example
392
+ * Large numbers:
393
+ * ```typescript
394
+ * toWords(1500000); // 'satu juta lima ratus ribu rupiah'
395
+ * ```
396
+ *
397
+ * @example
398
+ * With options:
399
+ * ```typescript
400
+ * toWords(1500000, { uppercase: true });
401
+ * // 'Satu juta lima ratus ribu rupiah'
402
+ *
403
+ * toWords(1500000, { withCurrency: false });
404
+ * // 'satu juta lima ratus ribu'
405
+ * ```
406
+ *
407
+ * @public
408
+ */
409
+ declare function toWords(amount: number, options?: WordOptions): string;
410
+
411
+ /**
412
+ * Currency utility functions.
413
+ *
414
+ * @module currency/utils
415
+ * @packageDocumentation
416
+ */
417
+
418
+ /**
419
+ * Rounds a number to a clean currency amount.
420
+ *
421
+ * Common use case: displaying approximate prices or budgets
422
+ * in clean, rounded numbers.
423
+ *
424
+ * @param amount - The amount to round
425
+ * @param unit - The unit to round to (default: 'ribu')
426
+ * @returns Rounded amount
427
+ *
428
+ * @example
429
+ * Round to thousands:
430
+ * ```typescript
431
+ * roundToClean(1234567, 'ribu'); // 1235000
432
+ * ```
433
+ *
434
+ * @example
435
+ * Round to hundred thousands:
436
+ * ```typescript
437
+ * roundToClean(1234567, 'ratus-ribu'); // 1200000
438
+ * ```
439
+ *
440
+ * @example
441
+ * Round to millions:
442
+ * ```typescript
443
+ * roundToClean(1234567, 'juta'); // 1000000
444
+ * ```
445
+ *
446
+ * @public
447
+ */
448
+ declare function roundToClean(amount: number, unit?: RoundUnit): number;
449
+ /**
450
+ * Formats a number as Indonesian Rupiah in accounting style.
451
+ * Negative numbers are wrapped in parentheses.
452
+ *
453
+ * @param amount - The amount to format
454
+ * @param options - Formatting options
455
+ * @returns Formatted accounting string
456
+ *
457
+ * @example
458
+ * ```typescript
459
+ * formatAccounting(-1500000); // '(Rp 1.500.000)'
460
+ * ```
461
+ */
462
+ declare function formatAccounting(amount: number, options?: RupiahOptions): string;
463
+ /**
464
+ * Calculates tax (PPN) for a given amount.
465
+ *
466
+ * @param amount - The base amount
467
+ * @param rate - The tax rate (e.g., 0.11 for 11% PPN)
468
+ * @returns The calculated tax amount
469
+ *
470
+ * @example
471
+ * ```typescript
472
+ * calculateTax(1000000, 0.11); // 110000
473
+ * ```
474
+ */
475
+ declare function calculateTax(amount: number, rate: number): number;
476
+ /**
477
+ * Helper to ensure a string or number has the 'Rp ' prefix.
478
+ * If already prefixed, it returns the input as is.
479
+ *
480
+ * @param amount - The amount or formatted string
481
+ * @returns String with Rupiah prefix
482
+ */
483
+ declare function addRupiahSymbol(amount: string | number): string;
484
+
485
+ export { type CompactOptions as C, type RupiahOptions as R, type SplitOptions as S, type WordOptions as W, formatCompact as a, formatAccounting as b, calculateTax as c, addRupiahSymbol as d, type RoundUnit as e, formatRupiah as f, parseRupiah as p, roundToClean as r, toWords as t };