UNPKG

dev-utils-plus

Version:

Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math

210 lines 5.27 kB
"use strict"; /** * Math utility functions for common mathematical operations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.clamp = clamp; exports.lerp = lerp; exports.mapRange = mapRange; exports.isBetween = isBetween; exports.roundTo = roundTo; exports.percentage = percentage; exports.factorial = factorial; exports.gcd = gcd; exports.lcm = lcm; exports.isPrime = isPrime; exports.generatePrimes = generatePrimes; exports.sum = sum; exports.average = average; exports.median = median; exports.mode = mode; exports.standardDeviation = standardDeviation; exports.variance = variance; exports.randomInt = randomInt; exports.randomFloat = randomFloat; /** * Clamps a number between min and max values */ function clamp(value, min, max) { return Math.min(Math.max(value, min), max); } /** * Linear interpolation between two values */ function lerp(start, end, t) { return start + (end - start) * t; } /** * Maps a value from one range to another */ function mapRange(value, inMin, inMax, outMin, outMax) { return ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin; } /** * Checks if a number is between two values (inclusive) */ function isBetween(value, min, max) { return value >= min && value <= max; } /** * Rounds a number to a specified number of decimal places */ function roundTo(value, decimals) { const factor = Math.pow(10, decimals); return Math.round(value * factor) / factor; } /** * Calculates the percentage of a value relative to a total */ function percentage(value, total) { if (total === 0) return 0; return (value / total) * 100; } /** * Calculates the factorial of a number */ function factorial(n) { if (n < 0) return NaN; if (n === 0 || n === 1) return 1; let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; } /** * Calculates the greatest common divisor of two numbers */ function gcd(a, b) { while (b !== 0) { const temp = b; b = a % b; a = temp; } return a; } /** * Calculates the least common multiple of two numbers */ function lcm(a, b) { return Math.abs(a * b) / gcd(a, b); } /** * Checks if a number is prime */ function isPrime(n) { if (n < 2) return false; if (n === 2) return true; if (n % 2 === 0) return false; const sqrt = Math.sqrt(n); for (let i = 3; i <= sqrt; i += 2) { if (n % i === 0) return false; } return true; } /** * Generates an array of prime numbers up to a limit */ function generatePrimes(limit) { const sieve = new Array(limit + 1).fill(true); sieve[0] = sieve[1] = false; for (let i = 2; i <= Math.sqrt(limit); i++) { if (sieve[i]) { for (let j = i * i; j <= limit; j += i) { sieve[j] = false; } } } return sieve .map((isPrime, index) => isPrime ? index : null) .filter((num) => num !== null); } /** * Calculates the sum of an array of numbers */ function sum(numbers) { return numbers.reduce((acc, num) => acc + num, 0); } /** * Calculates the average of an array of numbers */ function average(numbers) { if (numbers.length === 0) return 0; return sum(numbers) / numbers.length; } /** * Calculates the median of an array of numbers */ function median(numbers) { if (numbers.length === 0) return 0; const sorted = [...numbers].sort((a, b) => a - b); const mid = Math.floor(sorted.length / 2); if (sorted.length % 2 === 0) { const mid1 = sorted[mid - 1]; const mid2 = sorted[mid]; if (mid1 !== undefined && mid2 !== undefined) { return (mid1 + mid2) / 2; } } const midValue = sorted[mid]; return midValue !== undefined ? midValue : 0; } /** * Calculates the mode of an array of numbers */ function mode(numbers) { if (numbers.length === 0) return []; const frequency = {}; let maxFreq = 0; numbers.forEach(num => { frequency[num] = (frequency[num] || 0) + 1; maxFreq = Math.max(maxFreq, frequency[num]); }); return Object.entries(frequency) .filter(([, freq]) => freq === maxFreq) .map(([num]) => parseInt(num)); } /** * Calculates the standard deviation of an array of numbers */ function standardDeviation(numbers) { if (numbers.length === 0) return 0; const avg = average(numbers); const squaredDiffs = numbers.map(num => Math.pow(num - avg, 2)); const variance = average(squaredDiffs); return Math.sqrt(variance); } /** * Calculates the variance of an array of numbers */ function variance(numbers) { if (numbers.length === 0) return 0; const avg = average(numbers); const squaredDiffs = numbers.map(num => Math.pow(num - avg, 2)); return average(squaredDiffs); } /** * Generates a random integer between min and max (inclusive) */ function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } /** * Generates a random float between min and max */ function randomFloat(min, max) { return Math.random() * (max - min) + min; } //# sourceMappingURL=index.js.map