UNPKG

@eristack/dimension

Version:

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

96 lines (93 loc) • 3.55 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { DimensionParseError: () => DimensionParseError, dimensionVolume: () => dimensionVolume, formatDimension: () => formatDimension, normalizeDimension: () => normalizeDimension }); module.exports = __toCommonJS(index_exports); // src/core/errors.ts var DimensionParseError = class extends Error { code = "DIMENSION_PARSE_ERROR"; constructor(message) { super(message); this.name = "DimensionParseError"; } }; // src/core/dimension.ts var import_decimal = __toESM(require("decimal.js"), 1); 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 import_decimal.default(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 import_decimal.default(d.length).mul(d.width).mul(d.height).toDecimalPlaces(scale, import_decimal.default.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; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DimensionParseError, dimensionVolume, formatDimension, normalizeDimension }); //# sourceMappingURL=index.cjs.map