New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

find-closest

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-closest - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

dist/findClosest.d.ts

27

package.json
{
"name": "find-closest",
"version": "2.0.1",
"version": "3.0.0",
"description": "Like Array.prototype.find, but for finding the closest match.",
"main": "index.js",
"main": "dist/findClosest.js",
"scripts": {
"test": "jest"
"build": "npm run clean && tsc",
"clean": "rimraf dist",
"lint": "tslint --format stylish --project .",
"test": "jest",
"prepublish": "npm test && npm run lint && npm run build"
},
"author": "Daniel Levett",
"license": "ISC",
"license": "MIT",
"repository": {

@@ -16,10 +20,11 @@ "type": "git",

"devDependencies": {
"babel-core": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"fast-levenshtein": "^2.0.6",
"jest": "^19.0.2"
},
"dependencies": {
"lodash.get": "^4.4.2"
"@dlevs/tslint-config": "1.0.2",
"@types/jest": "23.3.1",
"fast-levenshtein": "2.0.6",
"jest": "23.5.0",
"rimraf": "2.6.2",
"ts-jest": "23.1.4",
"tslint": "5.11.0",
"typescript": "3.0.3"
}
}

@@ -1,14 +0,27 @@

## About
# find-closest
This module provides functions equivalent to `Array.prototype.find` and `Array.prototype.indexOf`, but for finding the closest value where an exact match may not exist.
## Installation
`npm install find-closest`
## API
- [findClosest](#findclosest)
- [findClosestIndex](#findclosestindex)
- [find-closest](#find-closest)
- [Installation](#installation)
- [API](#api)
- [findClosest](#findclosest)
- [Basic usage](#basic-usage)
- [Compare with a custom function](#compare-with-a-custom-function)
- [Example: Comparing objects by a key](#example-comparing-objects-by-a-key)
- [Example: Comparing likeness of strings](#example-comparing-likeness-of-strings)
- [findClosestIndex](#findclosestindex)
### findClosest
#### Basic usage
The default behaviour is to compare numbers in an array to the target number provided. The closest match is returned.
```javascript

@@ -21,36 +34,34 @@ import findClosest from 'find-closest';

#### Compare by object properties
```javascript
const users = [
{details: {name: 'Bob', age: 20}},
{details: {name: 'Laura', age: 24}}
];
#### Compare with a custom function
findClosest(users, 30, 'details.age');
findClosest(users, 30, ['details', 'age']);
// both return the object for Laura
```
#### Compare with a custom function
To compare values other than numbers, a comparison function may be passed as the third argument to `findClosest`. This is invoked for each item in the array.
The comparison function is passed the current array item and the target value to find. It is expected to return a number indicating how similar the two values are. The results of this function determine which array item is returned by `findClosest`:
- The item with the lowest result is returned.
- If multiple items in the array share the lowest result, the first occurance is returned.
- If multiple items in the array share the lowest result, the first occurrence is returned.
- `0` indicates the closest possible likeness.
This example shows how to get the string with the closest length provided:
##### Example: Comparing objects by a key
This example shows how to compare objects by a key:
```javascript
const names = ['jim', 'jimmy', 'jimbob', 'jimbobby'];
const pets = [
{ name: 'Fluffy', age: 10 },
{ name: 'Biscuit', age: 6 },
{ name: 'Wilbur', age: 12 }
];
const ageComparer = ({ age }, targetAge) =>
Math.abs(age - targetAge);
const lengthComparer = (value, targetLength) =>
Math.abs(value.length - targetLength);
findClosest(pets, 4, ageComparer);
// returns object for Biscuit
findClosest(names, 4, lengthComparer);
// returns 'jim'
findClosest(names, 7, lengthComparer);
// returns 'jimbob'
findClosest(pets, 11, ageComparer);
// returns object for Fluffy
```
##### Example: Comparing likeness of strings
The following example shows usage with a third-party levenshtein distance module, installed by running `npm install fast-levenshtein`. This compares the similarity of strings.

@@ -68,3 +79,5 @@

### findClosestIndex
Like `findClosest`, except it returns the index instead of the value from the array.
Like [findClosest](#findclosest), except it returns the index instead of the value from the array.
```javascript

@@ -71,0 +84,0 @@ import { findClosestIndex } from 'find-closest';

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc