UNPKG

find-closest

Version:

Like Array.prototype.find, but for finding the closest match.

3 lines (2 loc) 934 B
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).findClosest={})}(this,function(e){var n=function(e,n,t){for(var r={index:-1,distance:Number.POSITIVE_INFINITY,value:0},i=0;i<e.length;i++){var o=e[i],a=void 0;if(t){var f=t(o,{index:i,target:n,collection:e});switch(f){case!1:continue;case!0:if("number"!=typeof o)throw new TypeError("`filterMapFn` returned `true` for non-number value `"+o+"`.");a=o;break;default:a=f}}else{if("number"!=typeof o)throw new TypeError("Array contains non-number value `"+o+"` without a `filterMapFn` to map it to a number.");a=o}var u=Math.abs(a-n);if(0===u)return i;u<r.distance&&(r={index:i,distance:u,value:a})}return r.index},t=n,r=function(){var e=[].slice.call(arguments);return e[0][n.apply(void 0,e)]};e.default=r,e.findClosest=r,e.findClosestIndex=t}); //# sourceMappingURL=index.umd.js.map