UNPKG

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.

27 lines (26 loc) 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findAnomalies = void 0; /** * Finds values that differ statistically or structurally. * @author @dailker * @param {any[]} array * @param {object} [options] * @returns {any[]} */ function findAnomalies(array, options) { if (options?.zScore && array.every(x => typeof x === 'number')) { const mean = array.reduce((a, b) => a + b, 0) / array.length; const std = Math.sqrt(array.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / array.length); return array.filter(x => Math.abs((x - mean) / (std || 1)) > 2); } // Type anomaly: lone type const typeGroups = array.reduce((acc, v) => { const t = typeof v; acc[t] = (acc[t] || 0) + 1; return acc; }, {}); const rareTypes = Object.entries(typeGroups).filter(([_, c]) => c === 1).map(([t]) => t); return array.filter(x => rareTypes.includes(typeof x)); } exports.findAnomalies = findAnomalies;