didyoumean3
Features
- Built-in fastest levenshtein algorithm
- Support custom return results
- Typescript
- Super fast
- More flexible configuration
- Super small (production.min.js < 2kb) and tree shaking! more info
Usage
install
npm i didyoumean3 // oryarn add didyoumean3
use case
- base use
// orconst default: didyoumean3 = ; let input = 'insargrm'let list = 'facebook' 'INSTAgram' ' in stagram' 'baidu' 'twitter' 'wechat' 'instagram' 'linkedin' console;// will print:// {// winner: 'instagram', // 🔥 This is the best match, our winner!// matched: [// {// score: 8,// target: 'facebook',// },// {// score: 3,// target: 'instagram',// },// {// score: 7,// target: 'linkedin',// },// // ...// ],// }
- optional configuration
didyoumea3
has some built-in string formatting configuration items:
-
ignore
: Ignore case when comparing, default false. -
trim
: Usestring.trim
format the string, default true. -
trimAll
: Use regexp/\s+/g
formate the string, default false. -
diacritics
: Usenormalize
Api, e.g. 'café' -> 'café'.normalize(), default false -
normalize
: Customize the formatting function by yourself.
🔥 If these parameters don't meet your requirements, you can customize the formatting function through
normalize
.
🔥 When using the custom normalize function, the above string formatting configurations will be ignored.
;
result
: Customize the structure of the results you want to return
// default result may be null or {winner: xx, matched: []}type Res = null | matched: any winner: string // you can custom your own result style!!const result = { if !res return 'nothing matched!' else return res} ;
filter
: You can filter the results you want, such as those with a score greater than 5
let i2 = 'insargrm';let l2 = 'facebook' 'instagram' 'linkedin';
benchmark
didyoumean x 159428 ops/sec ±163% 85 runs sampleddidyoumean2 x 227343 ops/sec ±126% 90 runs sampleddidyoumean3 x 400683 ops/sec ±059% 89 runs sampledFastest is didyoumean3
contributors
Both issure and pr are welcome!