didyoumean3
馃殌 the super fast and easy didyoumean which use dice-coefficient and levenshtein
Last updated 5 months ago by huangteng .
MITRepositoryBugsOriginal npmTarballpackage.json
$ cnpm install didyoumean3 
SYNC missed versions from official npm registry.

didyoumean3

NPM

Greenkeeper badge Build Status Codecov David npm npm GitHub top language NPM

notice: Covers most situations and still needs to be optimized, i will do better!

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
  • [ ] Support emoji or diacritics

Usage

install

npm i didyoumean3

use case

  • base use
import didyoumean3 from 'didyoumean3'
// or
const { didyoumean3 } = require('didyoumean3');

let input = 'insargrm'
let list = [
  'facebook', 'INSTAgram', ' in stagram', 'baidu', 'twitter', 'wechat', 'instagram', 'linkedin'
]

console.log(didyoumean3(input, list));

// will output:
// {
//   winner: 'instagram',
//   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: default is false, Case-insensitive
  • trim: default is true, will use string.trim format the string
  • trimAll: defalut is false, will trim with regexp /\s+/g
  • diacritics: default is false, just 'caf茅' -> 'caf茅'.normalize()
  • 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 fail

didyoumean3(input, target, { normalize: (s: string) => s.trim() } );
  • val: sometimes, you need to match against a list of object. you can use val to get the target string out.
let input = [
  { id: 'facebook' },
  { id: 'baidu' },
  { id: 'twitter' },
  { id: 'INSTAgram' },
  { id: ' in stagram' },
  { id: 'wechat' },
  { id: 'instagram' },
  { id: 'linkedin' },
];

didyoumean3(input, target, { val: item => item.id } );
  • 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 = (res: Res) => {
  if (!res) return 'nothing matched!'
  else return res
}

didyoumean3(input, target, { result } );
  • 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'];
expect(
  didyoumean3(i2, l2, { filter: (score: number, item: any) => score >= 7 })
    ?.matched.length
).toBe(2); 

benchmark

didyoumean x 194,593 ops/sec 卤1.07% (84 runs sampled)
didyoumean2 x 311,318 ops/sec 卤0.63% (90 runs sampled)
didyoumean3 x 510,067 ops/sec 卤0.48% (84 runs sampled)
Fastest is didyoumean3-leven

contributors

nobody now.

Both issure and pr are welcome!

license

MIT

Current Tags

  • 1.2.3                                ...           latest (5 months ago)

14 Versions

  • 1.2.3                                ...           5 months ago
  • 1.2.2                                ...           6 months ago
  • 1.2.1                                ...           6 months ago
  • 1.2.0                                ...           6 months ago
  • 1.1.4                                ...           6 months ago
  • 1.1.3                                ...           6 months ago
  • 1.1.2                                ...           7 months ago
  • 1.1.1                                ...           7 months ago
  • 1.1.0                                ...           7 months ago
  • 1.0.1                                ...           7 months ago
  • 1.0.0                                ...           7 months ago
  • 0.1.2                                ...           7 months ago
  • 0.1.1                                ...           7 months ago
  • 0.1.0                                ...           7 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 2
Last Month 108
Dependencies (0)
None
Dev Dependencies (9)
Dependents (1)

Copyright 2014 - 2016 漏 taobao.org |