@trail-ui/shared-utils
Version:
A set of TrailUI utilities
63 lines (61 loc) • 1.99 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/numbers.ts
var numbers_exports = {};
__export(numbers_exports, {
clampValue: () => clampValue,
countDecimalPlaces: () => countDecimalPlaces,
toPrecision: () => toPrecision
});
module.exports = __toCommonJS(numbers_exports);
function toNumber(value) {
const num = parseFloat(value);
return typeof num !== "number" || Number.isNaN(num) ? 0 : num;
}
function toPrecision(value, precision) {
let nextValue = toNumber(value);
const scaleFactor = 10 ** (precision != null ? precision : 10);
nextValue = Math.round(nextValue * scaleFactor) / scaleFactor;
return precision ? nextValue.toFixed(precision) : nextValue.toString();
}
function countDecimalPlaces(value) {
if (!Number.isFinite(value))
return 0;
let e = 1;
let p = 0;
while (Math.round(value * e) / e !== value) {
e *= 10;
p += 1;
}
return p;
}
function clampValue(value, min, max) {
if (value == null)
return value;
if (max < min) {
console.warn("clamp: max cannot be less than min");
}
return Math.min(Math.max(value, min), max);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
clampValue,
countDecimalPlaces,
toPrecision
});