@vielzeug/coins 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +12 -67
  2. package/dist/_decimal.cjs +1 -1
  3. package/dist/_decimal.cjs.map +1 -1
  4. package/dist/_decimal.d.ts +1 -0
  5. package/dist/_decimal.d.ts.map +1 -1
  6. package/dist/_decimal.js +30 -19
  7. package/dist/_decimal.js.map +1 -1
  8. package/dist/aggregate.cjs +1 -1
  9. package/dist/aggregate.cjs.map +1 -1
  10. package/dist/aggregate.d.ts.map +1 -1
  11. package/dist/aggregate.js +32 -26
  12. package/dist/aggregate.js.map +1 -1
  13. package/dist/coins.cjs +1 -1
  14. package/dist/coins.cjs.map +1 -1
  15. package/dist/coins.iife.js +1 -1
  16. package/dist/coins.iife.js.map +1 -1
  17. package/dist/coins.js +1 -1
  18. package/dist/coins.js.map +1 -1
  19. package/dist/currency.cjs +1 -1
  20. package/dist/currency.cjs.map +1 -1
  21. package/dist/currency.d.ts.map +1 -1
  22. package/dist/currency.js +7 -6
  23. package/dist/currency.js.map +1 -1
  24. package/dist/errors.cjs +1 -1
  25. package/dist/errors.cjs.map +1 -1
  26. package/dist/errors.d.ts +3 -0
  27. package/dist/errors.d.ts.map +1 -1
  28. package/dist/errors.js +16 -5
  29. package/dist/errors.js.map +1 -1
  30. package/dist/exchange.cjs +1 -1
  31. package/dist/exchange.cjs.map +1 -1
  32. package/dist/exchange.js +1 -1
  33. package/dist/exchange.js.map +1 -1
  34. package/dist/format.cjs +1 -1
  35. package/dist/format.cjs.map +1 -1
  36. package/dist/format.d.ts.map +1 -1
  37. package/dist/format.js +54 -52
  38. package/dist/format.js.map +1 -1
  39. package/dist/index.cjs +1 -1
  40. package/dist/index.d.ts +2 -2
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +3 -3
  43. package/dist/money.cjs +1 -1
  44. package/dist/money.cjs.map +1 -1
  45. package/dist/money.d.ts +9 -2
  46. package/dist/money.d.ts.map +1 -1
  47. package/dist/money.js +72 -54
  48. package/dist/money.js.map +1 -1
  49. package/dist/serialization.cjs +1 -1
  50. package/dist/serialization.cjs.map +1 -1
  51. package/dist/serialization.d.ts +1 -4
  52. package/dist/serialization.d.ts.map +1 -1
  53. package/dist/serialization.js +6 -38
  54. package/dist/serialization.js.map +1 -1
  55. package/package.json +8 -7
@@ -1 +1 @@
1
- {"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../src/serialization.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAK1D,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAI9C;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,QAAQ,CAAA;CAAO,GAAG,KAAK,CAS7G"}
1
+ {"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../src/serialization.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAI9C"}
@@ -1,45 +1,13 @@
1
- import { CoinsError as e } from "./errors.js";
2
- import { currency as t } from "./currency.js";
3
- import { assertMoney as n, money as r } from "./money.js";
1
+ import { assertMoney as e } from "./money.js";
4
2
  //#region src/serialization.ts
