@eristack/rounding-policy
Version:
Named rounding profiles that resolve to @eristack/money Rounding operators
57 lines (56 loc) • 1.59 kB
JavaScript
// src/core/errors.ts
var RoundingPolicyNotFoundError = class extends Error {
constructor(policyId) {
super(`Rounding policy not found: ${policyId}`);
this.policyId = policyId;
this.name = "RoundingPolicyNotFoundError";
}
policyId;
};
// src/core/registry.ts
import { Rounding } from "@eristack/money";
function resolveOperator(def, currency) {
const code = currency?.trim().toUpperCase();
const override = code ? def.currencyOverrides?.[code] : void 0;
if (override?.scale !== void 0) {
return Rounding.of(
override.scale,
override.mode ?? def.mode ?? "HALF_EVEN"
);
}
if (def.scale !== void 0) {
return Rounding.of(def.scale, def.mode ?? "HALF_EVEN");
}
return Rounding.currencyDefault(
code ?? currency,
def.mode ?? "HALF_EVEN"
);
}
function createRoundingPolicyRegistry(initial = []) {
const policies = /* @__PURE__ */ new Map();
function registerPolicy(def) {
if (!def.id.trim()) {
throw new Error("Rounding policy id cannot be empty");
}
policies.set(def.id, { ...def, id: def.id.trim() });
}
for (const def of initial) {
registerPolicy(def);
}
function roundingFor(args) {
const def = policies.get(args.policyId);
if (!def) {
throw new RoundingPolicyNotFoundError(args.policyId);
}
return resolveOperator(def, args.currency);
}
function listPolicies() {
return [...policies.values()];
}
return { registerPolicy, roundingFor, listPolicies };
}
export {
RoundingPolicyNotFoundError,
createRoundingPolicyRegistry
};
//# sourceMappingURL=index.js.map