UNPKG

@wordpress/components

Version:
105 lines (104 loc) • 3.24 kB
// packages/components/src/utils/math.ts /** * Parses and retrieves a number value. * * @param value The incoming value. * * @return The parsed number value. */ function getNumber(value) { const number = Number(value); return isNaN(number) ? 0 : number; } /** * Safely adds values together, handling floating-point precision issues. * * @param args Values to add together. * * @return The sum of values. */ function add(...args) { const precision = args.length ? Math.max(...args.map((arg) => getPrecision(getNumber(arg)))) : 0; const sum = args.reduce((total, arg) => total + getNumber(arg), 0); return precision ? getNumber(sum.toFixed(Math.min(precision, 20))) : sum; } /** * Safely subtracts values, handling floating-point precision issues. * * @param args Values to subtract together. * * @return The difference of the values. */ function subtract(...args) { const precision = args.length ? Math.max(...args.map((arg) => getPrecision(getNumber(arg)))) : 0; const diff = args.reduce((total, arg, index) => { const value = getNumber(arg); return index === 0 ? value : total - value; }, 0); return precision ? getNumber(diff.toFixed(Math.min(precision, 20))) : diff; } /** * Safely multiplies values together, handling floating-point precision issues. * * @param args Values to multiply together. * * @return The product of values. */ function multiply(...args) { if (!args.length) return 0; const precision = args.reduce((total, arg) => total + getPrecision(getNumber(arg)), 0); const product = args.reduce((total, arg, index) => { const value = getNumber(arg); return index === 0 ? value : total * value; }, 1); return precision ? getNumber(product.toFixed(Math.min(precision, 20))) : product; } /** * Determines the decimal position of a number value. * * @param value The number to evaluate. * * @return The number of decimal places. */ function getPrecision(value) { const [base, exponent] = (value + "").split(/[eE]/); if (exponent !== void 0) { const precision = (base.includes(".") ? base.split(".")[1].length : 0) - parseInt(exponent, 10); return precision > 0 ? precision : 0; } const split = base.split("."); return split[1] !== void 0 ? split[1].length : 0; } /** * Clamps a value based on a min/max range. * * @param value The value. * @param min The minimum range. * @param max The maximum range. * * @return The clamped value. */ function clamp(value, min, max) { const baseValue = getNumber(value); return Math.max(min, Math.min(baseValue, max)); } /** * Rounds a value to the nearest step offset by a minimum. * * @param value The value. * @param min The minimum range. * @param step The increment for the value. * * @return The value as a valid step. */ function ensureValidStep(value, min, step) { const baseValue = getNumber(value); const minValue = getNumber(min); const stepValue = getNumber(step); const precision = Math.max(getPrecision(step), getPrecision(min)); const tare = minValue % stepValue ? minValue : 0; const fromMin = Math.round((baseValue - tare) / stepValue) * stepValue + tare; return precision ? getNumber(fromMin.toFixed(precision)) : fromMin; } export { add, clamp, ensureValidStep, getNumber, multiply, subtract }; //# sourceMappingURL=math.mjs.map