5
- var i = /^(?:0|-[1-9]\d*|[1-9]\d*)$/, a = [
6
- "amount",
7
- "currency",
8
- "unit"
9
- ];
10
- function o(e) {
11
- return n(e), {
12
- amount: e.amount.toString(),
13
- currency: e.currency.code,
3
+ function t(t) {
4
+ return e(t), {
5
+ amount: t.amount.toString(),
6
+ currency: t.currency.code,
14
7
  unit: "minor"
15
8
  };
16
9
  }
17
- function s(n, i = {}) {
18
- try {
19
- let e = c(n), a = i.currency ?? t;
20
- return r(BigInt(e.amount), a(e.currency), { unit: "minor" });
21
- } catch (t) {
22
- throw new e("INVALID_MONEY", "Invalid Money JSON", { cause: t });
23
- }
24
- }
25
- function c(e) {
26
- if (typeof e != "object" || !e || Object.getPrototypeOf(e) !== Object.prototype) throw TypeError("Money JSON must be a plain object");
27
- let t = Object.getOwnPropertyDescriptors(e), n = Reflect.ownKeys(e);
28
- if (n.length !== a.length || !a.every((e) => n.includes(e))) throw TypeError("Money JSON must contain only amount, currency, and unit");
29
- for (let e of a) {
30
- let n = t[e];
31
- if (!n || !("value" in n) || n.get || n.set) throw TypeError(`Money JSON property "${e}" must be a data value`);
32
- }
33
- let r = t.amount?.value, o = t.currency?.value, s = t.unit?.value;
34
- if (typeof r != "string" || !i.test(r)) throw TypeError("Money JSON amount must be a canonical integer string");
35
- if (typeof o != "string" || s !== "minor") throw TypeError("Money JSON currency/unit are invalid");
36
- return {
37
- amount: r,
38
- currency: o,
39
- unit: s
40
- };
41
- }
42
10
  //#endregion
43
- export { s as parseMoneyJSON, o as toJSON };
11
+ export { t as toJSON };
44
12
 
45
13
  //# sourceMappingURL=serialization.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"serialization.js","names":[],"sources":["../src/serialization.ts"],"sourcesContent":["import { currency } from './currency';\nimport { CoinsError } from './errors';\nimport { assertMoney, money } from './money';\nimport type { Currency, Money, MoneyJSON } from './types';\n\nconst INTEGER = /^(?:0|-[1-9]\\d*|[1-9]\\d*)$/;\nconst KEYS = ['amount', 'currency', 'unit'] as const;\n\nexport function toJSON(value: Money): MoneyJSON {\n assertMoney(value);\n\n return { amount: value.amount.toString(), currency: value.currency.code, unit: 'minor' };\n}\n\nexport function parseMoneyJSON(value: unknown, options: { currency?: (code: string) => Currency } = {}): Money {\n try {\n const payload = readPayload(value);\n const resolveCurrency = options.currency ?? currency;\n\n return money(BigInt(payload.amount), resolveCurrency(payload.currency), { unit: 'minor' });\n } catch (error) {\n throw new CoinsError('INVALID_MONEY', 'Invalid Money JSON', { cause: error });\n }\n}\n\nfunction readPayload(value: unknown): MoneyJSON {\n if (typeof value !== 'object' || value === null || Object.getPrototypeOf(value) !== Object.prototype) {\n throw new TypeError('Money JSON must be a plain object');\n }\n\n const descriptors = Object.getOwnPropertyDescriptors(value);\n const ownKeys = Reflect.ownKeys(value);\n\n if (ownKeys.length !== KEYS.length || !KEYS.every((key) => ownKeys.includes(key))) {\n throw new TypeError('Money JSON must contain only amount, currency, and unit');\n }\n\n for (const key of KEYS) {\n const descriptor = descriptors[key];\n\n if (!descriptor || !('value' in descriptor) || descriptor.get || descriptor.set) {\n throw new TypeError(`Money JSON property \"${key}\" must be a data value`);\n }\n }\n\n const amount = descriptors.amount?.value;\n const currencyCode = descriptors.currency?.value;\n const unit = descriptors.unit?.value;\n\n if (typeof amount !== 'string' || !INTEGER.test(amount))\n throw new TypeError('Money JSON amount must be a canonical integer string');\n\n if (typeof currencyCode !== 'string' || unit !== 'minor') throw new TypeError('Money JSON currency/unit are invalid');\n\n return { amount, currency: currencyCode, unit };\n}\n"],"mappings":";;;;AAKA,IAAM,IAAU,8BACV,IAAO;CAAC;CAAU;CAAY;AAAM;AAE1C,SAAgB,EAAO,GAAyB;CAG9C,OAFA,EAAY,CAAK,GAEV;EAAE,QAAQ,EAAM,OAAO,SAAS;EAAG,UAAU,EAAM,SAAS;EAAM,MAAM;CAAQ;AACzF;AAEA,SAAgB,EAAe,GAAgB,IAAqD,CAAC,GAAU;CAC7G,IAAI;EACF,IAAM,IAAU,EAAY,CAAK,GAC3B,IAAkB,EAAQ,YAAY;EAE5C,OAAO,EAAM,OAAO,EAAQ,MAAM,GAAG,EAAgB,EAAQ,QAAQ,GAAG,EAAE,MAAM,QAAQ,CAAC;CAC3F,SAAS,GAAO;EACd,MAAM,IAAI,EAAW,iBAAiB,sBAAsB,EAAE,OAAO,EAAM,CAAC;CAC9E;AACF;AAEA,SAAS,EAAY,GAA2B;CAC9C,IAAI,OAAO,KAAU,aAAY,KAAkB,OAAO,eAAe,CAAK,MAAM,OAAO,WACzF,MAAU,UAAU,mCAAmC;CAGzD,IAAM,IAAc,OAAO,0BAA0B,CAAK,GACpD,IAAU,QAAQ,QAAQ,CAAK;CAErC,IAAI,EAAQ,WAAW,EAAK,UAAU,CAAC,EAAK,OAAO,MAAQ,EAAQ,SAAS,CAAG,CAAC,GAC9E,MAAU,UAAU,yDAAyD;CAG/E,KAAK,IAAM,KAAO,GAAM;EACtB,IAAM,IAAa,EAAY;EAE/B,IAAI,CAAC,KAAc,EAAE,WAAW,MAAe,EAAW,OAAO,EAAW,KAC1E,MAAU,UAAU,wBAAwB,EAAI,uBAAuB;CAE3E;CAEA,IAAM,IAAS,EAAY,QAAQ,OAC7B,IAAe,EAAY,UAAU,OACrC,IAAO,EAAY,MAAM;CAE/B,IAAI,OAAO,KAAW,YAAY,CAAC,EAAQ,KAAK,CAAM,GACpD,MAAU,UAAU,sDAAsD;CAE5E,IAAI,OAAO,KAAiB,YAAY,MAAS,SAAS,MAAU,UAAU,sCAAsC;CAEpH,OAAO;EAAE;EAAQ,UAAU;EAAc;CAAK;AAChD"}
1
+ {"version":3,"file":"serialization.js","names":[],"sources":["../src/serialization.ts"],"sourcesContent":["import { assertMoney } from './money';\nimport type { Money, MoneyJSON } from './types';\n\nexport function toJSON(value: Money): MoneyJSON {\n assertMoney(value);\n\n return { amount: value.amount.toString(), currency: value.currency.code, unit: 'minor' };\n}\n"],"mappings":";;AAGA,SAAgB,EAAO,GAAyB;CAG9C,OAFA,EAAY,CAAK,GAEV;EAAE,QAAQ,EAAM,OAAO,SAAS;EAAG,UAAU,EAAM,SAAS;EAAM,MAAM;CAAQ;AACzF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielzeug/coins",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "description": "Bigint-based monetary arithmetic with currency formatting, exchange rates, and rounding policies",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,18 +30,19 @@
30
30
  "registry": "https://registry.npmjs.org/"
31
31
  },
32
32
  "scripts": {
33
- "build": "vite build && pnpm run build:bundle && pnpm run build:types",
33
+ "build": "vite build && pnpm --silent run build:bundle && pnpm --silent run build:types && pnpm --silent run verify:exports",
34
34
  "build:bundle": "vite build --config vite.bundle.config.ts",
35
35
  "build:types": "tsc -p tsconfig.declarations.json",
36
36
  "fix": "biome check --write src",
37
37
  "lint": "biome ci src",
38
- "prepublishOnly": "pnpm run build",
39
- "test": "vitest"
38
+ "prepublishOnly": "pnpm --silent run build",
39
+ "test": "vitest",
40
+ "verify:exports": "node verify-exports.mjs"
40
41
  },
41
42
  "devDependencies": {
42
- "@types/node": "^26.2.0",
43
+ "@types/node": "^26.4.1",
43
44
  "typescript": "^6.0.3",
44
- "vite": "^8.2.1",
45
- "vitest": "^4.1.10"
45
+ "vite": "^8.2.2",
46
+ "vitest": "^5.0.0"
46
47
  }
47
48
  }