Socket
Socket
Sign inDemoInstall

didyoumean2

Package Overview
Dependencies
4
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

didyoumean2

a library for matching human-quality input to a list of potential matches using the Levenshtein distance algorithm


Version published
Maintainers
1
Weekly downloads
106,628
decreased by-7.63%
Install size
395 kB

Weekly downloads

Readme

Source

didyoumean2

Build Status codecov.io

node npm npm

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

  1. Based on fastest-levenshtein, the fastest JS implementation of the Levenshtein distance algorithm

  2. ~100% faster than didyoumean.js

  3. Well tested with 100% coverage

  4. Static type checking with TypeScript

  5. More control on what kind of matches you want to return

  6. Support matching object's path instead of just key

Installation

npm install didyoumean2
const didYouMean = require('didyoumean2').default
// or if you are using TypeScript or ES module
import didYouMean from 'didyoumean2'

// you can also access to Enums via:
const {
  default: didYouMean,
  ReturnTypeEnums,
  ThresholdTypeEnums,
} = require('didyoumean2')
// or
import didYouMean, { ReturnTypeEnums, ThresholdTypeEnums } from 'didyoumean2'

Development Setup

We are using corepack to manage the yarn version

corepack enable

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

deburr {boolean}
matchPath {Array}
  • default: []

  • If your matchList is an array of object, you must use matchPath to point to the string that you want to match

  • Refer to ramda R.path for how to define the path, e.g. ['obj', 'array', 0, 'key']

returnType {string}
  • default: ReturnTypeEnums.FIRST_CLOSEST_MATCH
returnTypeDescription
ReturnTypeEnums.ALL_CLOSEST_MATCHESReturn all matches with the closest value to the input in array
ReturnTypeEnums.ALL_MATCHESReturn all matches in array
ReturnTypeEnums.ALL_SORTED_MATCHESReturn all matches in array, sorted from closest to furthest
ReturnTypeEnums.FIRST_CLOSEST_MATCHReturn first match from ReturnTypeEnums.ALL_CLOSEST_MATCHES
ReturnTypeEnums.FIRST_MATCHReturn first match (FASTEST)
threshold {integer|number}
  • depends on thresholdType

  • type: {number} (similarity) or {integer} (edit-distance)

  • default: 0.4 (similarity) or 20 (edit-distance)

  • If the result is larger (similarity) or smaller (edit-distance) than or equal to the threshold, that result is matched

thresholdType {string}
  • default: ThresholdTypeEnums.SIMILARITY
thresholdTypeDescription
ThresholdTypeEnums.EDIT_DISTANCERefer to Levenshtein distance algorithm, must be integer, lower value means more similar
ThresholdTypeEnums.SIMILARITYl = max(input.length, matchItem.length), similarity = (l - editDistance) / l, number from 0 to 1, higher value means more similar
trimSpaces {boolean}
  • default: true

  • Remove noises when matching

  • Trim all starting and ending spaces, and concatenate all continuous spaces to one space

Test

Before all:

npm install -g yarn
yarn install

Unit test and coverage:

yarn test

Linter:

yarn lint

Keywords

FAQs

Last updated on 07 Sep 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc