UNPKG

@eristack/rounding-policy

Version:

Named rounding profiles that resolve to @eristack/money Rounding operators

1 lines • 3.44 kB
{"version":3,"sources":["../src/index.ts","../src/core/errors.ts","../src/core/registry.ts"],"sourcesContent":["export { RoundingPolicyNotFoundError } from \"./core/errors.js\";\nexport { createRoundingPolicyRegistry } from \"./core/registry.js\";\nexport type {\n CurrencyRoundingOverride,\n RoundingPolicyDefinition,\n} from \"./core/types.js\";\n","export class RoundingPolicyNotFoundError extends Error {\n constructor(public readonly policyId: string) {\n super(`Rounding policy not found: ${policyId}`);\n this.name = \"RoundingPolicyNotFoundError\";\n }\n}\n","import { Rounding, type MonetaryOperator, type RoundingMode } from \"@eristack/money\";\nimport { RoundingPolicyNotFoundError } from \"./errors.js\";\nimport type { RoundingPolicyDefinition } from \"./types.js\";\n\nfunction resolveOperator(\n def: RoundingPolicyDefinition,\n currency?: string,\n): MonetaryOperator {\n const code = currency?.trim().toUpperCase();\n const override = code ? def.currencyOverrides?.[code] : undefined;\n\n if (override?.scale !== undefined) {\n return Rounding.of(\n override.scale,\n override.mode ?? def.mode ?? \"HALF_EVEN\",\n );\n }\n\n if (def.scale !== undefined) {\n return Rounding.of(def.scale, def.mode ?? \"HALF_EVEN\");\n }\n\n return Rounding.currencyDefault(\n code ?? currency,\n (def.mode ?? \"HALF_EVEN\") as RoundingMode,\n );\n}\n\nexport function createRoundingPolicyRegistry(\n initial: RoundingPolicyDefinition[] = [],\n) {\n const policies = new Map<string, RoundingPolicyDefinition>();\n\n function registerPolicy(def: RoundingPolicyDefinition): void {\n if (!def.id.trim()) {\n throw new Error(\"Rounding policy id cannot be empty\");\n }\n policies.set(def.id, { ...def, id: def.id.trim() });\n }\n\n for (const def of initial) {\n registerPolicy(def);\n }\n\n function roundingFor(args: {\n policyId: string;\n currency?: string;\n }): MonetaryOperator {\n const def = policies.get(args.policyId);\n if (!def) {\n throw new RoundingPolicyNotFoundError(args.policyId);\n }\n return resolveOperator(def, args.currency);\n }\n\n function listPolicies(): Ro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