Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

didyoumean3

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

didyoumean3

🚀 the super fast and easy didyoumean which use dice-coefficient and levenshtein

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
867
increased by51.84%
Maintainers
1
Weekly downloads
 
Created
Source

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 shortest edit distance algorithm -> levenshtein and dice-coefficient
  • Support custom algorithms
  • Support custom return results
  • Typescript
  • Super fast
  • 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, 
// you must also specify a comparator to indicate whether the output is the smallest or the largest
const your_leven = require('some/leven');
const your_comparator = (a: number, b: number) => a < b;
didyoumean3(input, list, { similar: your_leven, comparator: your_comparator })


// 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 passing in an array of objects, you can specify the return result through val
 * @type {string | Function} similar: use builtin shortest edit-distance algorithm or yours
 * @type {Function} result: you can custom your return result
 * @type {Function} compartor: you can custom the compare rules, because will maybe use the highest score or the lowest score
 */
export type Options = {
  ignore?: boolean, // default false
  trim?: boolean, // default true
  trimAll?: boolean, // default false
  diacritics?: boolean, // default false
  normalize?: Normalize, // default undefined
  val?: Val, // default undefined
  similar?: BuiltInSimilar | Similar, // default leven
  result?: Return, // default undefined
  compartor?: Compartor // default undefined
}

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!

license

MIT

Keywords

FAQs

Package last updated on 18 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc