Package Exports
- didyoumean2
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (didyoumean2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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 leven, the fastest JS implementation of the Levenshtein distance algorithm
~100% faster than didyoumean.js
Well tested with 100% coverage
More control on what kind of matches you want to return
Support matching object's
path
instead of justkey
Installation
npm install didyoumean2
const didYouMean = require('didyoumean2')
Usage
didYouMean(input, matchList[, options])
input {string}
: A string that you are not sure and want to match with matchList
matchList {Object[]|string[]}
: A List for matching with input
options {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 if match
is {Object[]}
Options
caseSensitive {boolean}
default:
false
Perform case-sensitive matching
matchPath {string}
default:
''
If your
matchList
is an array of object, you must usematchPath
to point to the string that you want to matchRefer to lodash _.get for how to define the path
returnType {string}
- default:
'first-closest-match'
returnType | Description |
---|---|
'all-closest-matches' |
Return all matches with the closest value to the input in array |
'all-matches' |
Return all matches in array |
'all-sorted-matches' |
Return all matches in array, sorted from closest to furthest |
'first-closest-match' |
Return first match from 'all-closest-matches' |
'first-match' |
Return first match (FASTEST) |
'random-closest-match' |
Return a randomly pick up from 'all-closest-matches' |
threshold {integer|number}
depends on
thresholdType
type:
{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:
'similarity'
thresholdType | Description |
---|---|
'edit-distance' |
Refer to Levenshtein distance algorithm, must be integer , lower value means more similar |
'similarity' |
l = max(input.length, matchItem.length), similarity = (l - editDistance) / l , number from 0 to 1 , higher value means more similar |
Test
Unit test:
npm install
npm test
Coverage:
npm install
npm run coverage
Linter:
npm install
npm run lint