@pyreon/money 0.0.0-stage → 0.53.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 +21 -0
- package/README.md +40 -2
- package/lib/_chunks/money-B9MhDL2i.js +551 -0
- package/lib/_chunks/money-B9MhDL2i.js.map +1 -0
- package/lib/_chunks/scaled-eXdrPpWS.js +195 -0
- package/lib/_chunks/scaled-eXdrPpWS.js.map +1 -0
- package/lib/decimal.js +126 -0
- package/lib/decimal.js.map +1 -0
- package/lib/fp.js +91 -0
- package/lib/fp.js.map +1 -0
- package/lib/index.js +3 -0
- package/lib/types/_dts_chunks/money-Cq-JWPjZ.d.ts +253 -0
- package/lib/types/_dts_chunks/scaled-vdrxv7sl.d.ts +3 -0
- package/lib/types/decimal.d.ts +83 -0
- package/lib/types/fp.d.ts +67 -0
- package/lib/types/index.d.ts +4 -0
- package/package.json +61 -4
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { t as RoundingMode } from "./scaled-vdrxv7sl.js";
|
|
2
|
+
/** An ISO 4217 code (`"EUR"`) or a code registered with `defineCurrency`. */
|
|
3
|
+
type CurrencyCode = string;
|
|
4
|
+
/**
|
|
5
|
+
* The number of minor-unit digits of a currency, from the platform (`Intl`; no currency table is shipped) or from
|
|
6
|
+
* `defineCurrency`: 2 for EUR, 0 for JPY, 3 for KWD.
|
|
7
|
+
*
|
|
8
|
+
* @example currencyDigits('EUR') // => 2
|
|
9
|
+
* @example currencyDigits('JPY') // => 0
|
|
10
|
+
* @example currencyDigits('KWD') // => 3
|
|
11
|
+
*/
|
|
12
|
+
declare function currencyDigits(code: CurrencyCode): number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether `code` is a known currency: an upper-case ISO 4217 code the platform knows, or one registered with
|
|
15
|
+
* `defineCurrency`. Never throws.
|
|
16
|
+
*
|
|
17
|
+
* @example isCurrency('EUR') // => true
|
|
18
|
+
* @example isCurrency('eur') // => false
|
|
19
|
+
* @example isCurrency('XXXX') // => false
|
|
20
|
+
*/
|
|
21
|
+
declare function isCurrency(code: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Register a currency the platform does not know (crypto, loyalty points, a test currency): `defineCurrency('PTS', { digits: 0 })`.
|
|
24
|
+
* The code is 2 to 12 upper-case letters or digits starting with a letter. Registering the same code again with the same
|
|
25
|
+
* digits is fine; with different digits it throws, as does changing an ISO currency's digits. The registry holds at most
|
|
26
|
+
* 64 currencies.
|
|
27
|
+
*
|
|
28
|
+
* @example defineCurrency('PTS', { digits: 0 }); currencyDigits('PTS') // => 0
|
|
29
|
+
*/
|
|
30
|
+
declare function defineCurrency(code: string, spec: {
|
|
31
|
+
digits: number;
|
|
32
|
+
}): void;
|
|
33
|
+
/** Exact money as plain data: a canonical decimal string with exactly the currency's minor digits, and its currency code. */
|
|
34
|
+
interface Money {
|
|
35
|
+
readonly amount: string;
|
|
36
|
+
readonly currency: CurrencyCode;
|
|
37
|
+
}
|
|
38
|
+
/** Options of `money`, `multiply`, `divide`, `percentage`, `convert`. */
|
|
39
|
+
interface RoundingOptions {
|
|
40
|
+
/** How an inexact result is rounded to the currency's digits. Default `'halfExpand'` (round half away from zero). */
|
|
41
|
+
rounding?: RoundingMode;
|
|
42
|
+
}
|
|
43
|
+
/** Options of `money`: without `rounding`, a value with more decimals than the currency has throws. */
|
|
44
|
+
interface MoneyOptions {
|
|
45
|
+
/** Round an amount with too many decimals instead of throwing. */
|
|
46
|
+
rounding?: RoundingMode;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Build a Money. A string is exact; a number is read by its shortest round-trip text (`9.99` is exactly 9.99, not the binary
|
|
50
|
+
* float near it); a bigint is whole units. A value with more decimals than the currency has throws unless `rounding` is given
|
|
51
|
+
* (so `money(0.1 + 0.2, 'EUR')` throws and explains floating point, instead of hiding the bug). The amount of the result has
|
|
52
|
+
* exactly the currency's digits: `"9.90"`, `"100"` for JPY, `"0.123"` for KWD.
|
|
53
|
+
*
|
|
54
|
+
* @example money(9.99, 'EUR') // => { amount: '9.99', currency: 'EUR' }
|
|
55
|
+
* @example money('10', 'EUR') // => { amount: '10.00', currency: 'EUR' }
|
|
56
|
+
* @example money(100, 'JPY') // => { amount: '100', currency: 'JPY' }
|
|
57
|
+
* @example money(0.1 + 0.2, 'EUR', { rounding: 'halfExpand' }) // => { amount: '0.30', currency: 'EUR' }
|
|
58
|
+
*/
|
|
59
|
+
declare function money(amount: number | string | bigint, currency: CurrencyCode, options?: MoneyOptions): Money;
|
|
60
|
+
/**
|
|
61
|
+
* A Money from minor units (cents): `fromMinor(999, 'EUR')` is 9.99. A number must be a safe integer; use a bigint or a
|
|
62
|
+
* digit string above 2^53.
|
|
63
|
+
*
|
|
64
|
+
* @example fromMinor(999, 'EUR') // => { amount: '9.99', currency: 'EUR' }
|
|
65
|
+
* @example fromMinor('123456789012345678901', 'EUR') // => { amount: '1234567890123456789.01', currency: 'EUR' }
|
|
66
|
+
*/
|
|
67
|
+
declare function fromMinor(units: number | bigint | string, currency: CurrencyCode): Money;
|
|
68
|
+
/**
|
|
69
|
+
* The amount in minor units as a bigint: `toMinor(money('9.99', 'EUR'))` is `999n`.
|
|
70
|
+
*
|
|
71
|
+
* @example toMinor(money('9.99', 'EUR')) // => 999n
|
|
72
|
+
*/
|
|
73
|
+
declare function toMinor(m: Money): bigint;
|
|
74
|
+
/**
|
|
75
|
+
* The amount in minor units as a number; throws beyond the safe integer range, where a number would not be exact.
|
|
76
|
+
*
|
|
77
|
+
* @example toMinorNumber(money('9.99', 'EUR')) // => 999
|
|
78
|
+
*/
|
|
79
|
+
declare function toMinorNumber(m: Money): number;
|
|
80
|
+
/**
|
|
81
|
+
* Whether `x` is a Money: exactly the fields `amount` and `currency`, a known currency, and a canonical amount with exactly
|
|
82
|
+
* that currency's digits. Never throws.
|
|
83
|
+
*
|
|
84
|
+
* @example isMoney({ amount: '9.99', currency: 'EUR' }) // => true
|
|
85
|
+
* @example isMoney({ amount: '9.9', currency: 'EUR' }) // => false
|
|
86
|
+
* @example isMoney({ amount: 9.99, currency: 'EUR' }) // => false
|
|
87
|
+
*/
|
|
88
|
+
declare function isMoney(x: unknown): x is Money;
|
|
89
|
+
/**
|
|
90
|
+
* Zero in a currency.
|
|
91
|
+
*
|
|
92
|
+
* @example zero('EUR') // => { amount: '0.00', currency: 'EUR' }
|
|
93
|
+
*/
|
|
94
|
+
declare function zero(currency: CurrencyCode): Money;
|
|
95
|
+
/**
|
|
96
|
+
* Exact sum of two amounts of one currency. Mixing currencies throws and names both.
|
|
97
|
+
*
|
|
98
|
+
* @example add(money('0.10', 'EUR'), money('0.20', 'EUR')) // => { amount: '0.30', currency: 'EUR' }
|
|
99
|
+
*/
|
|
100
|
+
declare function add(a: Money, b: Money): Money;
|
|
101
|
+
/**
|
|
102
|
+
* Exact difference `a - b` of two amounts of one currency.
|
|
103
|
+
*
|
|
104
|
+
* @example subtract(money('1.00', 'EUR'), money('0.01', 'EUR')) // => { amount: '0.99', currency: 'EUR' }
|
|
105
|
+
*/
|
|
106
|
+
declare function subtract(a: Money, b: Money): Money;
|
|
107
|
+
/**
|
|
108
|
+
* The same amount with the opposite sign.
|
|
109
|
+
*
|
|
110
|
+
* @example negate(money('5', 'EUR')) // => { amount: '-5.00', currency: 'EUR' }
|
|
111
|
+
*/
|
|
112
|
+
declare function negate(m: Money): Money;
|
|
113
|
+
/**
|
|
114
|
+
* The amount without its sign.
|
|
115
|
+
*
|
|
116
|
+
* @example abs(money('-5', 'EUR')) // => { amount: '5.00', currency: 'EUR' }
|
|
117
|
+
*/
|
|
118
|
+
declare function abs(m: Money): Money;
|
|
119
|
+
/**
|
|
120
|
+
* The sum of a list of one currency. An empty list needs `currency`.
|
|
121
|
+
*
|
|
122
|
+
* @example sum([money('1.10', 'EUR'), money('2.20', 'EUR')]) // => { amount: '3.30', currency: 'EUR' }
|
|
123
|
+
* @example sum([], 'EUR') // => { amount: '0.00', currency: 'EUR' }
|
|
124
|
+
*/
|
|
125
|
+
declare function sum(list: Iterable<Money>, currency?: CurrencyCode): Money;
|
|
126
|
+
/**
|
|
127
|
+
* The amount times a plain number, rounded to the currency's digits (default `'halfExpand'`). The factor is a pure number: a
|
|
128
|
+
* number is read by its shortest text, so `1.21` is exactly 1.21; prefer a string. For a Money times a Money there is no
|
|
129
|
+
* operation, because it has no meaning.
|
|
130
|
+
*
|
|
131
|
+
* @example multiply(money('19.99', 'EUR'), 3) // => { amount: '59.97', currency: 'EUR' }
|
|
132
|
+
* @example multiply(money('10.00', 'EUR'), '1.21') // => { amount: '12.10', currency: 'EUR' }
|
|
133
|
+
* @example multiply(money('0.05', 'EUR'), '0.5') // => { amount: '0.03', currency: 'EUR' }
|
|
134
|
+
*/
|
|
135
|
+
declare function multiply(m: Money, factor: number | string | bigint, options?: RoundingOptions): Money;
|
|
136
|
+
/**
|
|
137
|
+
* The amount divided by a plain number, rounded to the currency's digits. A zero divisor throws. To split an amount without
|
|
138
|
+
* losing a cent use `allocate` or `split`: dividing three ways and multiplying back does not give the amount.
|
|
139
|
+
*
|
|
140
|
+
* @example divide(money('10.00', 'EUR'), 3) // => { amount: '3.33', currency: 'EUR' }
|
|
141
|
+
*/
|
|
142
|
+
declare function divide(m: Money, divisor: number | string | bigint, options?: RoundingOptions): Money;
|
|
143
|
+
/**
|
|
144
|
+
* `percent` percent of an amount, rounded to the currency's digits.
|
|
145
|
+
*
|
|
146
|
+
* @example percentage(money('200.00', 'EUR'), 21) // => { amount: '42.00', currency: 'EUR' }
|
|
147
|
+
* @example percentage(money('9.99', 'EUR'), '7.5') // => { amount: '0.75', currency: 'EUR' }
|
|
148
|
+
*/
|
|
149
|
+
declare function percentage(m: Money, percent: number | string | bigint, options?: RoundingOptions): Money;
|
|
150
|
+
/**
|
|
151
|
+
* Share an amount by ratios so that the parts sum EXACTLY to the amount. Each part is its floor share in minor units; the
|
|
152
|
+
* minor units left over go one each to the parts with the largest fractional remainders, ties to the first. A negative amount
|
|
153
|
+
* is shared by its absolute value and the parts take its sign. A ratio of zero gets zero. All-zero or negative ratios throw.
|
|
154
|
+
* Each part is within one minor unit of its exact share. Reordering the ratios reorders the parts, but a tie can move a cent.
|
|
155
|
+
*
|
|
156
|
+
* @example allocate(money('100.00', 'EUR'), [1, 1, 1]).map((p) => p.amount) // => ['33.34', '33.33', '33.33']
|
|
157
|
+
* @example allocate(money('0.05', 'EUR'), [3, 7]).map((p) => p.amount) // => ['0.02', '0.03']
|
|
158
|
+
* @example allocate(money('10.00', 'EUR'), [0, 1]).map((p) => p.amount) // => ['0.00', '10.00']
|
|
159
|
+
*/
|
|
160
|
+
declare function allocate(m: Money, ratios: readonly (number | string)[]): Money[];
|
|
161
|
+
/**
|
|
162
|
+
* Split an amount into `parts` equal parts that sum exactly to it: the first parts get the extra cent.
|
|
163
|
+
*
|
|
164
|
+
* @example split(money('100.00', 'EUR'), 3).map((p) => p.amount) // => ['33.34', '33.33', '33.33']
|
|
165
|
+
*/
|
|
166
|
+
declare function split(m: Money, parts: number): Money[];
|
|
167
|
+
/** Options of `ratio`. */
|
|
168
|
+
interface RatioOptions {
|
|
169
|
+
/** Digits after the decimal point. Default 10. */
|
|
170
|
+
scale?: number;
|
|
171
|
+
/** Default `'halfExpand'`. */
|
|
172
|
+
rounding?: RoundingMode;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* `a / b` as a decimal string with trailing zeros removed, rounded to `scale` digits (default 10). Both amounts must be one
|
|
176
|
+
* currency; a zero `b` throws.
|
|
177
|
+
*
|
|
178
|
+
* @example ratio(money('1.00', 'EUR'), money('4.00', 'EUR')) // => '0.25'
|
|
179
|
+
* @example ratio(money('1.00', 'EUR'), money('3.00', 'EUR'), { scale: 4 }) // => '0.3333'
|
|
180
|
+
*/
|
|
181
|
+
declare function ratio(a: Money, b: Money, options?: RatioOptions): string;
|
|
182
|
+
/**
|
|
183
|
+
* -1, 0 or 1. Different currencies throw (they cannot be ordered without a rate).
|
|
184
|
+
*
|
|
185
|
+
* @example compare(money('1.00', 'EUR'), money('2.00', 'EUR')) // => -1
|
|
186
|
+
*/
|
|
187
|
+
declare function compare(a: Money, b: Money): -1 | 0 | 1;
|
|
188
|
+
/**
|
|
189
|
+
* Whether two amounts are equal. Amounts of different currencies are not equal (this does not throw).
|
|
190
|
+
*
|
|
191
|
+
* @example equals(money('1.5', 'EUR'), money('1.50', 'EUR')) // => true
|
|
192
|
+
* @example equals(money('1', 'EUR'), money('1', 'USD')) // => false
|
|
193
|
+
*/
|
|
194
|
+
declare function equals(a: Money, b: Money): boolean;
|
|
195
|
+
/**
|
|
196
|
+
* The smallest amount. At least one is required; currencies must match.
|
|
197
|
+
*
|
|
198
|
+
* @example min(money('3', 'EUR'), money('2', 'EUR')) // => { amount: '2.00', currency: 'EUR' }
|
|
199
|
+
*/
|
|
200
|
+
declare function min(...amounts: [Money, ...Money[]]): Money;
|
|
201
|
+
/**
|
|
202
|
+
* The largest amount. At least one is required; currencies must match.
|
|
203
|
+
*
|
|
204
|
+
* @example max(money('3', 'EUR'), money('2', 'EUR')) // => { amount: '3.00', currency: 'EUR' }
|
|
205
|
+
*/
|
|
206
|
+
declare function max(...amounts: [Money, ...Money[]]): Money;
|
|
207
|
+
/**
|
|
208
|
+
* Whether the amount is zero.
|
|
209
|
+
*
|
|
210
|
+
* @example isZero(zero('EUR')) // => true
|
|
211
|
+
*/
|
|
212
|
+
declare function isZero(m: Money): boolean;
|
|
213
|
+
/**
|
|
214
|
+
* Whether the amount is above zero.
|
|
215
|
+
*
|
|
216
|
+
* @example isPositive(money('0.01', 'EUR')) // => true
|
|
217
|
+
*/
|
|
218
|
+
declare function isPositive(m: Money): boolean;
|
|
219
|
+
/**
|
|
220
|
+
* Whether the amount is below zero.
|
|
221
|
+
*
|
|
222
|
+
* @example isNegative(money('-0.01', 'EUR')) // => true
|
|
223
|
+
*/
|
|
224
|
+
declare function isNegative(m: Money): boolean;
|
|
225
|
+
/** Options of `round`. */
|
|
226
|
+
interface MoneyRoundOptions {
|
|
227
|
+
/** Keep fewer fraction digits than the currency has (`0` rounds to whole units). */
|
|
228
|
+
digits?: number;
|
|
229
|
+
/** Round to a multiple of this amount (`'0.05'` for cash rounding in CHF). Not together with `digits`. */
|
|
230
|
+
increment?: string | number;
|
|
231
|
+
/** Default `'halfExpand'`. */
|
|
232
|
+
mode?: RoundingMode;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Round to fewer digits or to a multiple of an increment (cash rounding), by `mode` (default `'halfExpand'`, round half away
|
|
236
|
+
* from zero; `'halfEven'` is banker's rounding). With no options it is the amount itself, because a Money always has exactly
|
|
237
|
+
* its currency's digits.
|
|
238
|
+
*
|
|
239
|
+
* @example round(money('12.34', 'EUR'), { digits: 0 }) // => { amount: '12.00', currency: 'EUR' }
|
|
240
|
+
* @example round(money('1.23', 'CHF'), { increment: '0.05' }) // => { amount: '1.25', currency: 'CHF' }
|
|
241
|
+
* @example round(money('2.50', 'EUR'), { digits: 0, mode: 'halfEven' }) // => { amount: '2.00', currency: 'EUR' }
|
|
242
|
+
*/
|
|
243
|
+
declare function round(m: Money, options?: MoneyRoundOptions): Money;
|
|
244
|
+
/**
|
|
245
|
+
* Convert to another currency with a rate (`1 unit of m.currency = rate units of to`), rounded to the target's digits. Give
|
|
246
|
+
* the rate as a decimal string; a number is read by its shortest text. Keep `{ from, to, rate }` next to the result: it is the
|
|
247
|
+
* audit trail.
|
|
248
|
+
*
|
|
249
|
+
* @example convert(money('100.00', 'EUR'), 'USD', '1.0842') // => { amount: '108.42', currency: 'USD' }
|
|
250
|
+
* @example convert(money('100.00', 'EUR'), 'JPY', '162.5') // => { amount: '16250', currency: 'JPY' }
|
|
251
|
+
*/
|
|
252
|
+
declare function convert(m: Money, to: CurrencyCode, rate: number | string | bigint, options?: RoundingOptions): Money;
|
|
253
|
+
export { toMinorNumber as A, percentage as C, subtract as D, split as E, isCurrency as F, CurrencyCode as M, currencyDigits as N, sum as O, defineCurrency as P, negate as S, round as T, isZero as _, RoundingOptions as a, money as b, allocate as c, divide as d, equals as f, isPositive as g, isNegative as h, RatioOptions as i, zero as j, toMinor as k, compare as l, isMoney as m, MoneyOptions as n, abs as o, fromMinor as p, MoneyRoundOptions as r, add as s, Money as t, convert as u, max as v, ratio as w, multiply as x, min as y };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { t as RoundingMode } from "./_dts_chunks/scaled-vdrxv7sl.js";
|
|
2
|
+
/**
|
|
3
|
+
* Exact decimals on text: `Decimal` is canonical decimal text (`"-"`, digits, optional `"." digits`; no exponent, no `+`, no
|
|
4
|
+
* leading zeros, never `-0`). Every operation is exact at any size and returns canonical text; inputs may use the lenient
|
|
5
|
+
* forms `normalize` accepts (`+1.5`, `.5`, `1e-3`). Math runs on `bigint` scaled integers.
|
|
6
|
+
*/
|
|
7
|
+
export type Decimal = string;
|
|
8
|
+
/**
|
|
9
|
+
* Whether `x` is a canonical decimal string. Never throws.
|
|
10
|
+
*
|
|
11
|
+
* @example isDecimal('9.90') // => true
|
|
12
|
+
* @example isDecimal('.5') // => false
|
|
13
|
+
* @example isDecimal('1e3') // => false
|
|
14
|
+
*/
|
|
15
|
+
export declare function isDecimal(x: unknown): x is Decimal;
|
|
16
|
+
/**
|
|
17
|
+
* The canonical form of a decimal given as text, a number (by its shortest text) or a bigint. The scale is kept: `"1.50"`
|
|
18
|
+
* stays `"1.50"`. Throws on anything that is not a decimal.
|
|
19
|
+
*
|
|
20
|
+
* @example normalize('+.5') // => '0.5'
|
|
21
|
+
* @example normalize('1.5e2') // => '150'
|
|
22
|
+
* @example normalize('-0.00') // => '0.00'
|
|
23
|
+
*/
|
|
24
|
+
export declare function normalize(value: number | string | bigint): Decimal;
|
|
25
|
+
/**
|
|
26
|
+
* Exact sum.
|
|
27
|
+
*
|
|
28
|
+
* @example add('0.1', '0.2') // => '0.3'
|
|
29
|
+
*/
|
|
30
|
+
export declare function add(a: number | string | bigint, b: number | string | bigint): Decimal;
|
|
31
|
+
/**
|
|
32
|
+
* Exact difference.
|
|
33
|
+
*
|
|
34
|
+
* @example subtract('0.3', '0.1') // => '0.2'
|
|
35
|
+
*/
|
|
36
|
+
export declare function subtract(a: number | string | bigint, b: number | string | bigint): Decimal;
|
|
37
|
+
/**
|
|
38
|
+
* Exact product (the scale is the sum of the scales).
|
|
39
|
+
*
|
|
40
|
+
* @example multiply('19.99', '3') // => '59.97'
|
|
41
|
+
*/
|
|
42
|
+
export declare function multiply(a: number | string | bigint, b: number | string | bigint): Decimal;
|
|
43
|
+
/** Options of `divide`. */
|
|
44
|
+
export interface DivideOptions {
|
|
45
|
+
/** Digits after the decimal point in the result. Default 20. */
|
|
46
|
+
scale?: number;
|
|
47
|
+
/** Default `'halfExpand'`. */
|
|
48
|
+
rounding?: RoundingMode;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Quotient rounded to `scale` digits (default 20). A zero divisor throws.
|
|
52
|
+
*
|
|
53
|
+
* @example divide('1', '3', { scale: 4 }) // => '0.3333'
|
|
54
|
+
* @example divide('2', '3', { scale: 2, rounding: 'floor' }) // => '0.66'
|
|
55
|
+
*/
|
|
56
|
+
export declare function divide(a: number | string | bigint, b: number | string | bigint, options?: DivideOptions): Decimal;
|
|
57
|
+
/**
|
|
58
|
+
* -1, 0 or 1.
|
|
59
|
+
*
|
|
60
|
+
* @example compare('9.90', '9.9') // => 0
|
|
61
|
+
* @example compare('-1', '0.5') // => -1
|
|
62
|
+
*/
|
|
63
|
+
export declare function compare(a: number | string | bigint, b: number | string | bigint): -1 | 0 | 1;
|
|
64
|
+
/** Options of `round`. */
|
|
65
|
+
export interface RoundOptions {
|
|
66
|
+
/** Digits after the decimal point to keep. Default 0 (or the increment's scale). */
|
|
67
|
+
digits?: number;
|
|
68
|
+
/** Round to a multiple of this decimal instead (`'0.05'` for cash rounding). */
|
|
69
|
+
increment?: number | string;
|
|
70
|
+
/** Default `'halfExpand'`. */
|
|
71
|
+
mode?: RoundingMode;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Round to `digits` fraction digits (default 0) or to a multiple of `increment`, by `mode` (default `'halfExpand'`, round
|
|
75
|
+
* half away from zero; `'halfEven'` is banker's rounding). The result has the target scale.
|
|
76
|
+
*
|
|
77
|
+
* @example round('2.5') // => '3'
|
|
78
|
+
* @example round('2.5', { mode: 'halfEven' }) // => '2'
|
|
79
|
+
* @example round('1.2345', { digits: 2 }) // => '1.23'
|
|
80
|
+
* @example round('1.23', { increment: '0.05' }) // => '1.25'
|
|
81
|
+
*/
|
|
82
|
+
export declare function round(value: number | string | bigint, options?: RoundOptions): Decimal;
|
|
83
|
+
export type { RoundingMode };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { M as CurrencyCode, S as negate, _ as isZero, a as RoundingOptions, g as isPositive, h as isNegative, l as compare, o as abs, r as MoneyRoundOptions, t as Money } from "./_dts_chunks/money-Cq-JWPjZ.js";
|
|
2
|
+
/**
|
|
3
|
+
* Value-last forms of the Money operations: each call takes the configuration and returns a plain `(m: Money) => Money`, so an
|
|
4
|
+
* operation can be built once, named, passed to `map` and composed with any `pipe` (`pipe` from `@pyreon/time/fp` or
|
|
5
|
+
* `@pyreon/rx`; this package ships none, so that it has no dependencies). Thin wrappers over the core functions.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* `add(b)` is a function that adds `b` to a Money.
|
|
9
|
+
*
|
|
10
|
+
* @example add({ amount: '1.00', currency: 'EUR' })({ amount: '2.50', currency: 'EUR' }) // => { amount: '3.50', currency: 'EUR' }
|
|
11
|
+
*/
|
|
12
|
+
export declare function add(b: Money): (m: Money) => Money;
|
|
13
|
+
/**
|
|
14
|
+
* `subtract(b)` subtracts `b`.
|
|
15
|
+
*
|
|
16
|
+
* @example subtract({ amount: '1.00', currency: 'EUR' })({ amount: '2.50', currency: 'EUR' }) // => { amount: '1.50', currency: 'EUR' }
|
|
17
|
+
*/
|
|
18
|
+
export declare function subtract(b: Money): (m: Money) => Money;
|
|
19
|
+
/**
|
|
20
|
+
* `multiply('1.21')` multiplies by a factor, rounded to the currency's digits.
|
|
21
|
+
*
|
|
22
|
+
* @example multiply('1.21')({ amount: '10.00', currency: 'EUR' }) // => { amount: '12.10', currency: 'EUR' }
|
|
23
|
+
*/
|
|
24
|
+
export declare function multiply(factor: number | string | bigint, options?: RoundingOptions): (m: Money) => Money;
|
|
25
|
+
/**
|
|
26
|
+
* `divide(3)` divides, rounded to the currency's digits.
|
|
27
|
+
*
|
|
28
|
+
* @example divide(3)({ amount: '10.00', currency: 'EUR' }) // => { amount: '3.33', currency: 'EUR' }
|
|
29
|
+
*/
|
|
30
|
+
export declare function divide(divisor: number | string | bigint, options?: RoundingOptions): (m: Money) => Money;
|
|
31
|
+
/**
|
|
32
|
+
* `percentage(21)` takes 21 percent.
|
|
33
|
+
*
|
|
34
|
+
* @example percentage(21)({ amount: '200.00', currency: 'EUR' }) // => { amount: '42.00', currency: 'EUR' }
|
|
35
|
+
*/
|
|
36
|
+
export declare function percentage(percent: number | string | bigint, options?: RoundingOptions): (m: Money) => Money;
|
|
37
|
+
/**
|
|
38
|
+
* `round({ increment: '0.05' })` rounds (cash rounding, fewer digits, any mode).
|
|
39
|
+
*
|
|
40
|
+
* @example round({ increment: '0.05' })({ amount: '1.23', currency: 'CHF' }) // => { amount: '1.25', currency: 'CHF' }
|
|
41
|
+
*/
|
|
42
|
+
export declare function round(options?: MoneyRoundOptions): (m: Money) => Money;
|
|
43
|
+
/**
|
|
44
|
+
* `convert('USD', '1.0842')` converts with a rate.
|
|
45
|
+
*
|
|
46
|
+
* @example convert('USD', '1.0842')({ amount: '100.00', currency: 'EUR' }) // => { amount: '108.42', currency: 'USD' }
|
|
47
|
+
*/
|
|
48
|
+
export declare function convert(to: CurrencyCode, rate: number | string | bigint, options?: RoundingOptions): (m: Money) => Money;
|
|
49
|
+
/**
|
|
50
|
+
* `allocate([1, 1, 1])` shares an amount into parts that sum exactly to it.
|
|
51
|
+
*
|
|
52
|
+
* @example allocate([1, 1, 1])({ amount: '100.00', currency: 'EUR' }).map((p) => p.amount) // => ['33.34', '33.33', '33.33']
|
|
53
|
+
*/
|
|
54
|
+
export declare function allocate(ratios: readonly (number | string)[]): (m: Money) => Money[];
|
|
55
|
+
/**
|
|
56
|
+
* `split(3)` splits into equal parts that sum exactly to the amount.
|
|
57
|
+
*
|
|
58
|
+
* @example split(2)({ amount: '0.01', currency: 'EUR' }).map((p) => p.amount) // => ['0.01', '0.00']
|
|
59
|
+
*/
|
|
60
|
+
export declare function split(parts: number): (m: Money) => Money[];
|
|
61
|
+
/**
|
|
62
|
+
* `equals(other)` is a predicate: true for an amount equal to `other`.
|
|
63
|
+
*
|
|
64
|
+
* @example equals({ amount: '1.00', currency: 'EUR' })({ amount: '1.00', currency: 'EUR' }) // => true
|
|
65
|
+
*/
|
|
66
|
+
export declare function equals(other: Money): (m: Money) => boolean;
|
|
67
|
+
export { type Money, type MoneyRoundOptions, type RoundingOptions, abs, compare, isNegative, isPositive, isZero, negate };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Decimal } from "./decimal.js";
|
|
2
|
+
import { A as toMinorNumber, C as percentage, D as subtract, E as split, F as isCurrency, M as CurrencyCode, N as currencyDigits, O as sum, P as defineCurrency, S as negate, T as round, _ as isZero, a as RoundingOptions, b as money, c as allocate, d as divide, f as equals, g as isPositive, h as isNegative, i as RatioOptions, j as zero, k as toMinor, l as compare, m as isMoney, n as MoneyOptions, o as abs, p as fromMinor, r as MoneyRoundOptions, s as add, t as Money, u as convert, v as max, w as ratio, x as multiply, y as min } from "./_dts_chunks/money-Cq-JWPjZ.js";
|
|
3
|
+
import { t as RoundingMode } from "./_dts_chunks/scaled-vdrxv7sl.js";
|
|
4
|
+
export { type CurrencyCode, type Decimal, type Money, type MoneyOptions, type MoneyRoundOptions, type RatioOptions, type RoundingMode, type RoundingOptions, abs, add, allocate, compare, convert, currencyDigits, defineCurrency, divide, equals, fromMinor, isCurrency, isMoney, isNegative, isPositive, isZero, max, min, money, multiply, negate, percentage, ratio, round, split, subtract, sum, toMinor, toMinorNumber, zero };
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/money",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.53.0",
|
|
4
|
+
"description": "Exact amounts of money as plain data for Pyreon: decimal-string amounts, bigint math, rounding modes, allocation that never loses a cent",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/fundamentals/money#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/pyreon/pyreon/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/pyreon/pyreon.git",
|
|
13
|
+
"directory": "packages/fundamentals/money"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./lib/index.js",
|
|
18
|
+
"module": "./lib/index.js",
|
|
19
|
+
"types": "./lib/types/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./lib/index.js",
|
|
23
|
+
"types": "./lib/types/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./decimal": {
|
|
26
|
+
"import": "./lib/decimal.js",
|
|
27
|
+
"types": "./lib/types/decimal.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./fp": {
|
|
30
|
+
"import": "./lib/fp.js",
|
|
31
|
+
"types": "./lib/types/fp.d.ts"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"lib",
|
|
36
|
+
"!lib/analysis",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "vl_rolldown_build",
|
|
42
|
+
"dev": "vl_rolldown_build-watch",
|
|
43
|
+
"test": "vitest run --maxWorkers=2",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"lint": "oxlint .",
|
|
46
|
+
"bench": "NODE_ENV=production bun bench/money-bench.ts"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@pyreon/manifest": "0.50.0",
|
|
50
|
+
"@pyreon/tsconfig": "0.50.3",
|
|
51
|
+
"@pyreon/vitest-config": "0.50.0",
|
|
52
|
+
"@types/big.js": "7.0.0",
|
|
53
|
+
"big.js": "7.0.1",
|
|
54
|
+
"currency.js": "2.0.4",
|
|
55
|
+
"dinero.js": "2.0.2"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"pyreon": {
|
|
61
|
+
"stability": "experimental"
|
|
62
|
+
}
|
|
63
|
+
}
|