UNPKG

@eristack/dimension

Version:

L×W×H dimension triple as decimal strings — cubic volume, optional unit label

57 lines (55 loc) • 1.72 kB
// src/core/errors.ts var DimensionParseError = class extends Error { code = "DIMENSION_PARSE_ERROR"; constructor(message) { super(message); this.name = "DimensionParseError"; } }; // src/core/dimension.ts import Decimal from "decimal.js"; function trimOptional(value) { const t = value?.trim(); return t ? t : void 0; } function parsePositiveDecimal(value, field) { const trimmed = value.trim(); if (!trimmed) { throw new DimensionParseError(`${field} is required`); } let dec; try { dec = new Decimal(trimmed); } catch { throw new DimensionParseError(`${field} must be a decimal string`); } if (!dec.isFinite() || dec.lte(0)) { throw new DimensionParseError(`${field} must be a positive finite number`); } return dec; } function normalizeDimension(input) { const length = parsePositiveDecimal(input.length, "length").toFixed(); const width = parsePositiveDecimal(input.width, "width").toFixed(); const height = parsePositiveDecimal(input.height, "height").toFixed(); const unit = trimOptional(input.unit); return { length, width, height, unit }; } function dimensionVolume(dimension, options = {}) { const d = normalizeDimension(dimension); const scale = options.scale ?? 6; const volume = new Decimal(d.length).mul(d.width).mul(d.height).toDecimalPlaces(scale, Decimal.ROUND_HALF_UP); return volume.toFixed(scale); } function formatDimension(dimension) { const d = normalizeDimension(dimension); const base = `${d.length} \xD7 ${d.width} \xD7 ${d.height}`; return d.unit ? `${base} ${d.unit}` : base; } export { DimensionParseError, normalizeDimension, dimensionVolume, formatDimension }; //# sourceMappingURL=chunk-OPXJHSVG.js.map