@julo-ui/number-utils
Version:
Number Utilities across Julo UI package
103 lines (95 loc) • 3.19 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/index.ts
var src_exports = {};
__export(src_exports, {
clampValue: () => clamp_value_default,
countDecimalPlaces: () => count_decimal_places_default,
removeNonNumber: () => remove_non_number_default,
thousandSeparator: () => thousand_separator_default,
toNumber: () => to_number_default,
toPreciseDecimal: () => to_precise_decimal_default
});
module.exports = __toCommonJS(src_exports);
// src/clamp-value.ts
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);
}
var clamp_value_default = clampValue;
// src/count-decimal-places.ts
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;
}
var count_decimal_places_default = countDecimalPlaces;
// src/remove-non-number.ts
var removeNonNumber = (str) => str.replace(/[^0-9]/g, "");
var remove_non_number_default = removeNonNumber;
// src/thousand-separator.ts
function thousandSeparator(value, options = {}) {
const { separator = ".", isReturnEmptyStringOnEmptyValue } = options;
if (!value) {
return isReturnEmptyStringOnEmptyValue ? "" : "0";
}
const num = value.toString().replace(/\D/g, "");
if (num === "") {
return isReturnEmptyStringOnEmptyValue ? "" : "0";
}
if (num.length <= 3) {
return num;
}
const result = num.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, `$1${separator}`);
return result;
}
var thousand_separator_default = thousandSeparator;
// src/to-number.ts
function toNumber(value) {
const num = parseFloat(value);
return typeof num !== "number" || Number.isNaN(num) ? 0 : num;
}
var to_number_default = toNumber;
// src/to-precise-decimal.ts
function toPreciseDecimal(value, precision) {
let nextValue = value;
const scaleFactor = 10 ** (precision != null ? precision : 10);
nextValue = Math.round(nextValue * scaleFactor) / scaleFactor;
return precision ? nextValue.toFixed(precision) : nextValue.toString();
}
var to_precise_decimal_default = toPreciseDecimal;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
clampValue,
countDecimalPlaces,
removeNonNumber,
thousandSeparator,
toNumber,
toPreciseDecimal
});