@eristack/rounding-policy
Version:
Named rounding profiles that resolve to @eristack/money Rounding operators
85 lines (82 loc) • 2.76 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
RoundingPolicyNotFoundError: () => RoundingPolicyNotFoundError,
createRoundingPolicyRegistry: () => createRoundingPolicyRegistry
});
module.exports = __toCommonJS(index_exports);
// 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
var import_money = require("@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 import_money.Rounding.of(
override.scale,
override.mode ?? def.mode ?? "HALF_EVEN"
);
}
if (def.scale !== void 0) {
return import_money.Rounding.of(def.scale, def.mode ?? "HALF_EVEN");
}
return import_money.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 };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RoundingPolicyNotFoundError,
createRoundingPolicyRegistry
});
//# sourceMappingURL=index.cjs.map