didyoumean2 v7.0.4
didyoumean2
didyoumean2 is a library for matching human-quality input to a list of potential matches using the Levenshtein distance algorithm.
It is inspired by didyoumean.js.
Why reinventing the wheel
Based on fastest-levenshtein, the fastest JS implementation of the Levenshtein distance algorithm
~100% faster than didyoumean.js
Well tested with 100% coverage
Static type checking with TypeScript
More control on what kind of matches you want to return
Support matching object's
pathinstead of justkey
Installation
npm install didyoumean2const didYouMean = require('didyoumean2').default
// or if you are using TypeScript or ES module
import didYouMean from 'didyoumean2'
// you can also access to Enums via:
const {
default: didYouMean,
ReturnTypeEnums,
ThresholdTypeEnums,
} = require('didyoumean2')
// or
import didYouMean, { ReturnTypeEnums, ThresholdTypeEnums } from 'didyoumean2'Development Setup
We are using corepack to manage the yarn version
corepack enableUsage
didYouMean(input, matchList[, options])input {string}: A string that you are not sure and want to match withmatchListmatchList {Object[]|string[]}: A List for matching withinputoptions {Object}(optional): An options that allows you to modify the behavior@return {Array|null|Object|string}: A list of or single matched result(s), return object ifmatchis{Object[]}
Options
caseSensitive {boolean}
default:
falsePerform case-sensitive matching
deburr {boolean}
default:
truePerform combining diacritical marks insensitive matching
Refer to lodash _.deburr for how it works
matchPath {Array}
default:
[]If your
matchListis an array of object, you must usematchPathto point to the string that you want to matchRefer to ramda R.path for how to define the path, e.g.
['obj', 'array', 0, 'key']
returnType {string}
- default:
ReturnTypeEnums.FIRST_CLOSEST_MATCH
| returnType | Description |
|---|---|
ReturnTypeEnums.ALL_CLOSEST_MATCHES | Return all matches with the closest value to the input in array |
ReturnTypeEnums.ALL_MATCHES | Return all matches in array |
ReturnTypeEnums.ALL_SORTED_MATCHES | Return all matches in array, sorted from closest to furthest |
ReturnTypeEnums.FIRST_CLOSEST_MATCH | Return first match from ReturnTypeEnums.ALL_CLOSEST_MATCHES |
ReturnTypeEnums.FIRST_MATCH | Return first match (FASTEST) |
threshold {integer|number}
depends on
thresholdTypetype:
{number}(similarity) or{integer}(edit-distance)default:
0.4(similarity) or20(edit-distance)If the result is larger (
similarity) or smaller (edit-distance) than or equal to thethreshold, that result is matched
thresholdType {string}
- default:
ThresholdTypeEnums.SIMILARITY
| thresholdType | Description |
|---|---|
ThresholdTypeEnums.EDIT_DISTANCE | Refer to Levenshtein distance algorithm, must be integer, lower value means more similar |
ThresholdTypeEnums.SIMILARITY | l = max(input.length, matchItem.length), similarity = (l - editDistance) / l, number from 0 to 1, higher value means more similar |
trimSpaces {boolean}
default:
trueRemove noises when matching
Trim all starting and ending spaces, and concatenate all continuous spaces to one space
Test
Before all:
npm install -g yarn
yarn installUnit test and coverage:
yarn testLinter:
yarn lint1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
4 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago