everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
21 lines (20 loc) • 885 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalize = void 0;
/**
* Normalizes an array of numbers using min-max or z-score normalization.
* @author @dailker
* @param {number[]} numbers - The array of numbers to normalize.
* @param {'minmax'|'zscore'} [method='minmax'] - The normalization method.
* @returns {number[]} The normalized array.
*/
function normalize(numbers, method = 'minmax') {
if (method === 'minmax') {
const min = Math.min(...numbers), max = Math.max(...numbers);
return numbers.map(n => (n - min) / (max - min));
}
const mean = numbers.reduce((a, b) => a + b, 0) / numbers.length;
const std = Math.sqrt(numbers.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / numbers.length);
return numbers.map(n => (n - mean) / (std || 1));
}
exports.normalize = normalize;