@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.
@@ -1,407 +1,103 @@
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
- /**
143
- * Unit for rounding currency amounts.
144
- *
145
- * Common Indonesian currency rounding units:
146
- * - `'ribu'`: Round to thousands (1.000)
147
- * - `'ratus-ribu'`: Round to hundred thousands (100.000)
148
- * - `'juta'`: Round to millions (1.000.000)
149
- *
150
- * @example
151
- * ```typescript
152
- * roundToClean(1234567, 'ribu'); // 1235000
153
- * roundToClean(1234567, 'ratus-ribu'); // 1200000
154
- * roundToClean(1234567, 'juta'); // 1000000
155
- * ```
156
- *
157
- * @public
158
- */
159
- type RoundUnit = 'ribu' | 'ratus-ribu' | 'juta';
160
-
161
- /**
162
- * Currency formatting utilities for Indonesian Rupiah.
163
- *
164
- * @module currency/format
165
- * @packageDocumentation
166
- */
1
+ import { S as SplitOptions } from '../utils-OG1yMaAa.cjs';
2
+ export { C as CompactOptions, e as RoundUnit, R as RupiahOptions, W as WordOptions, d as addRupiahSymbol, c as calculateTax, b as formatAccounting, a as formatCompact, f as formatRupiah, p as parseRupiah, r as roundToClean, t as toWords } from '../utils-OG1yMaAa.cjs';
167
3
 
168
4
  /**
169
- * Formats a number as Indonesian Rupiah currency.
170
- *
171
- * Provides flexible formatting options including symbol display,
172
- * decimal places, and custom separators.
173
- *
174
- * @param amount - The amount to format
175
- * @param options - Formatting options
176
- * @returns Formatted Rupiah string
177
- *
178
- * @example
179
- * Basic formatting:
180
- * ```typescript
181
- * formatRupiah(1500000); // 'Rp 1.500.000'
182
- * ```
183
- *
184
- * @example
185
- * With decimals:
186
- * ```typescript
187
- * formatRupiah(1500000.50, { decimal: true }); // 'Rp 1.500.000,50'
188
- * ```
189
- *
190
- * @example
191
- * Without symbol:
192
- * ```typescript
193
- * formatRupiah(1500000, { symbol: false }); // '1.500.000'
194
- * ```
195
- *
196
- * @example
197
- * Custom separators:
198
- * ```typescript
199
- * formatRupiah(1500000, { separator: ',' }); // 'Rp 1,500,000'
200
- * ```
5
+ * Invalid split error thrown when split parameters are invalid.
201
6
  *
202
7
  * @public
203
8
  */
204
- declare function formatRupiah(amount: number, options?: RupiahOptions): string;
9
+ declare class InvalidSplitError extends Error {
10
+ constructor(message: string);
11
+ }
205
12
  /**
206
- * Formats a number in compact Indonesian format.
207
- *
208
- * Uses Indonesian units: ribu, juta, miliar, triliun.
209
- * Follows Indonesian grammar rules (e.g., "1 juta" not "1,0 juta").
13
+ * Splits an amount into equal or custom-ratio parts.
210
14
  *
211
- * @param amount - The amount to format
212
- * @returns Compact formatted string
15
+ * @param amount - The amount to split
16
+ * @param parts - Number of parts to split into
17
+ * @param options - Split options (ratios, rounding)
18
+ * @returns Array of split amounts
213
19
  *
214
20
  * @example
215
- * Millions:
21
+ * Equal split:
216
22
  * ```typescript
217
- * formatCompact(1500000); // 'Rp 1,5 juta'
218
- * formatCompact(1000000); // 'Rp 1 juta'
23
+ * splitAmount(1500000, 3); // [500000, 500000, 500000]
219
24
  * ```
220
25
  *
221
26
  * @example
222
- * Thousands:
27
+ * Custom ratios:
223
28
  * ```typescript
224
- * formatCompact(500000); // 'Rp 500 ribu'
29
+ * splitAmount(1000000, 2, { ratios: [70, 30] }); // [700000, 300000]
225
30
  * ```
226
31
  *
227
32
  * @example
228
- * Small numbers:
33
+ * With rounding:
229
34
  * ```typescript
230
- * formatCompact(1500); // 'Rp 1.500'
35
+ * splitAmount(1234567, 3, { roundTo: 'ribu' }); // [412000, 411000, 411000]
231
36
  * ```
232
37
  *
233
38
  * @public
234
39
  */
