Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
find-closest
Advanced tools
This module provides functions equivalent to Array.prototype.find
and Array.prototype.findIndex
, but for finding the closest value where an exact match may not exist.
npm install find-closest
The default behaviour is to compare numbers in an array to the target number provided. The closest match is returned:
import { findClosest, findClosestIndex } from 'find-closest'
findClosest([0, 10, 20, 30], 12)
// => 10
findClosestIndex([0, 10, 20, 30], 12)
// => 1
filterMapFn
An optional filterMapFn
function can be passed to compare non-number values to the target:
const pets = [
{ name: 'Fluffy', age: 10 },
{ name: 'Biscuit', age: 6 },
{ name: 'Wilbur', age: 12 },
]
findClosest(pets, 7, ({ age }) => age)
// => { name: 'Biscuit', age: 6 }
Additionally, returning false
from this function omits the value:
const isGreaterThan10 = (n) => n > 10
findClosest([0, 10, 20, 30], 12)
// => 10
findClosest([0, 10, 20, 30], 12, isGreaterThan10)
// => 20
The mapping and filtering can be performed by the same function:
const pets = [
{ name: 'Fluffy', age: 10 },
{ name: 'Biscuit', age: 6 },
{ name: 'Wilbur', age: 12 },
]
findClosest(pets, 7, ({ age }) => {
if (age < 7) {
return false
}
return age
})
// => { name: 'Fluffy', age: 10 }
Note that, unless all the values in the array are numbers, the filterMapFn
cannot return true
- attempting to do so will cause an error to be thrown.
context
argumentfilterMapFn
also receives a second argument with context information, allowing the last example to be rewritten like this:
findClosest(pets, 7, (pet, context) => {
if (pet.age < context.target) {
return false
}
return pet.age
})
The context
argument also has context.index
and context.array
properties.
The filterMapFn
argument has potential performance gains over manually calling .map().filter()
on the input array:
FAQs
Like Array.prototype.find, but for finding the closest match.
The npm package find-closest receives a total of 475 weekly downloads. As such, find-closest popularity was classified as not popular.
We found that find-closest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.