80 lines
2.5 KiB
JavaScript
80 lines
2.5 KiB
JavaScript
"use strict";
|
|
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 src_exports = {};
|
|
__export(src_exports, {
|
|
clampValue: () => clampValue,
|
|
countDecimalPlaces: () => countDecimalPlaces,
|
|
percentToValue: () => percentToValue,
|
|
roundValueToStep: () => roundValueToStep,
|
|
toPrecision: () => toPrecision,
|
|
valueToPercent: () => valueToPercent
|
|
});
|
|
module.exports = __toCommonJS(src_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 valueToPercent(value, min, max) {
|
|
return (value - min) * 100 / (max - min);
|
|
}
|
|
function percentToValue(percent, min, max) {
|
|
return (max - min) * percent + min;
|
|
}
|
|
function roundValueToStep(value, from, step) {
|
|
const nextValue = Math.round((value - from) / step) * step + from;
|
|
const precision = countDecimalPlaces(step);
|
|
return toPrecision(nextValue, precision);
|
|
}
|
|
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,
|
|
percentToValue,
|
|
roundValueToStep,
|
|
toPrecision,
|
|
valueToPercent
|
|
});
|