@amboras-dev/money 0.1.0 → 0.3.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/README.md +25 -10
- package/dist/ai.cjs +12 -13
- package/dist/ai.d.cts +5 -9
- package/dist/ai.d.ts +5 -9
- package/dist/ai.js +12 -13
- package/dist/{allocate-D7tm_t_o.cjs → allocate-AusholrC.cjs} +3 -4
- package/dist/{allocate-CdnhvAda.js → allocate-BBRfdaSC.js} +3 -4
- package/dist/{amount-DHtvZSSm.cjs → amount-CwFzcxUl.cjs} +29 -12
- package/dist/{amount-D7dj76nH.js → amount-DHFOK2Yd.js} +29 -12
- package/dist/{arithmetic-C-Q_aI49.js → arithmetic-C97-YafD.js} +4 -5
- package/dist/{arithmetic-Bq-8mSal.cjs → arithmetic-D8A6IeZ0.cjs} +4 -5
- package/dist/billing.cjs +5 -8
- package/dist/billing.d.cts +1 -9
- package/dist/billing.d.ts +1 -9
- package/dist/billing.js +5 -8
- package/dist/commerce.cjs +15 -23
- package/dist/commerce.d.cts +2 -16
- package/dist/commerce.d.ts +2 -16
- package/dist/commerce.js +15 -23
- package/dist/{format-BnA1aLJk.cjs → format-92fzvTXY.cjs} +21 -6
- package/dist/{format-BdB7LEIp.js → format-BJC513u4.js} +16 -7
- package/dist/fx.cjs +12 -18
- package/dist/fx.d.cts +5 -15
- package/dist/fx.d.ts +5 -15
- package/dist/fx.js +12 -18
- package/dist/index.cjs +13 -12
- package/dist/index.d.cts +28 -22
- package/dist/index.d.ts +28 -22
- package/dist/index.js +11 -13
- package/dist/input.cjs +7 -10
- package/dist/input.d.cts +9 -16
- package/dist/input.d.ts +9 -16
- package/dist/input.js +7 -10
- package/dist/{iso-minor--gQWI1f2.cjs → iso-minor-DyNjT9A1.cjs} +2 -3
- package/dist/{iso-minor-CtaiIifs.js → iso-minor-odkY-e6I.js} +2 -3
- package/dist/{medusa-OJ2TJOxt.cjs → medusa-CzVK0bth.cjs} +24 -4
- package/dist/{medusa-DuQ3-KoD.d.cts → medusa-DofdvIgQ.d.cts} +11 -4
- package/dist/{medusa-RwqxaDGq.d.ts → medusa-DrbYmD2Y.d.ts} +11 -4
- package/dist/{medusa-CmxCni-G.js → medusa-pKGlImn9.js} +19 -5
- package/dist/{money-CbhsT0fB.d.cts → money-BkMeYDEW.d.cts} +19 -9
- package/dist/{money-CbhsT0fB.d.ts → money-BkMeYDEW.d.ts} +19 -9
- package/dist/{money-CIgELekG.js → money-BxbAw7BP.js} +25 -21
- package/dist/{money-D5-vlv0h.cjs → money-DWBjuuEK.cjs} +42 -20
- package/dist/{number-text-C82oh6JB.js → number-text-Bj-gZXiF.js} +3 -4
- package/dist/{number-text-BpvN6NAo.cjs → number-text-CWuVwrlg.cjs} +3 -4
- package/dist/{own-oP9DXFWv.cjs → own-C5imDkyB.cjs} +1 -2
- package/dist/{own-D7544D3k.js → own-DHxqIYqL.js} +1 -2
- package/dist/{percent-CKVI_c-m.js → percent-CB3Ixjc_.js} +2 -3
- package/dist/{percent-Dt8vNIAW.cjs → percent-DABHiSBs.cjs} +2 -3
- package/dist/{rate-CmeTGNex.d.ts → rate-BvvZUdSu.d.ts} +1 -3
- package/dist/{rate-toyS3EMf.d.cts → rate-CEakW3ia.d.cts} +1 -3
- package/dist/{rate-DGPfB_jc.cjs → rate-CEjWTATc.cjs} +3 -4
- package/dist/{rate-lLPViBuM.js → rate-D6S2_wgZ.js} +3 -4
- package/dist/{reporting-5xby-Zas.d.ts → reporting-DFNyDBdi.d.cts} +2 -4
- package/dist/{reporting-DEM97DhQ.d.cts → reporting-jIBILvnr.d.ts} +2 -4
- package/dist/{retail-BwcFJcVN.d.ts → retail-B04CjW3b.d.ts} +1 -3
- package/dist/{retail-DTk2XAMM.cjs → retail-CYGPU4ea.cjs} +4 -5
- package/dist/{retail-Mqd-sPMF.d.cts → retail-CmEyJQOH.d.cts} +1 -3
- package/dist/{retail-BsvpNv1F.js → retail-DXgs11ov.js} +4 -5
- package/dist/{rounding-oZ832yTD.cjs → rounding-4QKTRbMQ.cjs} +2 -3
- package/dist/{rounding-CVAPEfq_.js → rounding-CA1PVP3b.js} +2 -3
- package/dist/sql.cjs +9 -15
- package/dist/sql.d.cts +2 -12
- package/dist/sql.d.ts +2 -12
- package/dist/sql.js +9 -15
- package/dist/testing.cjs +72675 -12
- package/dist/testing.d.cts +46 -11
- package/dist/testing.d.ts +46 -11
- package/dist/testing.js +72674 -13
- package/dist/{wire-CI3Sm1iC.js → wire-B4QU8O3W.js} +584 -114
- package/dist/wire-CVNicirK.d.ts +428 -0
- package/dist/wire-CtNJmWVP.d.cts +428 -0
- package/dist/{wire-DQZ6fA3c.cjs → wire-u2tmHY0Y.cjs} +649 -113
- package/dist/wire.cjs +12 -1
- package/dist/wire.d.cts +2 -2
- package/dist/wire.d.ts +2 -2
- package/dist/wire.js +1 -2
- package/package.json +4 -3
- package/dist/wire-Cd4Q1Jmm.d.ts +0 -269
- package/dist/wire-hWuixv3w.d.cts +0 -269
|
@@ -137,8 +137,7 @@ function withinFloatNoise(major, digits) {
|
|
|
137
137
|
function unitsWithin(d, limit) {
|
|
138
138
|
return (d.units < 0n ? -d.units : d.units) <= limit;
|
|
139
139
|
}
|
|
140
|
-
|
|
141
|
-
//#region src/errors.ts
|
|
140
|
+
// src/errors.ts
|
|
142
141
|
/** The one error this package throws for bad money input. Check `code`, not the message. */
|
|
143
142
|
var MoneyError = class extends Error {
|
|
144
143
|
constructor(code, message, options = {}) {
|
|
@@ -157,8 +156,7 @@ function describeValue(value) {
|
|
|
157
156
|
return Object.prototype.toString.call(value);
|
|
158
157
|
}
|
|
159
158
|
}
|
|
160
|
-
|
|
161
|
-
//#region src/internal/rational.ts
|
|
159
|
+
// src/internal/rational.ts
|
|
162
160
|
/** `a x b / c` rounded by `mode`. `c` must not be zero; its sign is folded into the numerator. */
|
|
163
161
|
function mulDiv(a, b, c, mode) {
|
|
164
162
|
if (c === 0n) throw new RangeError("mulDiv: division by zero");
|
|
@@ -185,8 +183,7 @@ function gcd(a, b) {
|
|
|
185
183
|
while (y !== 0n) [x, y] = [y, x % y];
|
|
186
184
|
return x;
|
|
187
185
|
}
|
|
188
|
-
|
|
189
|
-
//#region src/decimal-text.ts
|
|
186
|
+
// src/decimal-text.ts
|
|
190
187
|
/**
|
|
191
188
|
* Internal: `input` as an exact Decimal. Text must be plain decimal (`'-12.50'`, `'7'`):
|
|
192
189
|
* no blank, whitespace, exponent, hex, `'.5'` or `'1.'` (`parse`). Numbers are taken by
|
|
@@ -276,6 +273,18 @@ function decimalRound(a, options) {
|
|
|
276
273
|
function decimalCompare(a, b) {
|
|
277
274
|
return compareDecimal(decimalOf(a), decimalOf(b));
|
|
278
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Whether `a` and `b` are the same decimal, exactly (`'43.0'` equals `'43.00'` and `43`). Takes what
|
|
278
|
+
* `decimalCompare` takes, but never throws: anything that is not plain decimal text, a finite number
|
|
279
|
+
* or a bigint is never equal, not even to itself.
|
|
280
|
+
*/
|
|
281
|
+
function decimalEquals(a, b) {
|
|
282
|
+
try {
|
|
283
|
+
return compareDecimal(decimalOf(a), decimalOf(b)) === 0;
|
|
284
|
+
} catch {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
279
288
|
/** `a` limited to `[min, max]` (either bound optional). `min` above `max` throws `amount_invalid`. */
|
|
280
289
|
function decimalClamp(a, bounds) {
|
|
281
290
|
const value = decimalOf(a);
|
|
@@ -296,8 +305,7 @@ function decimalWithin(a, b, tolerance, options = {}) {
|
|
|
296
305
|
}, limit);
|
|
297
306
|
return options.inclusive === false ? gap < 0 : gap <= 0;
|
|
298
307
|
}
|
|
299
|
-
|
|
300
|
-
//#region src/data/currencies.ts
|
|
308
|
+
// src/data/currencies.ts
|
|
301
309
|
const CURRENCY_ROWS = [
|
|
302
310
|
{
|
|
303
311
|
code: "AED",
|
|
@@ -3215,8 +3223,7 @@ const CURRENCY_ROWS = [
|
|
|
3215
3223
|
commaDecimal: false
|
|
3216
3224
|
}
|
|
3217
3225
|
];
|
|
3218
|
-
|
|
3219
|
-
//#region src/internal/intl.ts
|
|
3226
|
+
// src/internal/intl.ts
|
|
3220
3227
|
const DEFAULT_LOCALE = "en-US";
|
|
3221
3228
|
/**
|
|
3222
3229
|
* `locale` when the runtime supports it, else `'en-US'`. Blank, malformed or unsupported
|
|
@@ -3253,8 +3260,7 @@ function separatorsOf(locale) {
|
|
|
3253
3260
|
group: parts.find((p) => p.type === "group")?.value ?? (decimal === "." ? "," : ".")
|
|
3254
3261
|
};
|
|
3255
3262
|
}
|
|
3256
|
-
|
|
3257
|
-
//#region src/registry.ts
|
|
3263
|
+
// src/registry.ts
|
|
3258
3264
|
/** Every currency this package knows, active and historic, sorted by code. */
|
|
3259
3265
|
const CURRENCIES = Object.freeze(CURRENCY_ROWS.map((row) => Object.freeze({
|
|
3260
3266
|
...row,
|
|
@@ -3413,8 +3419,7 @@ function medusaEpsilon(code) {
|
|
|
3413
3419
|
scale: currencyInfo(code).medusaDigits
|
|
3414
3420
|
});
|
|
3415
3421
|
}
|
|
3416
|
-
|
|
3417
|
-
//#region src/money.ts
|
|
3422
|
+
// src/money.ts
|
|
3418
3423
|
const MAX_UNITS = 2n ** 46n;
|
|
3419
3424
|
const anyKnownCurrency = (currency, field) => resolveCurrency(currency, {
|
|
3420
3425
|
allowHistoric: true,
|
|
@@ -3553,21 +3558,20 @@ function medusaAmountDecimal(raw) {
|
|
|
3553
3558
|
return typeof primitive === "string" ? parseDecimal(primitive) : null;
|
|
3554
3559
|
}
|
|
3555
3560
|
const SAFE_LIMIT = BigInt(Number.MAX_SAFE_INTEGER);
|
|
3556
|
-
/** A stored integer from Medusa's shapes, or null when it is fractional, unsafe or unreadable. */
|
|
3557
|
-
function unwrapStored(raw) {
|
|
3561
|
+
/** A stored integer from Medusa's shapes (a fraction rounded half-up with `round`), or null when it is fractional, unsafe or unreadable. */
|
|
3562
|
+
function unwrapStored(raw, round) {
|
|
3558
3563
|
const parsed = medusaAmountDecimal(raw);
|
|
3559
|
-
if (!parsed || fractionDigitsOf(parsed) !== 0) return null;
|
|
3560
|
-
const whole = roundToScale(parsed, 0, "
|
|
3564
|
+
if (!parsed || !round && fractionDigitsOf(parsed) !== 0) return null;
|
|
3565
|
+
const whole = roundToScale(parsed, 0, "half-up");
|
|
3561
3566
|
return unitsWithin(whole, SAFE_LIMIT) ? Number(whole.units) : null;
|
|
3562
3567
|
}
|
|
3563
3568
|
function readStored(raw, currency, options = {}) {
|
|
3564
3569
|
const info = anyKnownCurrency(currency, options.field);
|
|
3565
|
-
const amount = unwrapStored(raw);
|
|
3570
|
+
const amount = unwrapStored(raw, options.precision === "round");
|
|
3566
3571
|
if (amount !== null) return storedFrom(amount, info);
|
|
3567
3572
|
if (options.mode === "lenient") return null;
|
|
3568
3573
|
throw new MoneyError("amount_invalid", `Invalid stored amount: ${describeValue(raw)}`, { field: options.field });
|
|
3569
3574
|
}
|
|
3570
|
-
//#endregion
|
|
3571
3575
|
Object.defineProperty(exports, "CURRENCIES", {
|
|
3572
3576
|
enumerable: true,
|
|
3573
3577
|
get: function() {
|
|
@@ -3670,6 +3674,12 @@ Object.defineProperty(exports, "decimalDivide", {
|
|
|
3670
3674
|
return decimalDivide;
|
|
3671
3675
|
}
|
|
3672
3676
|
});
|
|
3677
|
+
Object.defineProperty(exports, "decimalEquals", {
|
|
3678
|
+
enumerable: true,
|
|
3679
|
+
get: function() {
|
|
3680
|
+
return decimalEquals;
|
|
3681
|
+
}
|
|
3682
|
+
});
|
|
3673
3683
|
Object.defineProperty(exports, "decimalFromNumber", {
|
|
3674
3684
|
enumerable: true,
|
|
3675
3685
|
get: function() {
|
|
@@ -3832,6 +3842,12 @@ Object.defineProperty(exports, "multiplyDecimal", {
|
|
|
3832
3842
|
return multiplyDecimal;
|
|
3833
3843
|
}
|
|
3834
3844
|
});
|
|
3845
|
+
Object.defineProperty(exports, "normalizeDecimal", {
|
|
3846
|
+
enumerable: true,
|
|
3847
|
+
get: function() {
|
|
3848
|
+
return normalizeDecimal;
|
|
3849
|
+
}
|
|
3850
|
+
});
|
|
3835
3851
|
Object.defineProperty(exports, "numberFormat", {
|
|
3836
3852
|
enumerable: true,
|
|
3837
3853
|
get: function() {
|
|
@@ -3958,3 +3974,9 @@ Object.defineProperty(exports, "trimDecimalZeros", {
|
|
|
3958
3974
|
return trimDecimalZeros;
|
|
3959
3975
|
}
|
|
3960
3976
|
});
|
|
3977
|
+
Object.defineProperty(exports, "unitsWithin", {
|
|
3978
|
+
enumerable: true,
|
|
3979
|
+
get: function() {
|
|
3980
|
+
return unitsWithin;
|
|
3981
|
+
}
|
|
3982
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { $ as describeValue, E as resolveCurrency, dt as roundToScale, m as currencyMarks } from "./money-BxbAw7BP.js";
|
|
2
|
+
// src/internal/number-text.ts
|
|
3
3
|
const MINUS = /^[-−]/;
|
|
4
4
|
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
5
5
|
const unparseable = Object.freeze({ reason: "unparseable" });
|
|
@@ -123,12 +123,11 @@ function readNumberText(text, options = {}) {
|
|
|
123
123
|
negative = true;
|
|
124
124
|
rest = rest.slice(1);
|
|
125
125
|
}
|
|
126
|
-
const read = auto ? readAuto(negative, rest, info, options.keepFraction ? null : options.rounding ?? "half-up") : readExplicit(negative, rest, decimal, group, info);
|
|
126
|
+
const read = auto ? readAuto(negative, rest, info, options.keepFraction ? null : options.rounding ?? "half-up") : readExplicit(negative, rest, decimal, group, options.wholeCurrencyGrouping === false ? null : info);
|
|
127
127
|
if ("reason" in read) return read;
|
|
128
128
|
return {
|
|
129
129
|
...read,
|
|
130
130
|
groupMark: groupMarkOf(written, read.decimalMark)
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
|
-
//#endregion
|
|
134
133
|
export { readNumberText as n, digitGroupingSource as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_money = require("./money-
|
|
2
|
-
|
|
1
|
+
const require_money = require("./money-DWBjuuEK.cjs");
|
|
2
|
+
// src/internal/number-text.ts
|
|
3
3
|
const MINUS = /^[-−]/;
|
|
4
4
|
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
5
5
|
const unparseable = Object.freeze({ reason: "unparseable" });
|
|
@@ -123,14 +123,13 @@ function readNumberText(text, options = {}) {
|
|
|
123
123
|
negative = true;
|
|
124
124
|
rest = rest.slice(1);
|
|
125
125
|
}
|
|
126
|
-
const read = auto ? readAuto(negative, rest, info, options.keepFraction ? null : options.rounding ?? "half-up") : readExplicit(negative, rest, decimal, group, info);
|
|
126
|
+
const read = auto ? readAuto(negative, rest, info, options.keepFraction ? null : options.rounding ?? "half-up") : readExplicit(negative, rest, decimal, group, options.wholeCurrencyGrouping === false ? null : info);
|
|
127
127
|
if ("reason" in read) return read;
|
|
128
128
|
return {
|
|
129
129
|
...read,
|
|
130
130
|
groupMark: groupMarkOf(written, read.decimalMark)
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
|
-
//#endregion
|
|
134
133
|
Object.defineProperty(exports, "digitGroupingSource", {
|
|
135
134
|
enumerable: true,
|
|
136
135
|
get: function() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { X as
|
|
2
|
-
|
|
1
|
+
import { X as mulDiv, Z as ratioToDecimal, ut as pow10 } from "./money-BxbAw7BP.js";
|
|
2
|
+
// src/internal/percent.ts
|
|
3
3
|
/** 100% for a percent written with `scale` fraction digits: the denominator of its units. */
|
|
4
4
|
function hundredPercent(scale) {
|
|
5
5
|
return 100n * pow10(scale);
|
|
@@ -16,5 +16,4 @@ function basisPointsOf(units, bps, mode, denominator = 1n) {
|
|
|
16
16
|
function percentRatio(a, b, digits) {
|
|
17
17
|
return ratioToDecimal(a * 100n, b, digits);
|
|
18
18
|
}
|
|
19
|
-
//#endregion
|
|
20
19
|
export { percentRatio as i, hundredPercent as n, percentOfUnits as r, basisPointsOf as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_money = require("./money-
|
|
2
|
-
|
|
1
|
+
const require_money = require("./money-DWBjuuEK.cjs");
|
|
2
|
+
// src/internal/percent.ts
|
|
3
3
|
/** 100% for a percent written with `scale` fraction digits: the denominator of its units. */
|
|
4
4
|
function hundredPercent(scale) {
|
|
5
5
|
return 100n * require_money.pow10(scale);
|
|
@@ -16,7 +16,6 @@ function basisPointsOf(units, bps, mode, denominator = 1n) {
|
|
|
16
16
|
function percentRatio(a, b, digits) {
|
|
17
17
|
return require_money.ratioToDecimal(a * 100n, b, digits);
|
|
18
18
|
}
|
|
19
|
-
//#endregion
|
|
20
19
|
Object.defineProperty(exports, "basisPointsOf", {
|
|
21
20
|
enumerable: true,
|
|
22
21
|
get: function() {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/fx/rate.d.ts
|
|
1
|
+
import { R as DecimalText } from "./money-BkMeYDEW.js";
|
|
3
2
|
/** 1 base unit = numerator / denominator quote units, both in major units. Positive integers. */
|
|
4
3
|
interface Rate {
|
|
5
4
|
readonly numerator: bigint;
|
|
@@ -59,5 +58,4 @@ declare function invertRate(r: Rate): Rate;
|
|
|
59
58
|
declare function crossRate(table: Readonly<Record<string, Rate>>, from: string, to: string, base: string): Rate | null;
|
|
60
59
|
/** Same value (cross-multiplied), whatever the parts: 2/4 equals 1/2. */
|
|
61
60
|
declare function rateEquals(a: Rate, b: Rate): boolean;
|
|
62
|
-
//#endregion
|
|
63
61
|
export { RateRow as a, rate as c, rateFromRow as d, rateToDecimal as f, RateFromDecimalOptions as i, rateEquals as l, RATE_REF as n, crossRate as o, rateToNumber as p, Rate as r, invertRate as s, RATE_BOUNDS as t, rateFromDecimal as u };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/fx/rate.d.ts
|
|
1
|
+
import { R as DecimalText } from "./money-BkMeYDEW.cjs";
|
|
3
2
|
/** 1 base unit = numerator / denominator quote units, both in major units. Positive integers. */
|
|
4
3
|
interface Rate {
|
|
5
4
|
readonly numerator: bigint;
|
|
@@ -59,5 +58,4 @@ declare function invertRate(r: Rate): Rate;
|
|
|
59
58
|
declare function crossRate(table: Readonly<Record<string, Rate>>, from: string, to: string, base: string): Rate | null;
|
|
60
59
|
/** Same value (cross-multiplied), whatever the parts: 2/4 equals 1/2. */
|
|
61
60
|
declare function rateEquals(a: Rate, b: Rate): boolean;
|
|
62
|
-
//#endregion
|
|
63
61
|
export { RateRow as a, rate as c, rateFromRow as d, rateToDecimal as f, RateFromDecimalOptions as i, rateEquals as l, RATE_REF as n, crossRate as o, rateToNumber as p, Rate as r, invertRate as s, RATE_BOUNDS as t, rateFromDecimal as u };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const require_money = require("./money-
|
|
2
|
-
const require_own = require("./own-
|
|
3
|
-
|
|
1
|
+
const require_money = require("./money-DWBjuuEK.cjs");
|
|
2
|
+
const require_own = require("./own-C5imDkyB.cjs");
|
|
3
|
+
// src/fx/rate.ts
|
|
4
4
|
/** Denominator of every locked rate (`store_currency_rate.rate_denominator` default): 7 decimals. */
|
|
5
5
|
const RATE_REF = 10000000n;
|
|
6
6
|
/** Inclusive bounds of a locked rate's value: `lockedRateNumerator` (n >= 1 at RATE_REF, value <= 1e9). */
|
|
@@ -148,7 +148,6 @@ function rateEquals(a, b) {
|
|
|
148
148
|
const y = checkRate(b);
|
|
149
149
|
return require_money.compareRatio(x.numerator, x.denominator, y.numerator, y.denominator) === 0;
|
|
150
150
|
}
|
|
151
|
-
//#endregion
|
|
152
151
|
Object.defineProperty(exports, "RATE_BOUNDS", {
|
|
153
152
|
enumerable: true,
|
|
154
153
|
get: function() {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as hasOwnKey } from "./own-
|
|
3
|
-
|
|
1
|
+
import { $ as describeValue, B as decimalOf, J as compareRatio, Q as MoneyError, Z as ratioToDecimal, dt as roundToScale, lt as parseDecimal, pt as toDecimalText, ut as pow10 } from "./money-BxbAw7BP.js";
|
|
2
|
+
import { t as hasOwnKey } from "./own-DHxqIYqL.js";
|
|
3
|
+
// src/fx/rate.ts
|
|
4
4
|
/** Denominator of every locked rate (`store_currency_rate.rate_denominator` default): 7 decimals. */
|
|
5
5
|
const RATE_REF = 10000000n;
|
|
6
6
|
/** Inclusive bounds of a locked rate's value: `lockedRateNumerator` (n >= 1 at RATE_REF, value <= 1e9). */
|
|
@@ -148,5 +148,4 @@ function rateEquals(a, b) {
|
|
|
148
148
|
const y = checkRate(b);
|
|
149
149
|
return compareRatio(x.numerator, x.denominator, y.numerator, y.denominator) === 0;
|
|
150
150
|
}
|
|
151
|
-
//#endregion
|
|
152
151
|
export { invertRate as a, rateFromDecimal as c, rateToNumber as d, crossRate as i, rateFromRow as l, RATE_REF as n, rate as o, checkRate as r, rateEquals as s, RATE_BOUNDS as t, rateToDecimal as u };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as RateRow } from "./rate-
|
|
3
|
-
//#region src/fx/reporting.d.ts
|
|
1
|
+
import { R as DecimalText, g as CurrencyCode, i as Money } from "./money-BkMeYDEW.cjs";
|
|
2
|
+
import { a as RateRow } from "./rate-CEakW3ia.cjs";
|
|
4
3
|
/** `factors[lower code]` takes a stored amount in that currency to a stored amount in `base`. */
|
|
5
4
|
interface FactorTable {
|
|
6
5
|
readonly base: CurrencyCode;
|
|
@@ -27,5 +26,4 @@ interface ReportingTotal {
|
|
|
27
26
|
* Items whose currency has no factor are excluded and reported in `missing`.
|
|
28
27
|
*/
|
|
29
28
|
declare function toReporting(items: readonly Money[], table: FactorTable): ReportingTotal;
|
|
30
|
-
//#endregion
|
|
31
29
|
export { toReporting as i, ReportingTotal as n, storeFactors as r, FactorTable as t };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as RateRow } from "./rate-
|
|
3
|
-
//#region src/fx/reporting.d.ts
|
|
1
|
+
import { R as DecimalText, g as CurrencyCode, i as Money } from "./money-BkMeYDEW.js";
|
|
2
|
+
import { a as RateRow } from "./rate-BvvZUdSu.js";
|
|
4
3
|
/** `factors[lower code]` takes a stored amount in that currency to a stored amount in `base`. */
|
|
5
4
|
interface FactorTable {
|
|
6
5
|
readonly base: CurrencyCode;
|
|
@@ -27,5 +26,4 @@ interface ReportingTotal {
|
|
|
27
26
|
* Items whose currency has no factor are excluded and reported in `missing`.
|
|
28
27
|
*/
|
|
29
28
|
declare function toReporting(items: readonly Money[], table: FactorTable): ReportingTotal;
|
|
30
|
-
//#endregion
|
|
31
29
|
export { toReporting as i, ReportingTotal as n, storeFactors as r, FactorTable as t };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/fx/retail.d.ts
|
|
1
|
+
import { i as Money } from "./money-BkMeYDEW.js";
|
|
3
2
|
/**
|
|
4
3
|
* - `'none'`: Medusa `applyRounding` 'none': a zero-decimal currency is rounded to a whole
|
|
5
4
|
* unit (half toward +infinity, as `Math.round`: JPY 150050 gives 150100, since there is no
|
|
@@ -27,5 +26,4 @@ declare function priceRoundingParams(currency: string): {
|
|
|
27
26
|
majorUnit: 100 | 1000;
|
|
28
27
|
charm: number | null;
|
|
29
28
|
};
|
|
30
|
-
//#endregion
|
|
31
29
|
export { priceRoundingParams as n, roundRetail as r, RetailRounding as t };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
const require_money = require("./money-
|
|
2
|
-
const require_rounding = require("./rounding-
|
|
3
|
-
const require_percent = require("./percent-
|
|
4
|
-
|
|
1
|
+
const require_money = require("./money-DWBjuuEK.cjs");
|
|
2
|
+
const require_rounding = require("./rounding-4QKTRbMQ.cjs");
|
|
3
|
+
const require_percent = require("./percent-DABHiSBs.cjs");
|
|
4
|
+
// src/fx/retail.ts
|
|
5
5
|
const RETAIL_ROUNDINGS = [
|
|
6
6
|
"none",
|
|
7
7
|
"nearest",
|
|
@@ -65,7 +65,6 @@ function priceRoundingParams(currency) {
|
|
|
65
65
|
charm: info.storageDigits === 0 ? null : Number(charmOf(scale))
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
|
-
//#endregion
|
|
69
68
|
Object.defineProperty(exports, "priceRoundingParams", {
|
|
70
69
|
enumerable: true,
|
|
71
70
|
get: function() {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/fx/retail.d.ts
|
|
1
|
+
import { i as Money } from "./money-BkMeYDEW.cjs";
|
|
3
2
|
/**
|
|
4
3
|
* - `'none'`: Medusa `applyRounding` 'none': a zero-decimal currency is rounded to a whole
|
|
5
4
|
* unit (half toward +infinity, as `Math.round`: JPY 150050 gives 150100, since there is no
|
|
@@ -27,5 +26,4 @@ declare function priceRoundingParams(currency: string): {
|
|
|
27
26
|
majorUnit: 100 | 1000;
|
|
28
27
|
charm: number | null;
|
|
29
28
|
};
|
|
30
|
-
//#endregion
|
|
31
29
|
export { priceRoundingParams as n, roundRetail as r, RetailRounding as t };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as unitSize, t as mulDivToUnit } from "./rounding-
|
|
3
|
-
import { r as percentOfUnits } from "./percent-
|
|
4
|
-
|
|
1
|
+
import { $ as describeValue, E as resolveCurrency, Q as MoneyError, rt as divRound, s as storedDecimal, t as boundedMoney } from "./money-BxbAw7BP.js";
|
|
2
|
+
import { a as unitSize, t as mulDivToUnit } from "./rounding-CA1PVP3b.js";
|
|
3
|
+
import { r as percentOfUnits } from "./percent-CB3Ixjc_.js";
|
|
4
|
+
// src/fx/retail.ts
|
|
5
5
|
const RETAIL_ROUNDINGS = [
|
|
6
6
|
"none",
|
|
7
7
|
"nearest",
|
|
@@ -65,5 +65,4 @@ function priceRoundingParams(currency) {
|
|
|
65
65
|
charm: info.storageDigits === 0 ? null : Number(charmOf(scale))
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
|
-
//#endregion
|
|
69
68
|
export { retailUnits as n, roundRetail as r, priceRoundingParams as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_money = require("./money-
|
|
2
|
-
|
|
1
|
+
const require_money = require("./money-DWBjuuEK.cjs");
|
|
2
|
+
// src/rounding.ts
|
|
3
3
|
/** Internal: the size of `unit` in storage units. */
|
|
4
4
|
function unitSize(info, unit) {
|
|
5
5
|
if (unit === "storage") return 1n;
|
|
@@ -46,7 +46,6 @@ function roundMajor(major, currency, options = {}) {
|
|
|
46
46
|
const digits = options.digits === "storage" ? info.storageDigits : info.displayDigits;
|
|
47
47
|
return Number(require_money.formatDecimal(require_money.roundToScale(value, digits, options.rounding ?? "half-up")));
|
|
48
48
|
}
|
|
49
|
-
//#endregion
|
|
50
49
|
Object.defineProperty(exports, "mulDivToUnit", {
|
|
51
50
|
enumerable: true,
|
|
52
51
|
get: function() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { B as decimalOf, E as resolveCurrency, Q as MoneyError, X as mulDiv, dt as roundToScale, it as formatDecimal, s as storedDecimal, t as boundedMoney, ut as pow10 } from "./money-BxbAw7BP.js";
|
|
2
|
+
// src/rounding.ts
|
|
3
3
|
/** Internal: the size of `unit` in storage units. */
|
|
4
4
|
function unitSize(info, unit) {
|
|
5
5
|
if (unit === "storage") return 1n;
|
|
@@ -46,5 +46,4 @@ function roundMajor(major, currency, options = {}) {
|
|
|
46
46
|
const digits = options.digits === "storage" ? info.storageDigits : info.displayDigits;
|
|
47
47
|
return Number(formatDecimal(roundToScale(value, digits, options.rounding ?? "half-up")));
|
|
48
48
|
}
|
|
49
|
-
//#endregion
|
|
50
49
|
export { unitSize as a, roundToUnit as i, roundMajor as n, roundStoredDecimal as r, mulDivToUnit as t };
|
package/dist/sql.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_money = require("./money-
|
|
3
|
-
const require_rate = require("./rate-
|
|
4
|
-
const require_retail = require("./retail-
|
|
5
|
-
|
|
2
|
+
const require_money = require("./money-DWBjuuEK.cjs");
|
|
3
|
+
const require_rate = require("./rate-CEjWTATc.cjs");
|
|
4
|
+
const require_retail = require("./retail-CYGPU4ea.cjs");
|
|
5
|
+
// src/sql/guard.ts
|
|
6
6
|
const STATEMENT_BREAKERS = /;|--|\/\*|\*\/|\0/;
|
|
7
7
|
const refuse = (name, value) => new require_money.MoneyError("parse", `${name} must be a SQL expression written in code, got ${require_money.describeValue(value)}`);
|
|
8
8
|
/** Code-written text, or `parse` when it is blank, could break out of the expression or holds a `?` placeholder. */
|
|
@@ -57,8 +57,7 @@ function dialectOf(value) {
|
|
|
57
57
|
if (value !== "pg" && value !== "clickhouse") throw new require_money.MoneyError("parse", `Unknown SQL dialect: ${require_money.describeValue(value)}`);
|
|
58
58
|
return value;
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
//#region src/sql/lists.ts
|
|
60
|
+
// src/sql/lists.ts
|
|
62
61
|
const quoted = (pick) => require_money.CURRENCIES.filter(pick).map((info) => `'${info.code}'`).join(",");
|
|
63
62
|
/** Storage scale 1000: `'BHD','IQD','JOD','KWD','LYD','OMR','TND'`. */
|
|
64
63
|
const SCALE_1000_CODES = quoted((info) => info.storageScale === 1e3);
|
|
@@ -70,8 +69,7 @@ const DISPLAY_3_CODES = quoted((info) => info.displayDigits === 3);
|
|
|
70
69
|
const MEDUSA_0_CODES = quoted((info) => info.medusaDigits === 0);
|
|
71
70
|
/** Medusa core `decimal_digits` 3 (payment-status epsilon 0.001). */
|
|
72
71
|
const MEDUSA_3_CODES = quoted((info) => info.medusaDigits === 3);
|
|
73
|
-
|
|
74
|
-
//#region src/sql/amounts.ts
|
|
72
|
+
// src/sql/amounts.ts
|
|
75
73
|
const CH_STORED_SCALE = 3;
|
|
76
74
|
const CH_ROUND_SCALE = 12;
|
|
77
75
|
/** The storage divisor of the currency in `codeExpr`, keyed by code (never `currency.decimal_digits`). */
|
|
@@ -145,8 +143,7 @@ function rawAmountJsonSql(expr) {
|
|
|
145
143
|
function normalizeCurrencySql(expr) {
|
|
146
144
|
return sql`lower(btrim(${expression(expr, "expr")}))`;
|
|
147
145
|
}
|
|
148
|
-
|
|
149
|
-
//#region src/sql/clickhouse.ts
|
|
146
|
+
// src/sql/clickhouse.ts
|
|
150
147
|
/**
|
|
151
148
|
* The SELECT list and GROUP BY of per-currency purchase revenue in stored units. The event name
|
|
152
149
|
* is a ClickHouse query parameter (`{revenue_event:String}`, pass `params` as `query_params`),
|
|
@@ -165,8 +162,7 @@ function chEventRevenueByCurrencySql(cols = {}) {
|
|
|
165
162
|
params: { revenue_event: event }
|
|
166
163
|
};
|
|
167
164
|
}
|
|
168
|
-
|
|
169
|
-
//#region src/sql/ddl.ts
|
|
165
|
+
// src/sql/ddl.ts
|
|
170
166
|
/** The column type of a stored amount. */
|
|
171
167
|
const STORAGE_AMOUNT_SQL_TYPE = "BIGINT";
|
|
172
168
|
/** A column for DDL, which Postgres never parameterises: a fragment with bindings is `parse`. */
|
|
@@ -208,8 +204,7 @@ function usdToCreditsSql(expr) {
|
|
|
208
204
|
function monthlyEquivalentSql(expr) {
|
|
209
205
|
return sql`ROUND((${expression(expr, "expr")})::numeric / 12)`;
|
|
210
206
|
}
|
|
211
|
-
|
|
212
|
-
//#region src/sql/fx.ts
|
|
207
|
+
// src/sql/fx.ts
|
|
213
208
|
function factorsJson(table) {
|
|
214
209
|
require_money.resolveCurrency(table?.base, { allowHistoric: true });
|
|
215
210
|
return JSON.stringify(table.factors);
|
|
@@ -268,7 +263,6 @@ function convertStoredByColumnsSql(amountExpr, cols) {
|
|
|
268
263
|
const whole = sql`(FLOOR(${x} / ${major}) * ${major})`;
|
|
269
264
|
return sql`(CASE WHEN ${retail} = 'none' THEN ${x} WHEN ${x} <= 0 THEN ${x} WHEN ${charm} IS NULL THEN CEIL(${x} / (100 * ${major})) * (100 * ${major}) WHEN ${x} <= ${whole} + ${charm} THEN ${whole} + ${charm} ELSE ${whole} + ${major} + ${charm} END)`;
|
|
270
265
|
}
|
|
271
|
-
//#endregion
|
|
272
266
|
exports.STORAGE_AMOUNT_SQL_TYPE = STORAGE_AMOUNT_SQL_TYPE;
|
|
273
267
|
exports.chEventRevenueByCurrencySql = chEventRevenueByCurrencySql;
|
|
274
268
|
exports.convertStoredByColumnsSql = convertStoredByColumnsSql;
|
package/dist/sql.d.cts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { r as Rate } from "./rate-
|
|
2
|
-
import { t as FactorTable } from "./reporting-
|
|
3
|
-
//#region src/sql/guard.d.ts
|
|
1
|
+
import { r as Rate } from "./rate-CEakW3ia.cjs";
|
|
2
|
+
import { t as FactorTable } from "./reporting-DFNyDBdi.cjs";
|
|
4
3
|
type SqlBinding = string | number;
|
|
5
4
|
/** Text with knex `?` placeholders and the values they bind, in order: `knex.raw(f.sql, f.bindings)`. */
|
|
6
5
|
interface SqlFragment {
|
|
@@ -10,8 +9,6 @@ interface SqlFragment {
|
|
|
10
9
|
/** An expression argument: SQL text written in code (a column reference), or a builder's SqlFragment. */
|
|
11
10
|
type SqlExpression = string | SqlFragment;
|
|
12
11
|
type SqlDialect = 'pg' | 'clickhouse';
|
|
13
|
-
//#endregion
|
|
14
|
-
//#region src/sql/amounts.d.ts
|
|
15
12
|
/** The storage divisor of the currency in `codeExpr`, keyed by code (never `currency.decimal_digits`). */
|
|
16
13
|
export declare function storageDivisorSql(codeExpr: SqlExpression, dialect?: SqlDialect): SqlFragment;
|
|
17
14
|
/**
|
|
@@ -51,8 +48,6 @@ export declare function summaryAmountSql(totalsExpr: SqlExpression, key: string,
|
|
|
51
48
|
export declare function rawAmountJsonSql(expr: SqlExpression): SqlFragment;
|
|
52
49
|
/** A currency code column normalised the way Medusa stores it. */
|
|
53
50
|
export declare function normalizeCurrencySql(expr: SqlExpression): SqlFragment;
|
|
54
|
-
//#endregion
|
|
55
|
-
//#region src/sql/clickhouse.d.ts
|
|
56
51
|
interface EventRevenueColumns {
|
|
57
52
|
/** Default `'value'` (stored units). */
|
|
58
53
|
readonly value?: string;
|
|
@@ -79,8 +74,6 @@ interface EventRevenueSql {
|
|
|
79
74
|
* (verified on 24.8).
|
|
80
75
|
*/
|
|
81
76
|
export declare function chEventRevenueByCurrencySql(cols?: EventRevenueColumns): EventRevenueSql;
|
|
82
|
-
//#endregion
|
|
83
|
-
//#region src/sql/ddl.d.ts
|
|
84
77
|
/** The column type of a stored amount. */
|
|
85
78
|
export declare const STORAGE_AMOUNT_SQL_TYPE = "BIGINT";
|
|
86
79
|
/** Lower-case three-letter currency codes only. */
|
|
@@ -108,8 +101,6 @@ export declare function storageAmountCheckSql(column: SqlExpression, options?: S
|
|
|
108
101
|
export declare function usdToCreditsSql(expr: SqlExpression): SqlFragment;
|
|
109
102
|
/** A yearly price as its monthly equivalent, half away from zero, the twin of `billing.monthlyEquivalent`. */
|
|
110
103
|
export declare function monthlyEquivalentSql(expr: SqlExpression): SqlFragment;
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/sql/fx.d.ts
|
|
113
104
|
/**
|
|
114
105
|
* A stored amount in the currency of `codeExpr` converted with the bound factor table
|
|
115
106
|
* (`fx.storeFactors`): `output: 'stored'` rounds half away from zero to a storage unit of the
|
|
@@ -143,5 +134,4 @@ interface ConvertColumns {
|
|
|
143
134
|
}
|
|
144
135
|
/** `convertStoredSql` with the factor and rounding read from columns, so one statement converts many currencies. */
|
|
145
136
|
export declare function convertStoredByColumnsSql(amountExpr: SqlExpression, cols: ConvertColumns): SqlFragment;
|
|
146
|
-
//#endregion
|
|
147
137
|
export type { ConvertColumns, EventRevenueColumns, EventRevenueSql, SqlBinding, SqlDialect, SqlExpression, SqlFragment, StorageAmountCheckOptions };
|
package/dist/sql.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { r as Rate } from "./rate-
|
|
2
|
-
import { t as FactorTable } from "./reporting-
|
|
3
|
-
//#region src/sql/guard.d.ts
|
|
1
|
+
import { r as Rate } from "./rate-BvvZUdSu.js";
|
|
2
|
+
import { t as FactorTable } from "./reporting-jIBILvnr.js";
|
|
4
3
|
type SqlBinding = string | number;
|
|
5
4
|
/** Text with knex `?` placeholders and the values they bind, in order: `knex.raw(f.sql, f.bindings)`. */
|
|
6
5
|
interface SqlFragment {
|
|
@@ -10,8 +9,6 @@ interface SqlFragment {
|
|
|
10
9
|
/** An expression argument: SQL text written in code (a column reference), or a builder's SqlFragment. */
|
|
11
10
|
type SqlExpression = string | SqlFragment;
|
|
12
11
|
type SqlDialect = 'pg' | 'clickhouse';
|
|
13
|
-
//#endregion
|
|
14
|
-
//#region src/sql/amounts.d.ts
|
|
15
12
|
/** The storage divisor of the currency in `codeExpr`, keyed by code (never `currency.decimal_digits`). */
|
|
16
13
|
export declare function storageDivisorSql(codeExpr: SqlExpression, dialect?: SqlDialect): SqlFragment;
|
|
17
14
|
/**
|
|
@@ -51,8 +48,6 @@ export declare function summaryAmountSql(totalsExpr: SqlExpression, key: string,
|
|
|
51
48
|
export declare function rawAmountJsonSql(expr: SqlExpression): SqlFragment;
|
|
52
49
|
/** A currency code column normalised the way Medusa stores it. */
|
|
53
50
|
export declare function normalizeCurrencySql(expr: SqlExpression): SqlFragment;
|
|
54
|
-
//#endregion
|
|
55
|
-
//#region src/sql/clickhouse.d.ts
|
|
56
51
|
interface EventRevenueColumns {
|
|
57
52
|
/** Default `'value'` (stored units). */
|
|
58
53
|
readonly value?: string;
|
|
@@ -79,8 +74,6 @@ interface EventRevenueSql {
|
|
|
79
74
|
* (verified on 24.8).
|
|
80
75
|
*/
|
|
81
76
|
export declare function chEventRevenueByCurrencySql(cols?: EventRevenueColumns): EventRevenueSql;
|
|
82
|
-
//#endregion
|
|
83
|
-
//#region src/sql/ddl.d.ts
|
|
84
77
|
/** The column type of a stored amount. */
|
|
85
78
|
export declare const STORAGE_AMOUNT_SQL_TYPE = "BIGINT";
|
|
86
79
|
/** Lower-case three-letter currency codes only. */
|
|
@@ -108,8 +101,6 @@ export declare function storageAmountCheckSql(column: SqlExpression, options?: S
|
|
|
108
101
|
export declare function usdToCreditsSql(expr: SqlExpression): SqlFragment;
|
|
109
102
|
/** A yearly price as its monthly equivalent, half away from zero, the twin of `billing.monthlyEquivalent`. */
|
|
110
103
|
export declare function monthlyEquivalentSql(expr: SqlExpression): SqlFragment;
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/sql/fx.d.ts
|
|
113
104
|
/**
|
|
114
105
|
* A stored amount in the currency of `codeExpr` converted with the bound factor table
|
|
115
106
|
* (`fx.storeFactors`): `output: 'stored'` rounds half away from zero to a storage unit of the
|
|
@@ -143,5 +134,4 @@ interface ConvertColumns {
|
|
|
143
134
|
}
|
|
144
135
|
/** `convertStoredSql` with the factor and rounding read from columns, so one statement converts many currencies. */
|
|
145
136
|
export declare function convertStoredByColumnsSql(amountExpr: SqlExpression, cols: ConvertColumns): SqlFragment;
|
|
146
|
-
//#endregion
|
|
147
137
|
export type { ConvertColumns, EventRevenueColumns, EventRevenueSql, SqlBinding, SqlDialect, SqlExpression, SqlFragment, StorageAmountCheckOptions };
|