find-closest
Advanced tools
Comparing version 6.0.0 to 6.0.1
declare type IterationFn<T, R> = (value: T, index: number, array: T[]) => R; | ||
declare type FilterMapFn<T> = IterationFn<T, number | boolean>; | ||
declare type FilterMapFnStrict<T> = IterationFn<T, number | false>; | ||
declare type FilterMapFn<T> = [T] extends [number] ? IterationFn<T, number | boolean> | undefined : IterationFn<T, number | false>; | ||
declare type FindFn = <T>(haystack: T[], needle: number, filterMapFn: FilterMapFn<T>) => T; | ||
declare type FindIndexFn = <T>(haystack: T[], needle: number, filterMapFn: FilterMapFn<T>) => number; | ||
/** | ||
@@ -8,4 +9,3 @@ * Returns the index of the item in an array that is closest to the value | ||
*/ | ||
export declare function findClosestIndex(haystack: number[], needle: number, filterMapFn?: FilterMapFn<number>): number; | ||
export declare function findClosestIndex<T>(haystack: T[], needle: number, filterMapFn: FilterMapFnStrict<T>): number; | ||
export declare const findClosestIndex: FindIndexFn; | ||
/** | ||
@@ -15,4 +15,3 @@ * Returns the item in an array that is closest to the value specified by the | ||
*/ | ||
export declare function findClosest(haystack: number[], needle: number, filterMapFn?: FilterMapFn<number>): number; | ||
export declare function findClosest<T>(haystack: T[], needle: number, filterMapFn: FilterMapFnStrict<T>): T; | ||
export declare const findClosest: FindFn; | ||
export default findClosest; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// TODO: Look into creating a "sortedFindClosest", like https://lodash.com/docs/4.17.11#sortedIndexOf | ||
function baseFindClosestIndex(haystack, needle, filterMapFn) { | ||
/** | ||
* Returns the index of the item in an array that is closest to the value | ||
* specified by the `needle` argument. | ||
*/ | ||
exports.findClosestIndex = function (haystack, needle, filterMapFn) { | ||
var closest = { | ||
index: -1, | ||
distance: Number.POSITIVE_INFINITY, | ||
value: 0 | ||
value: 0, | ||
}; | ||
for (var index = 0; index < haystack.length; index++) { | ||
var value = haystack[index]; | ||
var rawValue = haystack[index]; | ||
var value = rawValue; | ||
if (filterMapFn) { | ||
var mappedValue = filterMapFn(value, index, haystack); | ||
var mappedValue = filterMapFn(rawValue, index, haystack); | ||
switch (mappedValue) { | ||
case false: | ||
continue; | ||
case true: | ||
break; | ||
case false: | ||
continue; | ||
default: | ||
@@ -38,12 +42,11 @@ value = mappedValue; | ||
return closest.index; | ||
} | ||
function findClosestIndex(haystack, needle, filterMapFn) { | ||
return baseFindClosestIndex(haystack, needle, filterMapFn); | ||
} | ||
exports.findClosestIndex = findClosestIndex; | ||
function findClosest(haystack, needle, filterMapFn) { | ||
return haystack[baseFindClosestIndex(haystack, needle, filterMapFn)]; | ||
} | ||
exports.findClosest = findClosest; | ||
exports.default = findClosest; | ||
}; | ||
/** | ||
* Returns the item in an array that is closest to the value specified by the | ||
* `needle` argument. | ||
*/ | ||
exports.findClosest = function (haystack, needle, filterMapFn) { | ||
return haystack[exports.findClosestIndex(haystack, needle, filterMapFn)]; | ||
}; | ||
exports.default = exports.findClosest; | ||
//# sourceMappingURL=findClosest.js.map |
{ | ||
"name": "find-closest", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "Like Array.prototype.find, but for finding the closest match.", | ||
@@ -17,8 +17,12 @@ "keywords": [ | ||
"main": "dist/findClosest.js", | ||
"files": [ | ||
"/dist" | ||
], | ||
"scripts": { | ||
"build": "npm run clean && tsc", | ||
"clean": "rimraf dist", | ||
"lint": "tslint --format stylish --project .", | ||
"test": "jest", | ||
"prepublish": "npm test && npm run lint && npm run build" | ||
"build": "tsc", | ||
"prepublish": "npm run build", | ||
"prebuild": "npm test && npm run lint && npm run clean" | ||
}, | ||
@@ -32,10 +36,10 @@ "author": "Daniel Levett", | ||
"devDependencies": { | ||
"@dlevs/tslint-config": "1.0.2", | ||
"@types/jest": "23.3.1", | ||
"jest": "23.5.0", | ||
"rimraf": "2.6.2", | ||
"ts-jest": "23.1.4", | ||
"tslint": "5.11.0", | ||
"typescript": "3.0.3" | ||
"@dlevs/tslint-config": "1.0.4", | ||
"@types/jest": "24.0.18", | ||
"jest": "24.9.0", | ||
"rimraf": "3.0.0", | ||
"ts-jest": "24.1.0", | ||
"tslint": "5.20.0", | ||
"typescript": "3.6.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7712
6
65
1