UNPKG

generic-min-max

Version:

This node.js module exports a generic min-max algorithm, alongside some implementations This package comes with full typescript support!

34 lines 1.26 kB
"use strict"; // Same as the compare function in the IComparer interface from c# // https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.comparer-1.compare?view=net-5.0#System_Collections_Generic_Comparer_1_Compare__0__0_ // if num1 is "bigger" then num2 => positive number // if num2 is "bigger" then num1 => negative number // if num1 is "equal" then num2 => zero Object.defineProperty(exports, "__esModule", { value: true }); exports.getMinIndex = exports.getMaxIndex = void 0; function getMaxIndexByComparer(arr, compare) { let currentMax; let currentMaxIndex = -1; arr.forEach((num, index) => { if (index === 0) { currentMax = num; currentMaxIndex = index; } else if (compare(num, currentMax) > 0) { currentMax = num; currentMaxIndex = index; } }); return currentMaxIndex; } const getBigger = ((num1, num2) => num1 - num2); function getMaxIndex(arr) { return getMaxIndexByComparer(arr, getBigger); } exports.getMaxIndex = getMaxIndex; const getSmaller = ((num1, num2) => num2 - num1); function getMinIndex(arr) { return getMaxIndexByComparer(arr, getSmaller); } exports.getMinIndex = getMinIndex; //# sourceMappingURL=math.js.map