UNPKG

find-closest

Version:

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

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