@eriveltondasilva/currency 1.0.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.
- package/LICENSE.md +21 -0
- package/README.md +215 -0
- package/dist/chunk-5VVSCRNB.js +18 -0
- package/dist/index.d.ts +526 -0
- package/dist/index.js +17 -0
- package/dist/presets.d.ts +60 -0
- package/dist/presets.js +17 -0
- package/dist/types-BX9n6pbS.d.ts +662 -0
- package/package.json +96 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ERIVELTONDASILVA/CURRENCY v1.0.0
|
|
3
|
+
*
|
|
4
|
+
* A lightweight and reliable JavaScript library for precise currency operations, built to safely handle monetary values without floating point errors.
|
|
5
|
+
*
|
|
6
|
+
* @author Erivelton Silva <eriveltondasilva13@gmail.com>
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright 2026 Erivelton Silva
|
|
9
|
+
* @version 1.0.0
|
|
10
|
+
*
|
|
11
|
+
* @see https://github.com/eriveltondasilva/currency#readme - Documentation
|
|
12
|
+
*
|
|
13
|
+
* Inspired by:
|
|
14
|
+
* @see https://github.com/scurker/currency.js
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { M as MoneyInput, C as CountryCode, P as PricedItem, a as MoneyContract, R as RoundingMode } from './types-BX9n6pbS.js';
|
|
18
|
+
export { F as FormatOptions } from './types-BX9n6pbS.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Computes the total cost of a list of priced items.
|
|
22
|
+
*
|
|
23
|
+
* Each item must have a `price` (`number` in major units or `MoneyContract`)
|
|
24
|
+
* and an optional `quantity` (non-negative integer, defaults to `1`).
|
|
25
|
+
* Returns `zero(country)` when `items` is empty.
|
|
26
|
+
*
|
|
27
|
+
* @param items - Array of `{ price, quantity? }` objects. See {@link PricedItem}.
|
|
28
|
+
* @param country - Supported country code that defines the output currency.
|
|
29
|
+
*
|
|
30
|
+
* @returns A new `MoneyContract` with the total amount.
|
|
31
|
+
*
|
|
32
|
+
* @throws `InvalidInputError` - when any item is not a plain object, or `quantity` is not a non-negative integer.
|
|
33
|
+
* @throws `CurrencyMismatchError` - when any `price` is a `MoneyContract` with a different currency.
|
|
34
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
35
|
+
* @throws `UnsafeIntegerError` - when the accumulated total exceeds `Number.MAX_SAFE_INTEGER`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const items = [
|
|
39
|
+
* { price: 29.90, quantity: 2 },
|
|
40
|
+
* { price: 9.99 },
|
|
41
|
+
* ];
|
|
42
|
+
*
|
|
43
|
+
* total(items, 'BR').format()
|
|
44
|
+
* // => 'R$ 69,79'
|
|
45
|
+
*/
|
|
46
|
+
declare function total(items: PricedItem[], country: CountryCode): MoneyContract;
|
|
47
|
+
/**
|
|
48
|
+
* Calculates what percentage `portion` represents of `base`.
|
|
49
|
+
*
|
|
50
|
+
* Both arguments are resolved to minor units before division, ensuring
|
|
51
|
+
* precision. The result is a plain `number`, not a `MoneyContract`.
|
|
52
|
+
*
|
|
53
|
+
* @param value - The partial amount (numerator).
|
|
54
|
+
* @param total - The reference amount (denominator). Must be non-zero.
|
|
55
|
+
* @param country - Supported country code used to resolve both amounts.
|
|
56
|
+
*
|
|
57
|
+
* @returns The percentage as a `number` (e.g. `25` for 25%).
|
|
58
|
+
*
|
|
59
|
+
* @throws `DivisionByZeroError` - when `base` resolves to zero.
|
|
60
|
+
* @throws `CurrencyMismatchError` - when either argument is a `MoneyContract` with a different currency.
|
|
61
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* percent(25, 200, 'US') // => 12.5
|
|
65
|
+
* percent(1, 3, 'BR') // => 33.333...
|
|
66
|
+
*/
|
|
67
|
+
declare function percent(value: MoneyInput, total: MoneyInput, country: CountryCode): number;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Sums an array of monetary values.
|
|
71
|
+
*
|
|
72
|
+
* Returns `zero(country)` when `values` is empty.
|
|
73
|
+
* All values must share the same currency as `country` — passing a
|
|
74
|
+
* `MoneyContract` with a different currency throws `CurrencyMismatchError`.
|
|
75
|
+
*
|
|
76
|
+
* @param values - Array of amounts as numbers (major units) or `MoneyContract` instances.
|
|
77
|
+
* @param country - Supported country code that defines the output currency.
|
|
78
|
+
*
|
|
79
|
+
* @returns A new `MoneyContract` with the total sum.
|
|
80
|
+
*
|
|
81
|
+
* @throws `CurrencyMismatchError` - when any `MoneyContract` in `values` has a different currency.
|
|
82
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
83
|
+
* @throws `UnsafeIntegerError` - when the accumulated sum exceeds `Number.MAX_SAFE_INTEGER`.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* sum([10, 20.50, 5], 'BR').format() // => 'R$ 35,50'
|
|
87
|
+
* sum([], 'US').isZero() // => true
|
|
88
|
+
*/
|
|
89
|
+
declare function sum(values: MoneyInput[], country: CountryCode): MoneyContract;
|
|
90
|
+
/**
|
|
91
|
+
* Computes the arithmetic mean of an array of monetary values.
|
|
92
|
+
*
|
|
93
|
+
* The result is rounded to the nearest minor unit using `'halfExpand'`.
|
|
94
|
+
* Returns `zero(country)` when `values` is empty.
|
|
95
|
+
*
|
|
96
|
+
* @param values - Array of amounts as numbers (major units) or `MoneyContract` instances.
|
|
97
|
+
* @param country - Supported country code that defines the output currency.
|
|
98
|
+
*
|
|
99
|
+
* @returns A new `MoneyContract` with the average amount.
|
|
100
|
+
*
|
|
101
|
+
* @throws `CurrencyMismatchError` - when any `MoneyContract` in `values` has a different currency.
|
|
102
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* average([10, 20, 30], 'US').amount() // => 20
|
|
106
|
+
* average([1, 2], 'BR').amount() // => 1.5
|
|
107
|
+
*/
|
|
108
|
+
declare function average(values: MoneyInput[], country: CountryCode, roundingMode?: RoundingMode): MoneyContract;
|
|
109
|
+
/**
|
|
110
|
+
* Returns the largest value in an array of monetary values.
|
|
111
|
+
*
|
|
112
|
+
* Unlike {@link sum} and {@link average}, `max` requires at least one element.
|
|
113
|
+
*
|
|
114
|
+
* @param values - Non-empty array of amounts as numbers (major units) or `MoneyContract` instances.
|
|
115
|
+
* @param country - Supported country code that defines the output currency.
|
|
116
|
+
*
|
|
117
|
+
* @returns A new `MoneyContract` representing the maximum amount.
|
|
118
|
+
*
|
|
119
|
+
* @throws `InvalidInputError` - when `values` is empty.
|
|
120
|
+
* @throws `CurrencyMismatchError` - when any `MoneyContract` in `values` has a different currency.
|
|
121
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* max([5, 30, 10], 'US').amount() // => 30
|
|
125
|
+
*/
|
|
126
|
+
declare function max(values: MoneyInput[], country: CountryCode): MoneyContract;
|
|
127
|
+
/**
|
|
128
|
+
* Returns the smallest value in an array of monetary values.
|
|
129
|
+
*
|
|
130
|
+
* Unlike {@link sum} and {@link average}, `min` requires at least one element.
|
|
131
|
+
*
|
|
132
|
+
* @param values - Non-empty array of amounts as numbers (major units) or `MoneyContract` instances.
|
|
133
|
+
* @param country - Supported country code that defines the output currency.
|
|
134
|
+
*
|
|
135
|
+
* @returns A new `MoneyContract` representing the minimum amount.
|
|
136
|
+
*
|
|
137
|
+
* @throws `InvalidInputError` - when `values` is empty.
|
|
138
|
+
* @throws `CurrencyMismatchError` - when any `MoneyContract` in `values` has a different currency.
|
|
139
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* min([5, 30, 10], 'US').amount() // => 5
|
|
143
|
+
*/
|
|
144
|
+
declare function min(values: MoneyInput[], country: CountryCode): MoneyContract;
|
|
145
|
+
/**
|
|
146
|
+
* Constrains a monetary value within a `[min, max]` closed interval.
|
|
147
|
+
*
|
|
148
|
+
* - When `value < min`, returns `min`.
|
|
149
|
+
* - When `value > max`, returns `max`.
|
|
150
|
+
* - Otherwise, returns `value` unchanged.
|
|
151
|
+
*
|
|
152
|
+
* @param value - The amount to constrain.
|
|
153
|
+
* @param min - Lower bound of the interval.
|
|
154
|
+
* @param max - Upper bound of the interval.
|
|
155
|
+
* @param country - Supported country code that defines the output currency.
|
|
156
|
+
*
|
|
157
|
+
* @returns A new `MoneyContract` clamped within `[min, max]`.
|
|
158
|
+
*
|
|
159
|
+
* @throws `InvalidRangeError` - when `min` is greater than `max`.
|
|
160
|
+
* @throws `CurrencyMismatchError` - when any `MoneyContract` argument has a different currency.
|
|
161
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* clamp(150, 0, 100, 'US').amount() // => 100
|
|
165
|
+
* clamp(-10, 0, 100, 'US').amount() // => 0
|
|
166
|
+
* clamp(50, 0, 100, 'US').amount() // => 50
|
|
167
|
+
*/
|
|
168
|
+
declare function clamp(value: MoneyInput, min: MoneyInput, max: MoneyInput, country: CountryCode): MoneyContract;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Creates a `MoneyContract` from a `number` in major units.
|
|
172
|
+
*
|
|
173
|
+
* The value is converted to minor units internally using the currency's
|
|
174
|
+
* `fractionDigits`. Rounding is applied with `'halfExpand'` when the
|
|
175
|
+
* conversion produces a non-integer result.
|
|
176
|
+
*
|
|
177
|
+
* Also exported as `money` for more expressive usage.
|
|
178
|
+
*
|
|
179
|
+
* @param value - Amount in major units (e.g. `19.99`).
|
|
180
|
+
* @param country - Supported country code (e.g. `'BR'`, `'US'`).
|
|
181
|
+
*
|
|
182
|
+
* @returns A new `MoneyContract` instance.
|
|
183
|
+
*
|
|
184
|
+
* @throws `InvalidInputError` - when `value` is not a finite number, is null/undefined, or exceeds the safe integer range after conversion to minor units.
|
|
185
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* from(19.99, 'BR').format() // => 'R$ 19,99'
|
|
189
|
+
* from(0, 'US').isZero() // => true
|
|
190
|
+
*/
|
|
191
|
+
declare function from(value: number, country: CountryCode): MoneyContract;
|
|
192
|
+
/**
|
|
193
|
+
* Creates a `MoneyContract` by parsing a locale-formatted currency string.
|
|
194
|
+
*
|
|
195
|
+
* The string is parsed according to the country's decimal and grouping
|
|
196
|
+
* separators. Currency symbols and unknown characters are stripped before
|
|
197
|
+
* parsing.
|
|
198
|
+
*
|
|
199
|
+
* @param value - A locale-formatted string (e.g. `'R$ 1.999,99'`, `'1,999.99'`).
|
|
200
|
+
* @param country - Supported country code that defines the decimal/grouping separators.
|
|
201
|
+
*
|
|
202
|
+
* @returns A new `MoneyContract` instance.
|
|
203
|
+
*
|
|
204
|
+
* @throws `InvalidInputError` - when `value` is not a string, cannot be parsed as a monetary amount, or contains multiple decimal separators.
|
|
205
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* parse('R$ 1.999,99', 'BR').amount() // => 1999.99
|
|
209
|
+
* parse('$1,999.99', 'US').amount() // => 1999.99
|
|
210
|
+
*/
|
|
211
|
+
declare function parse(value: string, country: CountryCode): MoneyContract;
|
|
212
|
+
/**
|
|
213
|
+
* Creates a `MoneyContract` directly from an integer in minor units.
|
|
214
|
+
*
|
|
215
|
+
* Use this when the value is already in minor units — for example, when
|
|
216
|
+
* reconstructing from a database or a {@link MoneyContract.toJSON} payload.
|
|
217
|
+
*
|
|
218
|
+
* @param value - Integer minor-unit value (e.g. `1999` for R$ 19,99).
|
|
219
|
+
* @param country - Supported country code.
|
|
220
|
+
*
|
|
221
|
+
* @returns A new `MoneyContract` instance.
|
|
222
|
+
*
|
|
223
|
+
* @throws `InvalidInputError` - when `value` is not a finite integer or exceeds `Number.MAX_SAFE_INTEGER`.
|
|
224
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* fromMinorUnits(1999, 'BR').amount() // => 19.99
|
|
228
|
+
* fromMinorUnits(500, 'JP').amount() // => 500 (JPY has 0 fraction digits)
|
|
229
|
+
*/
|
|
230
|
+
declare function fromMinorUnits(value: number, country: CountryCode): MoneyContract;
|
|
231
|
+
/**
|
|
232
|
+
* Creates a `MoneyContract` with an amount of zero for the given country.
|
|
233
|
+
* Useful as an identity value for reductions or as a neutral starting point.
|
|
234
|
+
*
|
|
235
|
+
* @param country - Supported country code.
|
|
236
|
+
*
|
|
237
|
+
* @returns A new `MoneyContract` with `minorUnits === 0`.
|
|
238
|
+
*
|
|
239
|
+
* @throws `UnsupportedCurrencyError` - when `country` is not a supported code.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* zero('BR').isZero() // => true
|
|
243
|
+
* zero('BR').format() // => 'R$ 0,00'
|
|
244
|
+
* zero('US').currencyCode() // => 'USD'
|
|
245
|
+
*/
|
|
246
|
+
declare function zero(country: CountryCode): MoneyContract;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Returns `true` if `value` is a `MoneyContract` instance.
|
|
250
|
+
*
|
|
251
|
+
* Use this as a type guard to distinguish `MoneyContract` from plain `number`
|
|
252
|
+
* when handling {@link MoneyInput}.
|
|
253
|
+
*
|
|
254
|
+
* @param value — Any value to test.
|
|
255
|
+
* @returns `true` when `value` is an instance of `Money`.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* isMoney(from(10, 'BR')) // => true
|
|
259
|
+
* isMoney(10) // => false
|
|
260
|
+
* isMoney(null) // => false
|
|
261
|
+
*/
|
|
262
|
+
declare function isMoney(value: unknown): value is MoneyContract;
|
|
263
|
+
/**
|
|
264
|
+
* Returns `true` if `value` is a valid {@link MoneyInput} — either a `number`
|
|
265
|
+
* or a `MoneyContract` instance.
|
|
266
|
+
*
|
|
267
|
+
* Use this to validate external inputs before passing them to API functions
|
|
268
|
+
* that accept `MoneyInput`.
|
|
269
|
+
*
|
|
270
|
+
* @param value — Any value to test.
|
|
271
|
+
* @returns `true` when `value` is a `number` or a `MoneyContract` instance.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* isMoneyInput(19.99) // => true
|
|
275
|
+
* isMoneyInput(from(10, 'BR')) // => true
|
|
276
|
+
* isMoneyInput('19.99') // => false
|
|
277
|
+
* isMoneyInput(null) // => false
|
|
278
|
+
*/
|
|
279
|
+
declare function isMoneyInput(value: unknown): value is number | MoneyContract;
|
|
280
|
+
|
|
281
|
+
declare const api_average: typeof average;
|
|
282
|
+
declare const api_clamp: typeof clamp;
|
|
283
|
+
declare const api_from: typeof from;
|
|
284
|
+
declare const api_fromMinorUnits: typeof fromMinorUnits;
|
|
285
|
+
declare const api_isMoney: typeof isMoney;
|
|
286
|
+
declare const api_isMoneyInput: typeof isMoneyInput;
|
|
287
|
+
declare const api_max: typeof max;
|
|
288
|
+
declare const api_min: typeof min;
|
|
289
|
+
declare const api_parse: typeof parse;
|
|
290
|
+
declare const api_percent: typeof percent;
|
|
291
|
+
declare const api_sum: typeof sum;
|
|
292
|
+
declare const api_total: typeof total;
|
|
293
|
+
declare const api_zero: typeof zero;
|
|
294
|
+
declare namespace api {
|
|
295
|
+
export { api_average as average, api_clamp as clamp, api_from as from, api_fromMinorUnits as fromMinorUnits, api_isMoney as isMoney, api_isMoneyInput as isMoneyInput, api_max as max, api_min as min, api_parse as parse, api_percent as percent, api_sum as sum, api_total as total, api_zero as zero };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Discriminant union of all error codes produced by this library.
|
|
300
|
+
* Available on every {@link MoneyError} instance via the `code` property,
|
|
301
|
+
* enabling exhaustive error handling without `instanceof` checks.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* try {
|
|
305
|
+
* from(10, 'BR').divide(0)
|
|
306
|
+
* } catch (err) {
|
|
307
|
+
* if (err instanceof MoneyError) {
|
|
308
|
+
* switch (err.code) {
|
|
309
|
+
* case 'DIVISION_BY_ZERO': // handle
|
|
310
|
+
* }
|
|
311
|
+
* }
|
|
312
|
+
* }
|
|
313
|
+
*/
|
|
314
|
+
type MoneyErrorCode = 'INVALID_INPUT' | 'INVALID_PERCENTAGE' | 'DIVISION_BY_ZERO' | 'INVALID_ALLOCATION' | 'INVALID_RANGE' | 'CURRENCY_MISMATCH' | 'UNSUPPORTED_CURRENCY' | 'UNSAFE_INTEGER';
|
|
315
|
+
/**
|
|
316
|
+
* Options accepted by {@link MoneyError} and its subclasses.
|
|
317
|
+
* Extends the native `ErrorOptions` (which includes `cause`).
|
|
318
|
+
*/
|
|
319
|
+
interface MoneyErrorOptions extends ErrorOptions {
|
|
320
|
+
input?: unknown;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Base class for all errors thrown by this library.
|
|
324
|
+
*
|
|
325
|
+
* Extends the native `Error` with two additional properties:
|
|
326
|
+
* - `code` — a machine-readable {@link MoneyErrorCode} for programmatic handling.
|
|
327
|
+
* - `input` — the offending value, when available, for easier debugging.
|
|
328
|
+
*
|
|
329
|
+
* Use `instanceof MoneyError` to catch any library error in a single branch,
|
|
330
|
+
* or check `error.code` to distinguish between specific cases.
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* import { MoneyError } from '@eriveltondasilva/currency'
|
|
334
|
+
*
|
|
335
|
+
* try {
|
|
336
|
+
* from(10, 'BR').plus(from(10, 'US'))
|
|
337
|
+
* } catch (err) {
|
|
338
|
+
* if (err instanceof MoneyError) {
|
|
339
|
+
* console.error(err.code, err.message)
|
|
340
|
+
* }
|
|
341
|
+
* }
|
|
342
|
+
*/
|
|
343
|
+
declare abstract class MoneyError extends Error {
|
|
344
|
+
abstract name: string;
|
|
345
|
+
readonly code: MoneyErrorCode;
|
|
346
|
+
readonly input?: unknown;
|
|
347
|
+
constructor(message: string, code: MoneyErrorCode, options?: MoneyErrorOptions);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Thrown when a value fails basic input validation — wrong type, non-finite
|
|
351
|
+
* number, unsafe integer, or a value that cannot be parsed as a monetary amount.
|
|
352
|
+
*
|
|
353
|
+
* `code`: `'INVALID_INPUT'`
|
|
354
|
+
*/
|
|
355
|
+
declare class InvalidInputError extends MoneyError {
|
|
356
|
+
readonly name = "InvalidInputError";
|
|
357
|
+
constructor(message: string, options?: MoneyErrorOptions);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Thrown when a percentage or discount/surcharge value is invalid —
|
|
361
|
+
* negative, non-finite, or out of the expected range.
|
|
362
|
+
*
|
|
363
|
+
* `code`: `'INVALID_PERCENTAGE'`
|
|
364
|
+
*/
|
|
365
|
+
declare class InvalidPercentageError extends MoneyError {
|
|
366
|
+
readonly name = "InvalidPercentageError";
|
|
367
|
+
constructor(message: string, options?: MoneyErrorOptions);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Thrown when a division or percentage operation is attempted with a
|
|
371
|
+
* denominator of zero.
|
|
372
|
+
*
|
|
373
|
+
* `code`: `'DIVISION_BY_ZERO'`
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* from(10, 'US').divide(0) // throws DivisionByZeroError
|
|
377
|
+
* percent(5, 0, 'US') // throws DivisionByZeroError
|
|
378
|
+
*/
|
|
379
|
+
declare class DivisionByZeroError extends MoneyError {
|
|
380
|
+
readonly name = "DivisionByZeroError";
|
|
381
|
+
constructor(options?: MoneyErrorOptions);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Thrown when arguments passed to {@link MoneyContract.allocate} or
|
|
385
|
+
* {@link MoneyContract.allocateByRatio} are invalid — non-integer parts,
|
|
386
|
+
* negative ratios, zero-sum ratios, or an empty ratio array.
|
|
387
|
+
*
|
|
388
|
+
* `code`: `'INVALID_ALLOCATION'`
|
|
389
|
+
*/
|
|
390
|
+
declare class InvalidAllocationError extends MoneyError {
|
|
391
|
+
readonly name = "InvalidAllocationError";
|
|
392
|
+
constructor(message: string, options?: MoneyErrorOptions);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Thrown when a `min` value is greater than a `max` value in range-based
|
|
396
|
+
* operations such as {@link MoneyContract.isBetween} or `clamp`.
|
|
397
|
+
*
|
|
398
|
+
* `code`: `'INVALID_RANGE'`
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* from(5, 'US').isBetween(10, 1) // throws InvalidRangeError
|
|
402
|
+
*/
|
|
403
|
+
declare class InvalidRangeError extends MoneyError {
|
|
404
|
+
readonly name = "InvalidRangeError";
|
|
405
|
+
constructor(options?: MoneyErrorOptions);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Thrown when an operation is attempted between two `MoneyContract` instances
|
|
409
|
+
* with different currency codes.
|
|
410
|
+
*
|
|
411
|
+
* `code`: `'CURRENCY_MISMATCH'`
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* from(10, 'BR').plus(from(10, 'US')) // throws CurrencyMismatchError
|
|
415
|
+
*/
|
|
416
|
+
declare class CurrencyMismatchError extends MoneyError {
|
|
417
|
+
readonly name = "CurrencyMismatchError";
|
|
418
|
+
constructor(baseCode: string, otherCode: string, options?: MoneyErrorOptions);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Thrown when an unsupported country code is passed to any API function.
|
|
422
|
+
*
|
|
423
|
+
* `code`: `'UNSUPPORTED_CURRENCY'`
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* from(10, 'XX' as any) // throws UnsupportedCurrencyError
|
|
427
|
+
*/
|
|
428
|
+
declare class UnsupportedCurrencyError extends MoneyError {
|
|
429
|
+
readonly name = "UnsupportedCurrencyError";
|
|
430
|
+
constructor(country: string, supportedCodes: string, options?: MoneyErrorOptions);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Thrown when an arithmetic operation produces a result that exceeds
|
|
434
|
+
* `Number.MAX_SAFE_INTEGER`, which would compromise integer precision
|
|
435
|
+
* in minor-unit calculations.
|
|
436
|
+
*
|
|
437
|
+
* `code`: `'UNSAFE_INTEGER'`
|
|
438
|
+
*/
|
|
439
|
+
declare class UnsafeIntegerError extends MoneyError {
|
|
440
|
+
readonly name = "UnsafeIntegerError";
|
|
441
|
+
constructor(options?: MoneyErrorOptions);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* @module @eriveltondasilva/currency
|
|
446
|
+
*
|
|
447
|
+
* # A lightweight TypeScript library for precise monetary operations.
|
|
448
|
+
*
|
|
449
|
+
* All values are stored internally as **minor-unit integers** to eliminate
|
|
450
|
+
* floating-point errors. Every operation returns a new instance — the API
|
|
451
|
+
* is fully immutable.
|
|
452
|
+
*
|
|
453
|
+
* ## Quick start
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* import { from, sum, total } from '@eriveltondasilva/currency'
|
|
457
|
+
*
|
|
458
|
+
* const price = from(19.99, 'BR')
|
|
459
|
+
* price.format() // => 'R$ 19,99'
|
|
460
|
+
* price.applyDiscount(10).format() // => 'R$ 17,99'
|
|
461
|
+
*
|
|
462
|
+
* sum([10, 20, 30], 'US').format() // => '$60.00'
|
|
463
|
+
*
|
|
464
|
+
* const items = [
|
|
465
|
+
* { price: 9.99, quantity: 3 },
|
|
466
|
+
* { price: 4.99 },
|
|
467
|
+
* ]
|
|
468
|
+
* total(items, 'US').format()
|
|
469
|
+
* // => '$34.96'
|
|
470
|
+
*
|
|
471
|
+
* ## Preset shorthand (optional import)
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* import { br, us } from '@eriveltondasilva/currency/presets'
|
|
475
|
+
*
|
|
476
|
+
* br(19.99).format() // => 'R$ 19,99'
|
|
477
|
+
* us(9.99).format() // => '$9.99'
|
|
478
|
+
*
|
|
479
|
+
* ## Supported countries
|
|
480
|
+
*
|
|
481
|
+
* `AU`, `BR`, `CA`, `CH`, `CN`, `DE`, `FR`, `GB`, `IN`, `JP`, `MX`, `PT`, `SG`, `US`
|
|
482
|
+
*/
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Frozen namespace that exposes the full library API as a single object.
|
|
486
|
+
* Useful when a single import is preferred over named imports.
|
|
487
|
+
*
|
|
488
|
+
* @remarks
|
|
489
|
+
* Prefer named imports over this namespace for better tree-shaking.
|
|
490
|
+
*
|
|
491
|
+
* ```ts
|
|
492
|
+
* // ✅ Only `from` and `format` are bundled
|
|
493
|
+
* import { from } from '@eriveltondasilva/currency'
|
|
494
|
+
*
|
|
495
|
+
* // ⚠️ Entire library is bundled
|
|
496
|
+
* import Money from '@eriveltondasilva/currency'
|
|
497
|
+
* ```
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* import Money from '@eriveltondasilva/currency'
|
|
501
|
+
*
|
|
502
|
+
* // Creation
|
|
503
|
+
* Money.from(19.99, 'BR').format() // => 'R$ 19,99'
|
|
504
|
+
* Money.fromMinorUnits(2000, 'BR').format() // => 'R$ 20,00'
|
|
505
|
+
* Money.parse('$20.00', 'US').format() // => '$20.00'
|
|
506
|
+
* Money.zero('US').format() // => '$0.00'
|
|
507
|
+
*
|
|
508
|
+
* // Collection
|
|
509
|
+
* Money.average([10, 20], 'US').format() // => '$15.00'
|
|
510
|
+
* Money.clamp(100, 1, 20, 'US').format() // => '$20.00'
|
|
511
|
+
* Money.max([10, 20], 'US').format() // => '$20.00'
|
|
512
|
+
* Money.min([10, 20], 'US').format() // => '$10.00'
|
|
513
|
+
* Money.sum([10, 20, 30], 'US').format() // => '$60.00'
|
|
514
|
+
*
|
|
515
|
+
* // Business
|
|
516
|
+
* Money.percent(10, 200, 'US') // => 5
|
|
517
|
+
* Money.total([{ price: 5, quantity: 2 }], 'US').format()
|
|
518
|
+
* // => '$10.00'
|
|
519
|
+
*
|
|
520
|
+
* // Utils
|
|
521
|
+
* Money.isMoney(Money.from(19.99, 'BR')) // => true
|
|
522
|
+
* Money.isMoneyInput(Money.from(19.99, 'BR')) // => true
|
|
523
|
+
*/
|
|
524
|
+
declare const Money: typeof api;
|
|
525
|
+
|
|
526
|
+
export { CurrencyMismatchError, DivisionByZeroError, InvalidAllocationError, InvalidInputError, InvalidPercentageError, InvalidRangeError, Money, MoneyContract, MoneyError, MoneyInput, PricedItem, RoundingMode, UnsafeIntegerError, UnsupportedCurrencyError, average, clamp, Money as default, from, fromMinorUnits, parse as fromString, isMoney, isMoneyInput, max, min, from as money, parse, percent, sum, total, zero };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {w}from'./chunk-5VVSCRNB.js';export{g as CurrencyMismatchError,d as DivisionByZeroError,e as InvalidAllocationError,b as InvalidInputError,c as InvalidPercentageError,f as InvalidRangeError,a as MoneyError,i as UnsafeIntegerError,h as UnsupportedCurrencyError,s as average,v as clamp,l as from,n as fromMinorUnits,m as fromString,j as isMoney,k as isMoneyInput,t as max,u as min,l as money,m as parse,q as percent,r as sum,p as total,o as zero}from'./chunk-5VVSCRNB.js';/**
|
|
2
|
+
* @ERIVELTONDASILVA/CURRENCY v1.0.0
|
|
3
|
+
*
|
|
4
|
+
* A lightweight and reliable JavaScript library for precise currency operations, built to safely handle monetary values without floating point errors.
|
|
5
|
+
*
|
|
6
|
+
* @author Erivelton Silva <eriveltondasilva13@gmail.com>
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright 2026 Erivelton Silva
|
|
9
|
+
* @version 1.0.0
|
|
10
|
+
*
|
|
11
|
+
* @see https://github.com/eriveltondasilva/currency#readme - Documentation
|
|
12
|
+
*
|
|
13
|
+
* Inspired by:
|
|
14
|
+
* @see https://github.com/scurker/currency.js
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
var U=Object.freeze({...w}),z=U;export{U as Money,z as default};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ERIVELTONDASILVA/CURRENCY v1.0.0
|
|
3
|
+
*
|
|
4
|
+
* A lightweight and reliable JavaScript library for precise currency operations, built to safely handle monetary values without floating point errors.
|
|
5
|
+
*
|
|
6
|
+
* @author Erivelton Silva <eriveltondasilva13@gmail.com>
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright 2026 Erivelton Silva
|
|
9
|
+
* @version 1.0.0
|
|
10
|
+
*
|
|
11
|
+
* @see https://github.com/eriveltondasilva/currency#readme - Documentation
|
|
12
|
+
*
|
|
13
|
+
* Inspired by:
|
|
14
|
+
* @see https://github.com/scurker/currency.js
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { a as MoneyContract } from './types-BX9n6pbS.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @module presets
|
|
21
|
+
*
|
|
22
|
+
* Country-specific shorthand factories. Each preset is a function that accepts
|
|
23
|
+
* a `number` (major units) or a `string` (parsed according to the country's
|
|
24
|
+
* decimal and grouping separators) and returns a `MoneyContract`.
|
|
25
|
+
*
|
|
26
|
+
* Import from the `/presets` entry point:
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { br, us } from '@eriveltondasilva/currency/presets'
|
|
30
|
+
*
|
|
31
|
+
* br(19.99).format() // => 'R$ 19,99'
|
|
32
|
+
* us('1,500.00').format() // => '$1,500.00'
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A country-bound factory that creates a `MoneyContract` from a `number` or
|
|
37
|
+
* a locale-formatted `string`.
|
|
38
|
+
*
|
|
39
|
+
* - `number` — interpreted as major units (e.g. `19.99`) {@link from}.
|
|
40
|
+
* - `string` — parsed using the country's decimal and grouping separators {@link parse}.
|
|
41
|
+
*
|
|
42
|
+
* @throws `InvalidInputError` — when the string cannot be parsed as a monetary amount.
|
|
43
|
+
*/
|
|
44
|
+
type CreatePreset = (value: number | string) => MoneyContract;
|
|
45
|
+
declare const au: CreatePreset;
|
|
46
|
+
declare const br: CreatePreset;
|
|
47
|
+
declare const ca: CreatePreset;
|
|
48
|
+
declare const ch: CreatePreset;
|
|
49
|
+
declare const cn: CreatePreset;
|
|
50
|
+
declare const de: CreatePreset;
|
|
51
|
+
declare const fr: CreatePreset;
|
|
52
|
+
declare const gb: CreatePreset;
|
|
53
|
+
declare const ind: CreatePreset;
|
|
54
|
+
declare const jp: CreatePreset;
|
|
55
|
+
declare const mx: CreatePreset;
|
|
56
|
+
declare const sg: CreatePreset;
|
|
57
|
+
declare const pt: CreatePreset;
|
|
58
|
+
declare const us: CreatePreset;
|
|
59
|
+
|
|
60
|
+
export { au, br, ca, ch, cn, de, fr, gb, ind, jp, mx, pt, sg, us };
|
package/dist/presets.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {m as m$1,l}from'./chunk-5VVSCRNB.js';/**
|
|
2
|
+
* @ERIVELTONDASILVA/CURRENCY v1.0.0
|
|
3
|
+
*
|
|
4
|
+
* A lightweight and reliable JavaScript library for precise currency operations, built to safely handle monetary values without floating point errors.
|
|
5
|
+
*
|
|
6
|
+
* @author Erivelton Silva <eriveltondasilva13@gmail.com>
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright 2026 Erivelton Silva
|
|
9
|
+
* @version 1.0.0
|
|
10
|
+
*
|
|
11
|
+
* @see https://github.com/eriveltondasilva/currency#readme - Documentation
|
|
12
|
+
*
|
|
13
|
+
* Inspired by:
|
|
14
|
+
* @see https://github.com/scurker/currency.js
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function e(r){return t=>typeof t=="string"?m$1(t,r):l(t,r)}var p=e("AU"),C=e("BR"),c=e("CA"),a=e("CH"),P=e("CN"),x=e("DE"),i=e("FR"),m=e("GB"),y=e("IN"),f=e("JP"),u=e("MX"),b=e("SG"),g=e("PT"),d=e("US");export{p as au,C as br,c as ca,a as ch,P as cn,x as de,i as fr,m as gb,y as ind,f as jp,u as mx,g as pt,b as sg,d as us};
|