UNPKG

find-closest

Version:

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

3 lines (2 loc) 759 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 l=Math.abs(o-r);if(0===l)return a;l<t.distance&&(t={index:a,distance:l,value:o})}return t.index},r=e,n=function(){var r=[].slice.call(arguments);return r[0][e.apply(void 0,r)]};exports.default=n,exports.findClosest=n,exports.findClosestIndex=r; //# sourceMappingURL=index.js.map