235
- declare function formatCompact(amount: number): string;
236
-
237
- /**
238
- * Currency parsing utilities for Indonesian Rupiah.
239
- *
240
- * @module currency/parse
241
- * @packageDocumentation
242
- */
40
+ declare function splitAmount(amount: number, parts: number, options?: SplitOptions): number[];
243
41
  /**
244
- * Parses a formatted Rupiah string back to a number.
245
- *
246
- * Handles multiple formats:
247
- * - Standard: "Rp 1.500.000"
248
- * - No symbol: "1.500.000"
249
- * - With decimals: "Rp 1.500.000,50"
250
- * - Compact: "Rp 1,5 juta", "Rp 500 ribu"
251
- *
252
- * @param formatted - The formatted Rupiah string to parse
253
- * @returns Parsed number, or null if invalid
254
- *
255
- * @example
256
- * Standard format:
257
- * ```typescript
258
- * parseRupiah('Rp 1.500.000'); // 1500000
259
- * ```
260
- *
261
- * @example
262
- * With decimals:
263
- * ```typescript
264
- * parseRupiah('Rp 1.500.000,50'); // 1500000.50
265
- * ```
42
+ * Calculates what percentage a part is of a total.
266
43
  *
267
- * @example
268
- * Compact format:
269
- * ```typescript
270
- * parseRupiah('Rp 1,5 juta'); // 1500000
271
- * parseRupiah('Rp 500 ribu'); // 500000
272
- * ```
44
+ * @param part - The part value
45
+ * @param total - The total value
46
+ * @returns Percentage as number (e.g., 15 for 15%)
273
47
  *
274
48
  * @example
275
- * Invalid input:
276
49
  * ```typescript
277
- * parseRupiah('invalid'); // null
50
+ * percentageOf(150000, 1000000); // 15
51
+ * percentageOf(0, 1000000); // 0
52
+ * percentageOf(100, 0); // 0 (not NaN)
278
53
  * ```
279
54
  *
280
55
  * @public
281
56
  */
282
- declare function parseRupiah(formatted: string): number | null;
283
-
284
- /**
285
- * Convert numbers to Indonesian words (terbilang).
286
- *
287
- * @module currency/words
288
- * @packageDocumentation
289
- */
290
-
57
+ declare function percentageOf(part: number, total: number): number;
291
58
  /**
292
- * Converts a number to Indonesian words (terbilang).
59
+ * Calculates absolute and percentage difference between two amounts.
293
60
  *
294
- * Supports numbers up to trillions (triliun).
295
- * Follows Indonesian language rules for number pronunciation.
296
- *
297
- * Special rules:
298
- * - 1 = "satu" in most cases, but "se-" for 100, 1000
299
- * - 11 = "sebelas" (not "satu belas")
300
- * - 100 = "seratus" (not "satu ratus")
301
- * - 1000 = "seribu" (not "satu ribu")
302
- *
303
- * @param amount - The number to convert
304
- * @param options - Conversion options
305
- * @returns Indonesian words representation
61
+ * @param amount1 - The new/current amount
62
+ * @param amount2 - The original/reference amount
63
+ * @returns Object with absolute difference, percentage, and direction
306
64
  *
307
65
  * @example
308
- * Basic numbers:
309
66
  * ```typescript
310
- * toWords(123); // 'seratus dua puluh tiga rupiah'
311
- * ```
67
+ * difference(1200000, 1000000);
68
+ * // { absolute: 200000, percentage: 20, direction: 'increase' }
312
69
  *
313
- * @example
314
- * Large numbers:
315
- * ```typescript
316
- * toWords(1500000); // 'satu juta lima ratus ribu rupiah'
317
- * ```
318
- *
319
- * @example
320
- * With options:
321
- * ```typescript
322
- * toWords(1500000, { uppercase: true });
323
- * // 'Satu juta lima ratus ribu rupiah'
324
- *
325
- * toWords(1500000, { withCurrency: false });
326
- * // 'satu juta lima ratus ribu'
70
+ * difference(0, 1000000);
71
+ * // { absolute: -1000000, percentage: null, direction: 'decrease' }
327
72
  * ```
328
73
  *
329
74
  * @public
330
75
  */
331
- declare function toWords(amount: number, options?: WordOptions): string;
332
-
333
- /**
334
- * Currency utility functions.
335
- *
336
- * @module currency/utils
337
- * @packageDocumentation
338
- */
76
+ declare function difference(amount1: number, amount2: number): {
77
+ absolute: number;
78
+ percentage: number | null;
79
+ direction: 'increase' | 'decrease' | 'same';
80
+ };
339
81
 
340
82
  /**
341
- * Rounds a number to a clean currency amount.
342
- *
343
- * Common use case: displaying approximate prices or budgets
344
- * in clean, rounded numbers.
345
- *
346
- * @param amount - The amount to round
347
- * @param unit - The unit to round to (default: 'ribu')
348
- * @returns Rounded amount
83
+ * Validates whether a string is a valid Rupiah format.
349
84
  *
350
- * @example
351
- * Round to thousands:
352
- * ```typescript
353
- * roundToClean(1234567, 'ribu'); // 1235000
354
- * ```
85
+ * Accepts standard, compact, and negative formats.
355
86
  *
356
- * @example
357
- * Round to hundred thousands:
358
- * ```typescript
359
- * roundToClean(1234567, 'ratus-ribu'); // 1200000
360
- * ```
87
+ * @param formatted - The string to validate
88
+ * @returns `true` if valid Rupiah format, `false` otherwise
361
89
  *
362
90
  * @example
363
- * Round to millions:
364
91
  * ```typescript
365
- * roundToClean(1234567, 'juta'); // 1000000
92
+ * validateRupiah('Rp 1.500.000'); // true
93
+ * validateRupiah('1.500.000'); // true
94
+ * validateRupiah('Rp 1,5 juta'); // true
95
+ * validateRupiah('abc'); // false
96
+ * validateRupiah(''); // false
366
97
  * ```
367
98
  *
368
99
  * @public
369
100
  */
370
- declare function roundToClean(amount: number, unit?: RoundUnit): number;
371
- /**
372
- * Formats a number as Indonesian Rupiah in accounting style.
373
- * Negative numbers are wrapped in parentheses.
374
- *
375
- * @param amount - The amount to format
376
- * @param options - Formatting options
377
- * @returns Formatted accounting string
378
- *
379
- * @example
380
- * ```typescript
381
- * formatAccounting(-1500000); // '(Rp 1.500.000)'
382
- * ```
383
- */
384
- declare function formatAccounting(amount: number, options?: RupiahOptions): string;
385
- /**
386
- * Calculates tax (PPN) for a given amount.
387
- *
388
- * @param amount - The base amount
389
- * @param rate - The tax rate (default: 0.11 for 11%)
390
- * @returns The calculated tax amount
391
- *
392
- * @example
393
- * ```typescript
394
- * calculateTax(1000000); // 110000
395
- * ```
396
- */
397
- declare function calculateTax(amount: number, rate?: number): number;
398
- /**
399
- * Helper to ensure a string or number has the 'Rp ' prefix.
400
- * If already prefixed, it returns the input as is.
401
- *
402
- * @param amount - The amount or formatted string
403
- * @returns String with Rupiah prefix
404
- */
405
- declare function addRupiahSymbol(amount: string | number): string;
101
+ declare function validateRupiah(formatted: string): boolean;
406
102
 
407
- export { type RoundUnit, type RupiahOptions, type WordOptions, addRupiahSymbol, calculateTax, formatAccounting, formatCompact, formatRupiah, parseRupiah, roundToClean, toWords };
103
+ export { InvalidSplitError, SplitOptions, difference, percentageOf, splitAmount, validateRupiah };