UNPKG

find-closest

Version:

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

3 lines (2 loc) 898 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},o=0;o<e.length;o++){var i=e[o],a=void 0;if(t){var f=t(i,{index:o,target:n,collection:e});switch(f){case!1:continue;case!0:if("number"!=typeof i)throw new TypeError("`filterMapFn` returned `true` for non-number value `"+i+"`.");a=i;break;default:a=f}}else{if("number"!=typeof i)throw new TypeError("Array contains non-number value `"+i+"` without a `filterMapFn` to map it to a number.");a=i}var u=Math.abs(a-n);if(0===u)return o;u<r.distance&&(r={index:o,distance:u,value:a})}return r.index},t=function(e,t,r){return e[n(e,t,r)]};e.default=t,e.findClosest=t,e.findClosestIndex=n}); //# sourceMappingURL=findClosest.umd.js.map