Clamp a number to a range
Keep a number inside an inclusive minimum and maximum.
function clamp({ value, min, max }) { return Math.min(max, Math.max(min, value)); }numberCollection · 22 utilities
Small numeric transformations with explicit validation and floating-point caveats. These are not financial arithmetic policies.
Keep a number inside an inclusive minimum and maximum.
function clamp({ value, min, max }) { return Math.min(max, Math.max(min, value)); }numberApply decimal scaling and JavaScript rounding for display-oriented values.
function roundTo({ value, digits }) { const factor = 10 ** digits; return Math.round(value * fa…numberAppend st, nd, rd or th to a positive integer.
function addOrdinal(n) { return `${n}${['st', 'nd', 'rd'][((((n + 90) % 100) - 10) % 10) - 1] |…numberReturn the nth Fibonacci number with memoized recursion.
function fibonacci(n, memo = {}) { return memo[n] || (n <= 2 ? 1 : (memo[n] = fibonacci(n - 1, …numberDivide the first number by each of the others in turn.
function division(values) { return values.reduce((a, b) => a / b); }numberWrap an index into 0…length−1, also for negative numbers.
function mod({ index, length }) { return ((index % length) + length) % length; }numberApply Euclid’s algorithm.
function gcd({ a, b }) { return b === 0 ? Math.abs(a) : gcd({ a: b, b: a % b }); }numberDivide the sum by the count.
function averageOf(values) { return values.reduce((a, b) => a + b) / values.length; }numberApply % from left to right.
function remainder(values) { return values.reduce((a, b) => a % b); }numberSubtract each number from the first in turn.
function subtract(values) { return values.reduce((a, b) => a - b); }numberMultiply the integers from 1 to n recursively.
function factorial(n) { return n <= 1 ? 1 : n * factorial(n - 1); }numberAdd numbers from left to right.
function sumOf(values) { return values.reduce((a, b) => a + b); }numberTurn a zero-based index into a spreadsheet-style column name.
function toChars(n) { return `${n >= 26 ? toChars(Math.floor(n / 26) - 1) : ''}${'ABCDEFGHIJKLM…numberMultiply numbers from left to right.
function multiply(values) { return values.reduce((a, b) => a * b); }numberDrop the fractional part toward zero.
function truncate(n) { return Math.trunc(n); }numberUse unary plus.
function toNumber(text) { return +text; }numberBuild the binary digits of a non-negative integer as a decimal-looking number.
function decToBinary(num) { return num === 0 ? 0 : (num % 2) + 10 * decToBinary(Math.floor(num …numberPad a non-negative integer to a given width.
function prefixWithZeros({ n, length }) { return String(n).padStart(length, '0'); }numberCut the decimal representation after a number of digits.
function toFixedDown({ n, digits }) { return Number(`${n}`.match(new RegExp(`^-?\\d+(?:\\.\\d{0…numberMap a number into [min, max) like an angle or a carousel index.
function wrap({ num, min, max }) { return ((((num - min) % (max - min)) + (max - min)) % (max -…numberSplit a non-negative integer into its decimal digits.
function digitize(n) { return [...`${n}`].map(digit => Number(digit)); }numberReturn the overlap of two closed ranges, or an empty array.
function intersectRanges({ a, b }) { return a[0] > b[1] || a[1] < b[0] ? [] : [Math.max(a[0], b…numberTry a function in the local code lab, then read its limits before using it in an application.