UNPKG

@fractorysolutions/safe-units

Version:
56 lines 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultFormatUnit = void 0; var exponentValueArithmetic_1 = require("../exponent/exponentValueArithmetic"); function defaultFormatUnit(unit) { var dimensions = Object.keys(unit) .map(function (dimension) { return unit[dimension]; }) .filter(isDimensionPresent) .sort(orderDimensions); if (dimensions.length === 0) { return ""; } var positive = dimensions.filter(function (_a) { var _ = _a[0], dim = _a[1]; return (0, exponentValueArithmetic_1.getExponentValue)(dim) > 0; }); var negative = dimensions.filter(function (_a) { var _ = _a[0], dim = _a[1]; return (0, exponentValueArithmetic_1.getExponentValue)(dim) < 0; }); if (positive.length === 0) { return formatDimensions(negative); } var numerator = formatDimensions(positive); if (negative.length === 0) { return numerator; } var denominator = formatDimensions(negative.map(negateDimension)); return "".concat(numerator, " / ").concat(maybeParenthesize(denominator, negative.length !== 1)); } exports.defaultFormatUnit = defaultFormatUnit; function isDimensionPresent(dimension) { return dimension !== undefined && dimension[1] !== "0"; } function orderDimensions(_a, _b) { var leftSymbol = _a[0]; var rightSymbol = _b[0]; return leftSymbol < rightSymbol ? -1 : 1; } function formatDimensions(dimensions) { return dimensions .map(function (_a) { var symbol = _a[0], exponent = _a[1]; var exponentStr = exponent !== "1" ? "^".concat(exponent) : ""; return "".concat(symbol).concat(exponentStr); }) .join(" * "); } function negateDimension(_a) { var symbol = _a[0], exponent = _a[1]; return [symbol, (0, exponentValueArithmetic_1.negateExponent)(exponent)]; } function maybeParenthesize(text, parenthesize) { return parenthesize ? "(".concat(text, ")") : text; } //# sourceMappingURL=format.js.map