Package Exports
- didyoumean3
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 (didyoumean3) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
didyoumean3
notice: Covers most situations and still needs to be optimized, i will do better!
features
- Built-in fastest shortest edit distance algorithm -> levenshtein and dice-coefficient
- Support custom algorithms
- Support custom return results
- Typescript
- Super fast
- More flexible configuration
- Super small (production.min.js ~ 2kb) and tree shaking!
- Support emoji or diacritics
usage
install
npm i didyoumean3
return results
// if none match return null
didyoumean3('', ['anything']); // null
// else will a object like 👇:
{
winner: 'the best matched item',
matches: [
{
score: 0.1,
target: 'item'
},
//...
]
}
// or you can use a custom function to specify the result, just add an option "result"
didyoumean3('', ['anything'], { result: x => x || 'no matched!' });
details
const didyoumean3 = require('didyoumean3').default
// or if you are using TypeScript or ES module
import didyoumean3 from 'didyoumean3'
let input = 'insargrm'
let list = [
'facebook', 'INSTAgram', ' in stagram', 'baidu', 'twitter', 'wechat', 'instagram', 'linkedin'
]
// levenshtein
didyoumean3(input, list)?.winner // instagram
// dice-coefficient
didyoumean3(input, list, { similar: 'dice' })?.winner // instagram
// or use your custom algorithm
// notice: If you customize the algorithm, the optimal route must take the minimum
const your_leven = require('some/your_leven');
const your_comparator = (a: number, b: number) => a < b;
didyoumean3(input, list, { similar: your_leven })
// specify the way you get the value
const val = item => item.id;
didyoumean3(input, [{id: 'facebook'}, {id: 'baidu'}, {id: 'instagram'}], { vaL })?.winner; // {id: 'instagram'}
options description
I'm lazy, I just give the declaration file 👇
export interface Val {
(x: string | object): string
}
export interface Similar {
(a: string, b: string, opts?: Partial<Options>): number
}
export interface Return {
(x: any): any
}
export interface Normalize {
(x: string): string
}
// dice-coefficient or levenshtein
export type BuiltInSimilar = 'dice' | 'leven'
export type Result <T extends string | object> = {
winner: T,
matches: ReadonlyArray<T>,
[key: string]: any
} | null
/**
* @type {boolean} ignore: ignore case 'A' -> 'a'
* @type {boolean} trim: ' a bcs ' -> 'a bcs'
* @type {boolean} trimAll: ' a bcs' -> 'abcs'
* @type {boolean} diacritics: 'café' -> 'café'.normalize()
* @type {Function} val: when you need find the best result in a object list, it's useful
* @type {string | Function} similar: use builtin shortest edit-distance algorithm or yours
* @type {Function} result: you can custom your return result
* @type {Function} filter: you can filter the data into the returned results
*/
export type Options = {
ignore?: boolean;
trim?: boolean;
trimAll?: boolean;
diacritics?: boolean;
normalize?: Normalize;
val?: Val;
similar?: BuiltInSimilar | Similar;
result?: Return;
filter?: Filter;
};
benchmark
didyoumean x 194,593 ops/sec ±1.07% (84 runs sampled)
didyoumean2 x 311,318 ops/sec ±0.63% (90 runs sampled)
didyoumean3-leven x 510,067 ops/sec ±0.48% (84 runs sampled)
didyoumean3-dice x 294,427 ops/sec ±0.46% (85 runs sampled)
Fastest is didyoumean3-leven
contributors
nobody now.
Both issure and pr are welcome!