UNPKG

@exabytellc/utils

Version:

EB react utils to make everything a little easier!

175 lines (167 loc) 6.07 kB
import { handleTryCatch } from "../_helpers/handleError"; import { isBool, isNum, isStr } from "../types"; export function clamp(value, min, max) { return handleTryCatch({ n: "clamp", c: !isNum(value) || !isNum(min) || !isNum(max) || min > max, p: { value, min, max }, e: () => { return Math.min(max, Math.max(value, min)); } }); } export function forceNum(value) { return Number(value?.replace(/\D+/g, '')); } export function loop(value, step, min, max) { return handleTryCatch({ n: "loop", c: !isNum(value) || !isNum(step) || !isNum(min) || !isNum(max), p: { value, step, min, max }, e: () => { const rangeSize = max - min + 1; value = (value - min + step) % rangeSize; if (value < 0) { value += rangeSize; } return value + min; } }); } export function randomNum(min = 0, max = 1, precision = null) { return handleTryCatch({ n: "randomNum", c: !isNum(min) || !isNum(max) || !isNum(precision, true), p: { min, max, precision }, e: () => { const rand = Math.random() * (max - min) + min; return precision ? round(rand, precision) : rand; } }); } export function randomNumSeed(seed = "", min = 0, max = 1, precision = null) { return handleTryCatch({ n: "randomNumSeed", c: !isStr(seed) || !isNum(min) || !isNum(max) || !isNum(precision, true), p: { seed, min, max, precision }, e: () => { const rand = splitmix32(cyrb128(seed)[0]) * (max - min) + min; return precision ? round(rand, precision) : rand; } }); function splitmix32(a) { a |= 0; a = a + 0x9e3779b9 | 0; var t = a ^ a >>> 16; t = Math.imul(t, 0x21f0aaad); t = t ^ t >>> 15; t = Math.imul(t, 0x735a2d97); return ((t = t ^ t >>> 15) >>> 0) / 4294967296; } function cyrb128(str) { let h1 = 1779033703, h2 = 3144134277, h3 = 1013904242, h4 = 2773480762; for (let i = 0, k; i < str.length; i++) { k = str.charCodeAt(i); h1 = h2 ^ Math.imul(h1 ^ k, 597399067); h2 = h3 ^ Math.imul(h2 ^ k, 2869860233); h3 = h4 ^ Math.imul(h3 ^ k, 951274213); h4 = h1 ^ Math.imul(h4 ^ k, 2716044179); } h1 = Math.imul(h3 ^ h1 >>> 18, 597399067); h2 = Math.imul(h4 ^ h2 >>> 22, 2869860233); h3 = Math.imul(h1 ^ h3 >>> 17, 951274213); h4 = Math.imul(h2 ^ h4 >>> 19, 2716044179); h1 ^= h2 ^ h3 ^ h4, h2 ^= h1, h3 ^= h1, h4 ^= h1; return [h1 >>> 0, h2 >>> 0, h3 >>> 0, h4 >>> 0]; } } export function randomInt(min = 0, max = 10) { return Math.round(randomNum(min, max, 0)); } export function randomIntSeed(seed = "", min = 0, max = 10) { return Math.round(randomNumSeed(seed, min, max, 0)); } export function readableNum(value, precision = 1, separator = ",", persistentPrecision = false) { return handleTryCatch({ n: "readableNum", c: !isNum(value) || !isNum(precision) || !isStr(separator) || !isBool(persistentPrecision), p: { value, precision, separator, persistentPrecision }, e: () => { // Convert value to number and handle NaN value = parseFloat(value); if (isNaN(value)) throw new Error('Invalid number'); const absoluteValue = Math.abs(value); const roundedValue = round(absoluteValue, precision); const [integerPart, decimalPart] = roundedValue.toFixed(precision).split("."); let formattedIntegerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, separator); if (Math.sign(value) < 0) formattedIntegerPart = `-${formattedIntegerPart}`; let formattedDecimalPart = precision > 0 || persistentPrecision ? decimalPart.padEnd(precision, "0") : ''; return `${formattedIntegerPart}${precision > 0 || persistentPrecision ? `.${formattedDecimalPart}` : ''}`; } }); } export function round(value, precision = 0) { return handleTryCatch({ n: "round", c: !isNum(value) || !isNum(precision), p: { value, precision }, e: () => { return +(parseFloat(value).toFixed(precision)); } }); } export function numLinearTo(value, step, target) { return handleTryCatch({ n: "linearTo", c: !isNum(value) || !isNum(step) || !isNum(target), p: { value, step, target }, e: () => { const difference = target - value; const absStep = Math.abs(step); if (difference > 0) { value = Math.min(value + absStep, target); } else if (difference < 0) { value = Math.max(value - absStep, target); } return value; } }); } export function numEaseTo(value, strength, target, start) { return handleTryCatch({ n: "easeInTo", c: !isNum(value) || !isNum(strength) || !isNum(target) || !isNum(start), p: { value, strength, target, start }, e: () => { const difference = start - start; const stepSize = Math.abs(difference) * strength; if (difference > 0) { return value + stepSize; } else if (difference < 0) { return value - stepSize; } return value; } }); } export function numExponentialTo(value, minInput, maxInput, minOutput, maxOutput, growthRate = 2) { return handleTryCatch({ n: "numExponentialTo", c: !isNum(value) || !isNum(minInput) || !isNum(maxInput) || !isNum(minOutput) || !isNum(maxOutput) || !isNum(growthRate), p: { value, minInput, maxInput, minOutput, maxOutput, growthRate }, e: () => { if (growthRate == 1 || growthRate <= 0) return value; if (value < minInput) return minOutput; if (value > maxInput) return maxOutput; // Normalize the input value to a 0-1 range const normalizedInput = (value - minInput) / (maxInput - minInput); // Apply the exponential growth formula const outputRange = maxOutput - minOutput; const output = minOutput + (outputRange * (growthRate ** normalizedInput - 1) / (growthRate - 1)); return output; } }); }