@ducatjs/money 0.0.0 → 0.1.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.
Files changed (285) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +51 -0
  3. package/README.md +617 -3
  4. package/assets/CREDITS.md +12 -0
  5. package/data/adyen-fx.json +19 -0
  6. package/data/adyen.json +171 -0
  7. package/data/braintree.json +223 -0
  8. package/data/cash-rounding.json +399 -0
  9. package/data/checkout.json +230 -0
  10. package/data/iso4217-list-one.xml +1956 -0
  11. package/data/klarna.json +95 -0
  12. package/data/paypal.json +65 -0
  13. package/data/stripe-fx.json +17 -0
  14. package/data/stripe.json +285 -0
  15. package/dist/adyen-fx.d.ts +25 -0
  16. package/dist/adyen-fx.d.ts.map +1 -0
  17. package/dist/adyen-fx.js +122 -0
  18. package/dist/adyen-fx.js.map +1 -0
  19. package/dist/adyen.d.ts +25 -0
  20. package/dist/adyen.d.ts.map +1 -0
  21. package/dist/adyen.js +34 -0
  22. package/dist/adyen.js.map +1 -0
  23. package/dist/allocate.d.ts +72 -0
  24. package/dist/allocate.d.ts.map +1 -0
  25. package/dist/allocate.js +314 -0
  26. package/dist/allocate.js.map +1 -0
  27. package/dist/bag.d.ts +58 -0
  28. package/dist/bag.d.ts.map +1 -0
  29. package/dist/bag.js +122 -0
  30. package/dist/bag.js.map +1 -0
  31. package/dist/braintree.d.ts +34 -0
  32. package/dist/braintree.d.ts.map +1 -0
  33. package/dist/braintree.js +45 -0
  34. package/dist/braintree.js.map +1 -0
  35. package/dist/cash.d.ts +35 -0
  36. package/dist/cash.d.ts.map +1 -0
  37. package/dist/cash.js +86 -0
  38. package/dist/cash.js.map +1 -0
  39. package/dist/checkout.d.ts +42 -0
  40. package/dist/checkout.d.ts.map +1 -0
  41. package/dist/checkout.js +52 -0
  42. package/dist/checkout.js.map +1 -0
  43. package/dist/convert.d.ts +51 -0
  44. package/dist/convert.d.ts.map +1 -0
  45. package/dist/convert.js +55 -0
  46. package/dist/convert.js.map +1 -0
  47. package/dist/currency.d.ts +22 -0
  48. package/dist/currency.d.ts.map +1 -0
  49. package/dist/currency.js +38 -0
  50. package/dist/currency.js.map +1 -0
  51. package/dist/decimal.d.ts +52 -0
  52. package/dist/decimal.d.ts.map +1 -0
  53. package/dist/decimal.js +251 -0
  54. package/dist/decimal.js.map +1 -0
  55. package/dist/ecb.d.ts +17 -0
  56. package/dist/ecb.d.ts.map +1 -0
  57. package/dist/ecb.js +147 -0
  58. package/dist/ecb.js.map +1 -0
  59. package/dist/exact.d.ts +62 -0
  60. package/dist/exact.d.ts.map +1 -0
  61. package/dist/exact.js +147 -0
  62. package/dist/exact.js.map +1 -0
  63. package/dist/fetch.d.ts +58 -0
  64. package/dist/fetch.d.ts.map +1 -0
  65. package/dist/fetch.js +79 -0
  66. package/dist/fetch.js.map +1 -0
  67. package/dist/fixed.d.ts +121 -0
  68. package/dist/fixed.d.ts.map +1 -0
  69. package/dist/fixed.js +420 -0
  70. package/dist/fixed.js.map +1 -0
  71. package/dist/fn.d.ts +27 -0
  72. package/dist/fn.d.ts.map +1 -0
  73. package/dist/fn.js +30 -0
  74. package/dist/fn.js.map +1 -0
  75. package/dist/format.d.ts +13 -0
  76. package/dist/format.d.ts.map +1 -0
  77. package/dist/format.js +90 -0
  78. package/dist/format.js.map +1 -0
  79. package/dist/generated/adyen.d.ts +3 -0
  80. package/dist/generated/adyen.d.ts.map +1 -0
  81. package/dist/generated/adyen.js +144 -0
  82. package/dist/generated/adyen.js.map +1 -0
  83. package/dist/generated/braintree.d.ts +3 -0
  84. package/dist/generated/braintree.d.ts.map +1 -0
  85. package/dist/generated/braintree.js +140 -0
  86. package/dist/generated/braintree.js.map +1 -0
  87. package/dist/generated/cash.d.ts +12 -0
  88. package/dist/generated/cash.d.ts.map +1 -0
  89. package/dist/generated/cash.js +364 -0
  90. package/dist/generated/cash.js.map +1 -0
  91. package/dist/generated/checkout.d.ts +3 -0
  92. package/dist/generated/checkout.d.ts.map +1 -0
  93. package/dist/generated/checkout.js +151 -0
  94. package/dist/generated/checkout.js.map +1 -0
  95. package/dist/generated/iso4217.d.ts +174 -0
  96. package/dist/generated/iso4217.d.ts.map +1 -0
  97. package/dist/generated/iso4217.js +175 -0
  98. package/dist/generated/iso4217.js.map +1 -0
  99. package/dist/generated/kernel.d.ts +17 -0
  100. package/dist/generated/kernel.d.ts.map +1 -0
  101. package/dist/generated/kernel.js +208 -0
  102. package/dist/generated/kernel.js.map +1 -0
  103. package/dist/generated/klarna.d.ts +3 -0
  104. package/dist/generated/klarna.d.ts.map +1 -0
  105. package/dist/generated/klarna.js +21 -0
  106. package/dist/generated/klarna.js.map +1 -0
  107. package/dist/generated/paypal.d.ts +3 -0
  108. package/dist/generated/paypal.d.ts.map +1 -0
  109. package/dist/generated/paypal.js +31 -0
  110. package/dist/generated/paypal.js.map +1 -0
  111. package/dist/generated/stripe.d.ts +8 -0
  112. package/dist/generated/stripe.d.ts.map +1 -0
  113. package/dist/generated/stripe.js +150 -0
  114. package/dist/generated/stripe.js.map +1 -0
  115. package/dist/index.d.ts +11 -0
  116. package/dist/index.d.ts.map +1 -0
  117. package/dist/index.js +11 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/klarna.d.ts +31 -0
  120. package/dist/klarna.d.ts.map +1 -0
  121. package/dist/klarna.js +38 -0
  122. package/dist/klarna.js.map +1 -0
  123. package/dist/money.d.ts +218 -0
  124. package/dist/money.d.ts.map +1 -0
  125. package/dist/money.js +577 -0
  126. package/dist/money.js.map +1 -0
  127. package/dist/parse.d.ts +11 -0
  128. package/dist/parse.d.ts.map +1 -0
  129. package/dist/parse.js +132 -0
  130. package/dist/parse.js.map +1 -0
  131. package/dist/paypal.d.ts +41 -0
  132. package/dist/paypal.d.ts.map +1 -0
  133. package/dist/paypal.js +58 -0
  134. package/dist/paypal.js.map +1 -0
  135. package/dist/plugins/adyen.d.ts +15 -0
  136. package/dist/plugins/adyen.d.ts.map +1 -0
  137. package/dist/plugins/adyen.js +10 -0
  138. package/dist/plugins/adyen.js.map +1 -0
  139. package/dist/plugins/all.d.ts +22 -0
  140. package/dist/plugins/all.d.ts.map +1 -0
  141. package/dist/plugins/all.js +23 -0
  142. package/dist/plugins/all.js.map +1 -0
  143. package/dist/plugins/allocate.d.ts +20 -0
  144. package/dist/plugins/allocate.d.ts.map +1 -0
  145. package/dist/plugins/allocate.js +26 -0
  146. package/dist/plugins/allocate.js.map +1 -0
  147. package/dist/plugins/braintree.d.ts +12 -0
  148. package/dist/plugins/braintree.d.ts.map +1 -0
  149. package/dist/plugins/braintree.js +12 -0
  150. package/dist/plugins/braintree.js.map +1 -0
  151. package/dist/plugins/cash.d.ts +19 -0
  152. package/dist/plugins/cash.d.ts.map +1 -0
  153. package/dist/plugins/cash.js +16 -0
  154. package/dist/plugins/cash.js.map +1 -0
  155. package/dist/plugins/checkout.d.ts +21 -0
  156. package/dist/plugins/checkout.d.ts.map +1 -0
  157. package/dist/plugins/checkout.js +16 -0
  158. package/dist/plugins/checkout.js.map +1 -0
  159. package/dist/plugins/convert.d.ts +20 -0
  160. package/dist/plugins/convert.d.ts.map +1 -0
  161. package/dist/plugins/convert.js +13 -0
  162. package/dist/plugins/convert.js.map +1 -0
  163. package/dist/plugins/exact.d.ts +15 -0
  164. package/dist/plugins/exact.d.ts.map +1 -0
  165. package/dist/plugins/exact.js +13 -0
  166. package/dist/plugins/exact.js.map +1 -0
  167. package/dist/plugins/fixed.d.ts +17 -0
  168. package/dist/plugins/fixed.d.ts.map +1 -0
  169. package/dist/plugins/fixed.js +13 -0
  170. package/dist/plugins/fixed.js.map +1 -0
  171. package/dist/plugins/format.d.ts +9 -0
  172. package/dist/plugins/format.d.ts.map +1 -0
  173. package/dist/plugins/format.js +16 -0
  174. package/dist/plugins/format.js.map +1 -0
  175. package/dist/plugins/klarna.d.ts +10 -0
  176. package/dist/plugins/klarna.d.ts.map +1 -0
  177. package/dist/plugins/klarna.js +10 -0
  178. package/dist/plugins/klarna.js.map +1 -0
  179. package/dist/plugins/paypal.d.ts +14 -0
  180. package/dist/plugins/paypal.d.ts.map +1 -0
  181. package/dist/plugins/paypal.js +16 -0
  182. package/dist/plugins/paypal.js.map +1 -0
  183. package/dist/plugins/prisma.d.ts +8 -0
  184. package/dist/plugins/prisma.d.ts.map +1 -0
  185. package/dist/plugins/prisma.js +13 -0
  186. package/dist/plugins/prisma.js.map +1 -0
  187. package/dist/plugins/stripe.d.ts +14 -0
  188. package/dist/plugins/stripe.d.ts.map +1 -0
  189. package/dist/plugins/stripe.js +16 -0
  190. package/dist/plugins/stripe.js.map +1 -0
  191. package/dist/plugins/tax.d.ts +21 -0
  192. package/dist/plugins/tax.d.ts.map +1 -0
  193. package/dist/plugins/tax.js +18 -0
  194. package/dist/plugins/tax.js.map +1 -0
  195. package/dist/prisma.d.ts +66 -0
  196. package/dist/prisma.d.ts.map +1 -0
  197. package/dist/prisma.js +66 -0
  198. package/dist/prisma.js.map +1 -0
  199. package/dist/processor.d.ts +28 -0
  200. package/dist/processor.d.ts.map +1 -0
  201. package/dist/processor.js +118 -0
  202. package/dist/processor.js.map +1 -0
  203. package/dist/provider.d.ts +89 -0
  204. package/dist/provider.d.ts.map +1 -0
  205. package/dist/provider.js +240 -0
  206. package/dist/provider.js.map +1 -0
  207. package/dist/rate.d.ts +119 -0
  208. package/dist/rate.d.ts.map +1 -0
  209. package/dist/rate.js +271 -0
  210. package/dist/rate.js.map +1 -0
  211. package/dist/rounding-error.d.ts +9 -0
  212. package/dist/rounding-error.d.ts.map +1 -0
  213. package/dist/rounding-error.js +9 -0
  214. package/dist/rounding-error.js.map +1 -0
  215. package/dist/rounding.d.ts +50 -0
  216. package/dist/rounding.d.ts.map +1 -0
  217. package/dist/rounding.js +87 -0
  218. package/dist/rounding.js.map +1 -0
  219. package/dist/stripe-fx.d.ts +22 -0
  220. package/dist/stripe-fx.d.ts.map +1 -0
  221. package/dist/stripe-fx.js +30 -0
  222. package/dist/stripe-fx.js.map +1 -0
  223. package/dist/stripe.d.ts +36 -0
  224. package/dist/stripe.d.ts.map +1 -0
  225. package/dist/stripe.js +39 -0
  226. package/dist/stripe.js.map +1 -0
  227. package/dist/tax.d.ts +29 -0
  228. package/dist/tax.d.ts.map +1 -0
  229. package/dist/tax.js +39 -0
  230. package/dist/tax.js.map +1 -0
  231. package/package.json +169 -5
  232. package/src/adyen-fx.ts +154 -0
  233. package/src/adyen.ts +46 -0
  234. package/src/allocate.ts +393 -0
  235. package/src/bag.ts +170 -0
  236. package/src/braintree.ts +71 -0
  237. package/src/cash.ts +115 -0
  238. package/src/checkout.ts +77 -0
  239. package/src/convert.ts +159 -0
  240. package/src/currency.ts +61 -0
  241. package/src/decimal.ts +283 -0
  242. package/src/ecb.ts +162 -0
  243. package/src/exact.ts +182 -0
  244. package/src/fetch.ts +127 -0
  245. package/src/fixed.ts +558 -0
  246. package/src/fn.ts +80 -0
  247. package/src/format.ts +106 -0
  248. package/src/generated/adyen.ts +144 -0
  249. package/src/generated/braintree.ts +140 -0
  250. package/src/generated/cash.ts +375 -0
  251. package/src/generated/checkout.ts +151 -0
  252. package/src/generated/iso4217.ts +179 -0
  253. package/src/generated/kernel.ts +230 -0
  254. package/src/generated/klarna.ts +21 -0
  255. package/src/generated/paypal.ts +31 -0
  256. package/src/generated/stripe.ts +150 -0
  257. package/src/index.ts +37 -0
  258. package/src/klarna.ts +54 -0
  259. package/src/money.ts +781 -0
  260. package/src/parse.ts +144 -0
  261. package/src/paypal.ts +93 -0
  262. package/src/plugins/adyen.ts +25 -0
  263. package/src/plugins/all.ts +47 -0
  264. package/src/plugins/allocate.ts +53 -0
  265. package/src/plugins/braintree.ts +23 -0
  266. package/src/plugins/cash.ts +33 -0
  267. package/src/plugins/checkout.ts +32 -0
  268. package/src/plugins/convert.ts +50 -0
  269. package/src/plugins/exact.ts +23 -0
  270. package/src/plugins/fixed.ts +28 -0
  271. package/src/plugins/format.ts +28 -0
  272. package/src/plugins/klarna.ts +21 -0
  273. package/src/plugins/paypal.ts +29 -0
  274. package/src/plugins/prisma.ts +24 -0
  275. package/src/plugins/stripe.ts +29 -0
  276. package/src/plugins/tax.ts +41 -0
  277. package/src/prisma.ts +118 -0
  278. package/src/processor.ts +181 -0
  279. package/src/provider.ts +324 -0
  280. package/src/rate.ts +377 -0
  281. package/src/rounding-error.ts +8 -0
  282. package/src/rounding.ts +122 -0
  283. package/src/stripe-fx.ts +49 -0
  284. package/src/stripe.ts +69 -0
  285. package/src/tax.ts +71 -0
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Braintree's amount view (https://developer.paypal.com/braintree/docs/reference/general/currencies,
3
+ * encoded in data/braintree.json). `amount` is a decimal string in major units, "formatted like
4
+ * '10' or '10.00'":
5
+ * - zero-decimal currencies can't include decimals: BIF CLP DJF GNF ISK JPY KMF KRW LAK PYG RWF UGX
6
+ * VND VUV XAF XOF XPF (LAK has 2 in ISO 4217);
7
+ * - the others take their ISO decimals ("10.00"). No three-decimal currency is listed.
8
+ * Amounts must be greater than zero, refunds and credits included.
9
+ */
10
+ import { BRAINTREE_EXPONENTS } from './generated/braintree.js';
11
+ import { type CurrencyInput, type Money } from './money.js';
12
+ import type { RoundingMode } from './rounding.js';
13
+ export { BRAINTREE_EXPONENTS };
14
+ /** The number of decimals Braintree takes for a currency (JPY 0, LAK 0, USD 2). */
15
+ export declare function braintreeExponent(currency: string): number;
16
+ /** Whether Braintree lists the currency (case-insensitive). */
17
+ export declare function isBraintreeCurrency(currency: string): boolean;
18
+ /** Options for the Braintree amount: how to round when Braintree needs a coarser amount. */
19
+ export interface BraintreeAmountOptions {
20
+ /**
21
+ * How to round when Braintree needs fewer decimals than the value has (default 'unnecessary':
22
+ * throw).
23
+ */
24
+ readonly rounding?: RoundingMode;
25
+ }
26
+ /**
27
+ * The exact `amount` string: toBraintree(money('10', 'USD')) === '10.00', JPY 100 -> '100'.
28
+ * Braintree has no currency field on a transaction: the merchant account sets it.
29
+ * Throws a RangeError for zero or negative amounts.
30
+ */
31
+ export declare function toBraintree(money: Money, options?: BraintreeAmountOptions): string;
32
+ /** Reads a Braintree amount string exactly: fromBraintree('10.00', 'USD'). */
33
+ export declare function fromBraintree<C extends CurrencyInput>(amount: string, currency: C): Money<Uppercase<C>>;
34
+ //# sourceMappingURL=braintree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"braintree.d.ts","sourceRoot":"","sources":["../src/braintree.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAE5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,OAAO,EAAE,mBAAmB,EAAE,CAAC;AAK/B,mFAAmF;AACnF,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAM1D;AAED,+DAA+D;AAC/D,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAE7D;AAED,4FAA4F;AAC5F,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;CAClC;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,sBAAsB,GAAG,MAAM,CAWlF;AAED,8EAA8E;AAC9E,wBAAgB,aAAa,CAAC,CAAC,SAAS,aAAa,EACnD,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,GACV,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAMrB"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Braintree's amount view (https://developer.paypal.com/braintree/docs/reference/general/currencies,
3
+ * encoded in data/braintree.json). `amount` is a decimal string in major units, "formatted like
4
+ * '10' or '10.00'":
5
+ * - zero-decimal currencies can't include decimals: BIF CLP DJF GNF ISK JPY KMF KRW LAK PYG RWF UGX
6
+ * VND VUV XAF XOF XPF (LAK has 2 in ISO 4217);
7
+ * - the others take their ISO decimals ("10.00"). No three-decimal currency is listed.
8
+ * Amounts must be greater than zero, refunds and credits included.
9
+ */
10
+ import { BRAINTREE_EXPONENTS } from './generated/braintree.js';
11
+ import {} from './money.js';
12
+ import { fromProcessorDecimal, toProcessorDecimal } from './processor.js';
13
+ export { BRAINTREE_EXPONENTS };
14
+ /** "Can only contain numbers and one decimal point (e.g. x.xx)". */
15
+ const AMOUNT_PATTERN = /^[0-9]+(\.[0-9]+)?$/;
16
+ /** The number of decimals Braintree takes for a currency (JPY 0, LAK 0, USD 2). */
17
+ export function braintreeExponent(currency) {
18
+ const exponent = BRAINTREE_EXPONENTS[currency.toUpperCase()];
19
+ if (exponent === undefined) {
20
+ throw new RangeError(`${currency} is not a Braintree currency`);
21
+ }
22
+ return exponent;
23
+ }
24
+ /** Whether Braintree lists the currency (case-insensitive). */
25
+ export function isBraintreeCurrency(currency) {
26
+ return BRAINTREE_EXPONENTS[currency.toUpperCase()] !== undefined;
27
+ }
28
+ /**
29
+ * The exact `amount` string: toBraintree(money('10', 'USD')) === '10.00', JPY 100 -> '100'.
30
+ * Braintree has no currency field on a transaction: the merchant account sets it.
31
+ * Throws a RangeError for zero or negative amounts.
32
+ */
33
+ export function toBraintree(money, options) {
34
+ const rounding = options?.rounding ?? 'unnecessary';
35
+ // 81531 "Amount must be greater than zero"; 81501: credits are positive too ("x.xx, not -x.xx").
36
+ return toProcessorDecimal('Braintree', money, braintreeExponent(money.currency), rounding, 'positive');
37
+ }
38
+ /** Reads a Braintree amount string exactly: fromBraintree('10.00', 'USD'). */
39
+ export function fromBraintree(amount, currency) {
40
+ const code = currency.toUpperCase();
41
+ // Validates the currency before parsing, so an unlisted currency is the error reported.
42
+ braintreeExponent(code);
43
+ return fromProcessorDecimal('Braintree', amount, code, AMOUNT_PATTERN);
44
+ }
45
+ //# sourceMappingURL=braint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,CAAC,CAAC;IACxB,OAAO,oBAAoB,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC;AACzE,CAAC"}
package/dist/cash.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { CASH_RULES, type CashRule } from './generated/cash.js';
2
+ import { type Money } from './money.js';
3
+ import { type RoundingMode } from './rounding.js';
4
+ /**
5
+ * Rounds to a multiple of `increment` (same currency): roundToIncrement(CHF 10.03, CHF 0.05) = CHF
6
+ * 10.05. Default rounding 'halfExpand' (nearest, ties away from zero), which is what the statutory
7
+ * cash-rounding rules (SE, NO, DK, HU, CZ, FI, BE ...) prescribe.
8
+ */
9
+ export declare function roundToIncrement<C extends string>(money: Money<C>, increment: Money<NoInfer<C>>, rounding?: RoundingMode): Money<C>;
10
+ /**
11
+ * The cash-rounding rule for a currency, optionally narrowed by country (ISO 3166 alpha-2).
12
+ * EUR has no euro-wide rule: pass the country (BE, NL, FI, IE, IT, SK, EE, LT).
13
+ */
14
+ export declare function cashRule(currency: string, country?: string): CashRule | undefined;
15
+ /**
16
+ * Options for `cashRound`: the country (needed for EUR), the rounding mode, and whether to use
17
+ * unverified rules.
18
+ */
19
+ export interface CashRoundOptions {
20
+ readonly country?: string;
21
+ readonly rounding?: RoundingMode;
22
+ /**
23
+ * Apply rules that are only backed by secondary sources (default true; see
24
+ * data/cash-rounding.json).
25
+ */
26
+ readonly includeUnverified?: boolean;
27
+ }
28
+ /**
29
+ * Rounds a cash total to the smallest legal/practical cash step (CHF 0.05, SEK 1, DKK 0.50, CAD
30
+ * 0.05, HUF 5, EUR 0.05 in BE/NL/FI/IE/...). Card and other electronic payments are NOT rounded:
31
+ * only call this for cash tenders. Returns m unchanged when no rule applies.
32
+ */
33
+ export declare function cashRound<C extends string>(money: Money<C>, options?: CashRoundOptions): Money<C>;
34
+ export { CASH_RULES, type CashRule };
35
+ //# sourceMappingURL=cash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cash.d.ts","sourceRoot":"","sources":["../src/cash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAiD,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AACvF,OAAO,EAAwC,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAGxF;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAC/C,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,SAAS,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAC5B,QAAQ,GAAE,YAA2B,GACpC,KAAK,CAAC,CAAC,CAAC,CAyBV;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS,CAkBjF;AAuBD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,OAAO,CAAC;CACtC;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,KAAK,CAAC,CAAC,CAAC,CAOjG;AAED,OAAO,EAAE,UAAU,EAAE,KAAK,QAAQ,EAAE,CAAC"}
package/dist/cash.js ADDED
@@ -0,0 +1,86 @@
1
+ import { CASH_RULES } from './generated/cash.js';
2
+ import { CurrencyMismatchError, make, scaleUp, toScale } from './money.js';
3
+ import { divideAndRound, divideAndRoundBigint } from './rounding.js';
4
+ import { currencyExponent } from './currency.js';
5
+ /**
6
+ * Rounds to a multiple of `increment` (same currency): roundToIncrement(CHF 10.03, CHF 0.05) = CHF
7
+ * 10.05. Default rounding 'halfExpand' (nearest, ties away from zero), which is what the statutory
8
+ * cash-rounding rules (SE, NO, DK, HU, CZ, FI, BE ...) prescribe.
9
+ */
10
+ export function roundToIncrement(money, increment, rounding = 'halfExpand') {
11
+ if (money.currency !== increment.currency) {
12
+ throw new CurrencyMismatchError(money.currency, increment.currency);
13
+ }
14
+ if (!(increment.minor > 0)) {
15
+ throw new RangeError('increment must be positive');
16
+ }
17
+ const scale = Math.max(money.scale, increment.scale);
18
+ const minor = scaleUp(money.minor, scale - money.scale);
19
+ const step = scaleUp(increment.minor, scale - increment.scale);
20
+ if (typeof minor === 'number' && typeof step === 'number') {
21
+ const steps = divideAndRound(minor, step, rounding);
22
+ const rounded = steps * step;
23
+ if (Number.isSafeInteger(rounded)) {
24
+ return toScale(make(money.currency, rounded, scale), money.scale > scale ? money.scale : scale);
25
+ }
26
+ }
27
+ return make(money.currency, divideAndRoundBigint(BigInt(minor), BigInt(step), rounding) * BigInt(step), scale);
28
+ }
29
+ /**
30
+ * The cash-rounding rule for a currency, optionally narrowed by country (ISO 3166 alpha-2).
31
+ * EUR has no euro-wide rule: pass the country (BE, NL, FI, IE, IT, SK, EE, LT).
32
+ */
33
+ export function cashRule(currency, country) {
34
+ const rules = cashRulesFor(currency);
35
+ if (rules === undefined) {
36
+ return undefined;
37
+ }
38
+ if (country !== undefined) {
39
+ for (let i = 0; i < rules.length; i++) {
40
+ if (rules[i].countries.includes(country)) {
41
+ return rules[i];
42
+ }
43
+ }
44
+ return undefined;
45
+ }
46
+ // EUR has no euro-wide rule, so it needs a country
47
+ if (currency === 'EUR') {
48
+ return undefined;
49
+ }
50
+ return rules[0];
51
+ }
52
+ // CASH_RULES grouped by currency, in table order. Built on first use, not at import, so the module
53
+ // stays free of side effects; cashRound calls cashRule on every amount, and a scan of the whole table
54
+ // was most of its cost.
55
+ let rulesByCurrency;
56
+ function cashRulesFor(currency) {
57
+ if (rulesByCurrency === undefined) {
58
+ rulesByCurrency = new Map();
59
+ for (let i = 0; i < CASH_RULES.length; i++) {
60
+ const rule = CASH_RULES[i];
61
+ const forCurrency = rulesByCurrency.get(rule.currency);
62
+ if (forCurrency === undefined) {
63
+ rulesByCurrency.set(rule.currency, [rule]);
64
+ }
65
+ else {
66
+ forCurrency.push(rule);
67
+ }
68
+ }
69
+ }
70
+ return rulesByCurrency.get(currency);
71
+ }
72
+ /**
73
+ * Rounds a cash total to the smallest legal/practical cash step (CHF 0.05, SEK 1, DKK 0.50, CAD
74
+ * 0.05, HUF 5, EUR 0.05 in BE/NL/FI/IE/...). Card and other electronic payments are NOT rounded:
75
+ * only call this for cash tenders. Returns m unchanged when no rule applies.
76
+ */
77
+ export function cashRound(money, options) {
78
+ const rule = cashRule(money.currency, options?.country);
79
+ if (!rule || (options?.includeUnverified === false && !rule.verified)) {
80
+ return money;
81
+ }
82
+ const inc = make(money.currency, rule.increment, currencyExponent(money.currency));
83
+ return roundToIncrement(money, inc, options?.rounding ?? 'halfExpand');
84
+ }
85
+ export { CASH_RULES };
86
+ //# sourceMappingURL=cash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cash.js","sourceRoot":"","sources":["../src/cash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAiB,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAc,MAAM,YAAY,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAqB,MAAM,eAAe,CAAC;AACxF,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAe,EACf,SAA4B,EAC5B,WAAyB,YAAY;IAErC,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,CAAC,QAAQ,EAAE,CAAC;QAC1C,MAAM,IAAI,qBAAqB,CAAC,KAAK,CAAC,QAA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AI,CAAC,IAAI,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AACvC,CAAC;AAgBD;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAmB,KAAe,EAAE,OAA0B;IACrF,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACxD,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,iBAAiB,KAAK,KAAK,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QACtE,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,gBAAgB,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;IACnF,OAAO,gBAAgB,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,IAAI,YAAY,CAAC,CAAC;AACzE,CAAC;AAED,OAAO,EAAE,UAAU,EAAiB,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Checkout.com's amount view (https://www.checkout.com/docs/payments/accept-payments/format-the-amount-value,
3
+ * encoded in data/checkout.json). `amount` is an integer:
4
+ * - the full amount: BIF DJF GNF ISK JPY KMF KRW PYG RWF UGX VUV VND XAF XOF XPF
5
+ * - divided by 1000, last digit 0: BHD IQD JOD KWD LYD OMR TND
6
+ * - divided by 100 for every other currency. CLP is here too (ISO says 0 decimals), and for payment
7
+ * requests its last two digits must be 00 (5000 CLP -> 500000). Standalone authentication is
8
+ * exempt.
9
+ */
10
+ import { CHECKOUT_RULES } from './generated/checkout.js';
11
+ import { type CurrencyInput, type Money } from './money.js';
12
+ import type { RoundingMode } from './rounding.js';
13
+ export { CHECKOUT_RULES };
14
+ /** Whether Checkout.com lists the currency (case-insensitive). */
15
+ export declare function isCheckoutCurrency(currency: string): boolean;
16
+ /** The exponent Checkout.com's API uses for a currency (CLP 2, JPY 0, BHD 3). */
17
+ export declare function checkoutExponent(currency: string): number;
18
+ /** Options for the Checkout.com amount: how to round when Checkout.com needs a coarser amount. */
19
+ export interface CheckoutAmountOptions {
20
+ /** How to round when Checkout.com needs a coarser amount (default 'unnecessary': throw). */
21
+ readonly rounding?: RoundingMode;
22
+ /**
23
+ * An authentication request to the Standalone API, where CLP's "last two digits 00" rule does not
24
+ * apply.
25
+ */
26
+ readonly standalone?: boolean;
27
+ }
28
+ /**
29
+ * The integer `amount` to send, e.g. toCheckoutAmount(money('5000', 'CLP')) === 500000.
30
+ * Throws a RangeError for negative amounts (refunds are sent as positive amounts).
31
+ * SLL and XDR are on Checkout.com's list but not in ISO 4217's active list: call
32
+ * `defineCurrency('SLL', 2)` or `defineCurrency('XDR', 2)` before using them.
33
+ */
34
+ export declare function toCheckoutAmount(money: Money, options?: CheckoutAmountOptions): number;
35
+ /** `{ amount, currency }` for a payment request. */
36
+ export declare function toCheckout(money: Money, options?: CheckoutAmountOptions): {
37
+ amount: number;
38
+ currency: string;
39
+ };
40
+ /** Reads a Checkout.com amount: fromCheckout(500000, 'CLP') is 5000 CLP. */
41
+ export declare function fromCheckout<C extends CurrencyInput>(amount: number | bigint, currency: C): Money<Uppercase<C>>;
42
+ //# sourceMappingURL=checkout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkout.d.ts","sourceRoot":"","sources":["../src/checkout.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAE5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,CAAC;AAW1B,kEAAkE;AAClE,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAE5D;AAED,iFAAiF;AACjF,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,kGAAkG;AAClG,MAAM,WAAW,qBAAqB;IACpC,4FAA4F;IAC5F,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAC/B;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,MAAM,CAOtF;AAED,oDAAoD;AACpD,wBAAgB,UAAU,CACxB,KAAK,EAAE,KAAK,EACZ,OAAO,CAAC,EAAE,qBAAqB,GAC9B;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAEtC;AAED,4EAA4E;AAC5E,wBAAgB,YAAY,CAAC,CAAC,SAAS,aAAa,EAClD,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,QAAQ,EAAE,CAAC,GACV,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAGrB"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Checkout.com's amount view (https://www.checkout.com/docs/payments/accept-payments/format-the-amount-value,
3
+ * encoded in data/checkout.json). `amount` is an integer:
4
+ * - the full amount: BIF DJF GNF ISK JPY KMF KRW PYG RWF UGX VUV VND XAF XOF XPF
5
+ * - divided by 1000, last digit 0: BHD IQD JOD KWD LYD OMR TND
6
+ * - divided by 100 for every other currency. CLP is here too (ISO says 0 decimals), and for payment
7
+ * requests its last two digits must be 00 (5000 CLP -> 500000). Standalone authentication is
8
+ * exempt.
9
+ */
10
+ import { CHECKOUT_RULES } from './generated/checkout.js';
11
+ import {} from './money.js';
12
+ import { fromProcessorAmount, toProcessorAmount } from './processor.js';
13
+ export { CHECKOUT_RULES };
14
+ /** Checkout.com's [exponent, payment multiple, Standalone API multiple] for a currency. */
15
+ function rulesFor(currency) {
16
+ const rules = CHECKOUT_RULES[currency.toUpperCase()];
17
+ if (!rules) {
18
+ throw new RangeError(`${currency} is not a Checkout.com currency`);
19
+ }
20
+ return rules;
21
+ }
22
+ /** Whether Checkout.com lists the currency (case-insensitive). */
23
+ export function isCheckoutCurrency(currency) {
24
+ return CHECKOUT_RULES[currency.toUpperCase()] !== undefined;
25
+ }
26
+ /** The exponent Checkout.com's API uses for a currency (CLP 2, JPY 0, BHD 3). */
27
+ export function checkoutExponent(currency) {
28
+ return rulesFor(currency)[0];
29
+ }
30
+ /**
31
+ * The integer `amount` to send, e.g. toCheckoutAmount(money('5000', 'CLP')) === 500000.
32
+ * Throws a RangeError for negative amounts (refunds are sent as positive amounts).
33
+ * SLL and XDR are on Checkout.com's list but not in ISO 4217's active list: call
34
+ * `defineCurrency('SLL', 2)` or `defineCurrency('XDR', 2)` before using them.
35
+ */
36
+ export function toCheckoutAmount(money, options) {
37
+ const [exponent, paymentMultiple, standaloneMultiple] = rulesFor(money.currency);
38
+ const multiple = options?.standalone ? standaloneMultiple : paymentMultiple;
39
+ const rounding = options?.rounding ?? 'unnecessary';
40
+ // "Be greater than zero", except 0 for a card verification (API reference); refunds are positive.
41
+ return toProcessorAmount('Checkout.com', money, exponent, multiple, rounding, 'nonNegative');
42
+ }
43
+ /** `{ amount, currency }` for a payment request. */
44
+ export function toCheckout(money, options) {
45
+ return { amount: toCheckoutAmount(money, options), currency: money.currency.toUpperCase() };
46
+ }
47
+ /** Reads a Checkout.com amount: fromCheckout(500000, 'CLP') is 5000 CLP. */
48
+ export function fromCheckout(amount, currency) {
49
+ const code = currency.toUpperCase();
50
+ return fromProcessorAmount('Checkout.com', amount, code, checkoutExponent(code));
51
+ }
52
+ //# sourceMappingURL=checkout.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkout.js","sourceRoot":"","sources":["../src/checkout.ts"],"names":[],"mappings":"AAAA;;;;;;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D,MAAM,UAAU,UAAU,CACxB,KAAY,EACZ,OAA+B;IAE/B,OAAO,EAAE,MAAM,EAAE,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,CAAC;AAC9F,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,YAAY,CAC1B,MAAuB,EACvB,QAAW;IAEX,MAAM,IAAI,GAAG,QAAQ,CAAC,WAAW,EAAkB,CAAC;IACpD,OAAO,mBAAmB,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;AACnF,CAAC"}
@@ -0,0 +1,51 @@
1
+ import { type Rounded } from './exact.js';
2
+ import { type Factor } from './decimal.js';
3
+ import { type CurrencyInput, type Money } from './money.js';
4
+ import { type RateProvider } from './provider.js';
5
+ import { type ConvertOptions, type Rate } from './rate.js';
6
+ import { type RoundingMode } from './rounding.js';
7
+ export { exchangeRate, impliedRate, isRate, multiplyByRatioAtScale, Rate, RateExpiredError, rateFromJSON, type ConvertOptions, type RateInfo, type RateJSON, type RateType, } from './rate.js';
8
+ export { bag, comparator, MoneyBag, sortByValue, sumByCurrency, type ComparatorOptions, type TotalOptions, } from './bag.js';
9
+ export { chain, identity, rateOf, rateTable, RateUnavailableError, reversed, snapshots, snapshotsFrom, via, type RateProvider, type RateQuery, type RateTable, type RateTableSpec, type SnapshotOptions, type Snapshots, } from './provider.js';
10
+ /** Options for converting through a provider. */
11
+ export interface ProviderConvertOptions extends ConvertOptions {
12
+ /** The time the rate is wanted for (an ISO-8601 date or instant, or a Date). */
13
+ readonly asOf?: string | Date;
14
+ }
15
+ /**
16
+ * Converts `money` at a Rate, rounding once to the target currency's exponent (or `options.scale`).
17
+ * The rate's pair is checked, at compile time for literal currencies and at run time otherwise.
18
+ *
19
+ * convert(money('100', 'EUR'), eurUsd, 'halfEven') // 111.65 USD
20
+ * convert(money('100', 'USD'), eurUsd.inverse().andThen(eurJpy)) // composed exactly, rounded once
21
+ */
22
+ export declare function convert<From extends string, To extends string>(money: Money<From>, rate: Rate<NoInfer<From>, To>, rounding?: RoundingMode, options?: ConvertOptions): Money<To>;
23
+ /**
24
+ * Converts at a bare rate: units of `to` per unit of money's currency, as a decimal string so that
25
+ * no float is involved ('1.0832', every digit kept). Rounds once.
26
+ *
27
+ * convert(money('100', 'EUR'), 'JPY', '162.457', 'halfEven') // 16246 JPY
28
+ */
29
+ export declare function convert<To extends CurrencyInput>(money: Money, to: To, rate: Factor, rounding?: RoundingMode, options?: ConvertOptions): Money<To>;
30
+ /**
31
+ * Converts through a provider: looks up money's currency → `to` (as of `options.asOf`) and rounds
32
+ * once. Throws RateUnavailableError when the provider has no rate.
33
+ *
34
+ * convert(money('100', 'USD'), 'JPY', via('EUR', ecb), 'halfEven')
35
+ */
36
+ export declare function convert<To extends CurrencyInput>(money: Money, to: To, provider: RateProvider, rounding?: RoundingMode, options?: ProviderConvertOptions): Money<To>;
37
+ /**
38
+ * A conversion's rounded value, what rounding left out, and the rate used (for the audit trail).
39
+ */
40
+ export interface DetailedConversion<From extends string, To extends string> extends Rounded<To> {
41
+ readonly rate: Rate<From, To>;
42
+ }
43
+ /**
44
+ * Converts at `rate` like convert(), and also reports the residual: `value + residual` is exactly
45
+ * money × rate, so a ledger can book the rounding difference.
46
+ *
47
+ * const { value, residual } = convertDetailed(money('0.07', 'EUR'), eurUsd, 'halfEven');
48
+ * // value 0.08 USD, residual -0.001845 USD (as an exact ExactMoney)
49
+ */
50
+ export declare function convertDetailed<From extends string, To extends string>(money: Money<From>, rate: Rate<NoInfer<From>, To>, rounding?: RoundingMode, options?: ConvertOptions): DetailedConversion<From, To>;
51
+ //# sourceMappingURL=convert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAEA,OAAO,EAAS,KAAK,OAAO,EAAE,MAAM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,EAC7B,QAAQ,CAAC,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE,cAAc,GACvB,KAAK,CAAC,EAAE,CAAC,CAAC;AACb;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,EAAE,SAAS,aAAa,EAC9C,KAAK,EAAE,KAAK,EACZ,EAAE,EAAE,EAAE,EACN,IAAI,EAAE,MAAM,EACZ,QAAQ,CAAC,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE,cAAc,GACvB,KAAK,CAAC,EAAE,CAAC,CAAC;AACb;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,EAAE,SAAS,aAAa,EAC9C,KAAK,EAAE,KAAK,EACZ,EAAE,EAAE,EAAE,EACN,QAAQ,EAAE,YAAY,EACtB,QAAQ,CAAC,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,KAAK,CAAC,EAAE,CAAC,CAAC;AAuCb;;GAEG;AACH,MAAM,WAAW,kBAAkB,CAAC,IAAI,SAAS,MAAM,EAAE,EAAE,SAAS,MAAM,CAAE,SAAQ,OAAO,CAAC,EAAE,CAAC;IAC7F,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;CAC/B;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,SAAS,MAAM,EAAE,EAAE,SAAS,MAAM,EACpE,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAC7B,QAAQ,GAAE,YAA+B,EACzC,OAAO,CAAC,EAAE,cAAc,GACvB,kBAAkB,CAAC,IAAI,EAAE,EAAE,CAAC,CAG9B"}
@@ -0,0 +1,55 @@
1
+ // `ducat/convert` free functions: exact exchange rates and conversion that rounds once.
2
+ import { currencyExponent } from './currency.js';
3
+ import { exact } from './exact.js';
4
+ import { ratio } from './decimal.js';
5
+ import { make } from './money.js';
6
+ import { rateOf } from './provider.js';
7
+ import { isRate, multiplyByRatioAtScale } from './rate.js';
8
+ import { DEFAULT_ROUNDING } from './rounding.js';
9
+ export { exchangeRate, impliedRate, isRate, multiplyByRatioAtScale, Rate, RateExpiredError, rateFromJSON, } from './rate.js';
10
+ export { bag, comparator, MoneyBag, sortByValue, sumByCurrency, } from './bag.js';
11
+ export { chain, identity, rateOf, rateTable, RateUnavailableError, reversed, snapshots, snapshotsFrom, via, } from './provider.js';
12
+ function isProvider(value) {
13
+ return typeof value?.rate === 'function';
14
+ }
15
+ export function convert(money, rateOrTarget, roundingOrRate, optionsOrRounding, maybeOptions) {
16
+ if (isRate(rateOrTarget)) {
17
+ const rounding = roundingOrRate;
18
+ return rateOrTarget.convert(money, rounding, optionsOrRounding);
19
+ }
20
+ const to = rateOrTarget;
21
+ if (isProvider(roundingOrRate)) {
22
+ let query;
23
+ if (maybeOptions?.asOf !== undefined) {
24
+ query = { asOf: maybeOptions.asOf };
25
+ }
26
+ const rate = rateOf(roundingOrRate, money.currency, to, query);
27
+ return rate.convert(money, optionsOrRounding, maybeOptions);
28
+ }
29
+ // The bare-rate form converts directly, without building a Rate, to keep it allocation-light.
30
+ let rate = ratio(roundingOrRate);
31
+ if (typeof roundingOrRate === 'object') {
32
+ // A hand-made Ratio object is passed through by ratio(); rebuild it so it is reduced and its
33
+ // number fields match its bigints. Strings and numbers are already normalised (and cached).
34
+ rate = ratio(rate.num, rate.den);
35
+ }
36
+ if (rate.num <= 0n) {
37
+ throw new RangeError('Exchange rate must be positive');
38
+ }
39
+ const rounding = optionsOrRounding ?? DEFAULT_ROUNDING;
40
+ const scale = maybeOptions?.scale ?? currencyExponent(to);
41
+ const minor = multiplyByRatioAtScale(money.minor, rate, scale - money.scale, rounding);
42
+ return make(to, minor, scale);
43
+ }
44
+ /**
45
+ * Converts at `rate` like convert(), and also reports the residual: `value + residual` is exactly
46
+ * money × rate, so a ledger can book the rounding difference.
47
+ *
48
+ * const { value, residual } = convertDetailed(money('0.07', 'EUR'), eurUsd, 'halfEven');
49
+ * // value 0.08 USD, residual -0.001845 USD (as an exact ExactMoney)
50
+ */
51
+ export function convertDetailed(money, rate, rounding = DEFAULT_ROUNDING, options) {
52
+ const rounded = exact(money).convert(rate).roundWithResidual(rounding, options?.scale);
53
+ return { value: rounded.value, residual: rounded.residual, rate };
54
+ }
55
+ //# sourceMappingURL=convert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.js","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,KAAK,EAAgB,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,KAAK,EAAe,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,IAAI,EAAkC,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,MAAM,EAAqB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,sBAAsB,EAAkC,MAAM,WAAW,CAAC;AAC3F,OAAO,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAEpE,OAAO,EACL,YAAY,EACZ,WAAW,EACX,MAAM,EACN,sBAAsB,EACtB,IAAI,EACJ,gBAAgB,EAChB,YAAY,GAKb,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,GAAG,EACH,UAAU,EACV,QAAQ,EACR,WAAW,EACX,aAAa,GAGd,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,KAAK,EACL,QAAQ,EACR,MAAM,EACN,SAAS,EACT,oBAAoB,EACpB,QAAQ,EACR,SAAS,EACT,aAAa,EACb,GAAG,GAOJ,MAAM,eAAe,CAAC;AAQvB,SAAS,UAAU,CAAC,KAAc;IAChC,OAAO,OAAQ,KAAkC,EAAE,IAAI,KAAK,UAAU,CAAC;AACzE,CAAC;AAyCD,MAAM,UAAU,OAAO,CACrB,KAAY,EACZ,YAA2B,EAC3B,cAAqD,EACrD,i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,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACvF,OAAO,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAChC,CAAC;AASD;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAkB,EAClB,IAA6B,EAC7B,WAAyB,gBAAgB,EACzC,OAAwB;IAExB,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACvF,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;AACpE,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { type IsoCurrencyCode } from './generated/iso4217.js';
2
+ /**
3
+ * Augment this interface to add non-ISO currencies to the `CurrencyCode` type:
4
+ *
5
+ * declare module '@ducatjs/money' { interface CustomCurrencies { BTC: true; USDC: true } }
6
+ * defineCurrency('BTC', 8);
7
+ */
8
+ export interface CustomCurrencies {
9
+ }
10
+ /** A known currency code: ISO 4217, or one added to `CustomCurrencies`. */
11
+ export type CurrencyCode = IsoCurrencyCode | (keyof CustomCurrencies & string);
12
+ /**
13
+ * Registers a currency that is not in ISO 4217 (crypto, loyalty points, withdrawn codes you still
14
+ * hold balances in). ISO codes cannot be redefined: use an explicit `scale` for extra precision.
15
+ */
16
+ export declare function defineCurrency(code: string, exponent: number): void;
17
+ /** The minor-unit exponent of a currency (EUR 2, JPY 0, KWD 3). Throws for unknown codes. */
18
+ export declare function currencyExponent(code: string): number;
19
+ /** Whether the code is an ISO 4217 currency or one registered with `defineCurrency`. */
20
+ export declare function isCurrency(code: string): code is CurrencyCode;
21
+ export { ISO_EXPONENTS, ISO_4217_PUBLISHED, ISO_NON_MONETARY, type IsoCurrencyCode, } from './generated/iso4217.js';
22
+ //# sourceMappingURL=currency.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../src/currency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE7E;;;;;GAKG;AAEH,MAAM,WAAW,gBAAgB;CAAG;AAEpC,2EAA2E;AAC3E,MAAM,MAAM,YAAY,GAAG,eAAe,GAAG,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,CAAC;AAK/E;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAYnE;AAED,6FAA6F;AAC7F,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYrD;AAED,wFAAwF;AACxF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,YAAY,CAE7D;AAED,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,gBAAgB,EAChB,KAAK,eAAe,GACrB,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,38 @@
1
+ import { ISO_EXPONENTS } from './generated/iso4217.js';
2
+ const custom = new Map();
3
+ const iso = ISO_EXPONENTS;
4
+ /**
5
+ * Registers a currency that is not in ISO 4217 (crypto, loyalty points, withdrawn codes you still
6
+ * hold balances in). ISO codes cannot be redefined: use an explicit `scale` for extra precision.
7
+ */
8
+ export function defineCurrency(code, exponent) {
9
+ if (!Number.isInteger(exponent) || exponent < 0 || exponent > 30) {
10
+ throw new RangeError(`Invalid exponent ${exponent}`);
11
+ }
12
+ if (typeof iso[code] === 'number') {
13
+ throw new TypeError(`${code} is an ISO 4217 currency and cannot be redefined`);
14
+ }
15
+ const prev = custom.get(code);
16
+ if (prev !== undefined && prev !== exponent) {
17
+ throw new TypeError(`${code} is already defined with exponent ${prev}`);
18
+ }
19
+ custom.set(code, exponent);
20
+ }
21
+ /** The minor-unit exponent of a currency (EUR 2, JPY 0, KWD 3). Throws for unknown codes. */
22
+ export function currencyExponent(code) {
23
+ const isoExponent = iso[code];
24
+ if (typeof isoExponent === 'number') {
25
+ return isoExponent;
26
+ }
27
+ const customExponent = custom.get(code);
28
+ if (customExponent !== undefined) {
29
+ return customExponent;
30
+ }
31
+ throw new RangeError(`Unknown currency "${code}" (not ISO 4217 and not registered with defineCurrency)`);
32
+ }
33
+ /** Whether the code is an ISO 4217 currency or one registered with `defineCurrency`. */
34
+ export function isCurrency(code) {
35
+ return typeof iso[code] === 'number' || custom.has(code);
36
+ }
37
+ export { ISO_EXPONENTS, ISO_4217_PUBLISHED, ISO_NON_MONETARY, } from './generated/iso4217.js';
38
+ //# sourceMappingURL=currency.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"currency.js","sourceRoot":"","sources":["../src/currency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAwB,MAAM,wBAAwB,CAAC;AAc7E,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;AACzC,MAAM,GAAG,GAAG,aAAiD,CAAC;AAE9D;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE,QAAgB;IAC3D,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,QAAQ,GAAG,CAAC,IAAI,QAAQ,GAAG,EAAE,EAAE,CAAC;QACjE,MAAM,IAAI,UAAU,CAAC,oBAAoB,QAAQ,EAAE,CAAC,CAAC;IACvD,CAAC;IACD,IAAI,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,QAAQ,EAAE,CAAC;QAClC,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,kDAAkD,CAAC,CAAC;IACjF,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC5C,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,qCAAqC,IAAI,EAAE,CAAC,CAAC;IAC1E,CAAC;IACD,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC7B,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,MAAM,WAAW,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE,CAAC;QACpC,OAAO,WAAW,CAAC;IACrB,CAAC;IACD,MAAM,cAAc,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;QACjC,OAAO,cAAc,CAAC;IACxB,CAAC;IACD,MAAM,IAAI,UAAU,CAClB,qBAAqB,IAAI,yDAAyD,CACnF,CAAC;AACJ,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC3D,CAAC;AAED,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,gBAAgB,GAEjB,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * An exact rational factor num/den (den > 0). `n`/`d` hold the same values as numbers when both are
3
+ * safe.
4
+ */
5
+ export interface Ratio {
6
+ readonly num: bigint;
7
+ readonly den: bigint;
8
+ /** num as a number, or NaN when it is not a safe integer */
9
+ readonly n: number;
10
+ /** den as a number, or NaN when it is not a safe integer */
11
+ readonly d: number;
12
+ }
13
+ /** Anything accepted where a multiplier, rate or percentage is expected. */
14
+ export type Factor = number | bigint | string | Ratio;
15
+ /** 10^exponent as a bigint, cached. */
16
+ export declare function pow10n(exponent: number): bigint;
17
+ /** 10^e as a number for 0 <= e <= 15 (all exact and safe). */
18
+ export declare const POW10: readonly [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000];
19
+ /**
20
+ * A bigint as a number when it is a safe integer, else unchanged: the representation Money keeps.
21
+ */
22
+ export declare const normalizeMinor: (value: bigint) => number | bigint;
23
+ /**
24
+ * Parses a plain decimal string ("-12.345", "+7", "0.50") into digits/10^scale. Returns the integer
25
+ * of all digits (number when safe, else bigint) and the count of fraction digits. Hand-rolled (no
26
+ * regex, no BigInt for short inputs): this is the hot path of `money("12.34", ...)`.
27
+ */
28
+ export declare function parsePlainDecimal(input: string): {
29
+ digits: number | bigint;
30
+ scale: number;
31
+ } | null;
32
+ /**
33
+ * The largest exponent and number of fraction digits a decimal string may have. Far beyond any real
34
+ * amount or rate, and it keeps input such as "1e999999999" from building enormous powers of ten.
35
+ */
36
+ export declare const MAX_DECIMAL_SCALE = 1000;
37
+ /** Parses a decimal string, allowing exponent notation ("1e-7", as produced by String(1e-7)). */
38
+ export declare function parseDecimal(input: string): {
39
+ digits: bigint;
40
+ scale: number;
41
+ };
42
+ /**
43
+ * Builds an exact ratio. `ratio('1.075')`, `ratio(3, 4)`, `ratio(10n)`, `ratio('1/3')`.
44
+ * Numbers are taken at their shortest decimal representation: `ratio(0.1)` is exactly 1/10.
45
+ * Pre-build ratios you use in hot loops; strings are also cached (up to 1024 entries).
46
+ */
47
+ export declare function ratio(value: Factor, den?: Factor): Ratio;
48
+ /** num/den -> exact decimal string if the expansion terminates, else null. */
49
+ export declare function ratioToDecimal(value: Ratio): string | null;
50
+ /** Formats an integer at `scale` as a plain decimal string ("-0.05"). */
51
+ export declare function formatScaled(minor: number | bigint, scale: number): string;
52
+ //# sourceMappingURL=decimal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decimal.d.ts","sourceRoot":"","sources":["../src/decimal.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,GAAG,EAAE,MAAM,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