Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

didyoumean2

foray1010449kMIT7.0.4TypeScript support: included

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

closest, compare, comparison, didyoumean, diff, difference, distance, edit, find, fuzzy, leven, levenshtein, match, matching, similar, similarity, string, suggest, suggestion, text, typescript, word

readme

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
returnType Description
ReturnTypeEnums.ALL_CLOSEST_MATCHES Return all matches with the closest value to the input in array
ReturnTypeEnums.ALL_MATCHES Return all matches in array
ReturnTypeEnums.ALL_SORTED_MATCHES Return all matches in array, sorted from closest to furthest
ReturnTypeEnums.FIRST_CLOSEST_MATCH Return first match from ReturnTypeEnums.ALL_CLOSEST_MATCHES
ReturnTypeEnums.FIRST_MATCH Return 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
thresholdType Description
ThresholdTypeEnums.EDIT_DISTANCE Refer to Levenshtein distance algorithm, must be integer, lower value means more similar
ThresholdTypeEnums.SIMILARITY l = 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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

7.0.4 (2024-09-17)

Bug Fixes

  • does not include any files on last release (b899e53)

7.0.3 (2024-09-17)

7.0.2 (2024-07-21)

Bug Fixes

  • cannot get types in jest (4a7344f)

7.0.1 (2024-07-21)

Bug Fixes

  • add back main entry point as eslint-plugin-import does not support conditional exports (42ef97c)

7.0.0 (2024-07-21)

⚠ BREAKING CHANGES

  • only support conditional exports for entry point
  • drop support for umd build
  • require node ^18.12.0 || >=20.9.0

Bug Fixes

  • temporarily remove husky as failed to install by npm 10.4.0 (c300cc1)

  • require node ^18.12.0 || >=20.9.0 (3ebb83e)

build

  • drop support for umd build (3933072)
  • only support conditional exports for entry point (f6c23c1)

6.0.1 (2023-09-07)

Bug Fixes

  • types: add mts types declaration file (6881520)

6.0.0 (2023-09-07)

⚠ BREAKING CHANGES

  • require node ^16.14.0 || >=18.12.0
  • drop nodejs 14.17 and 18.11
  • drop node.js v12 and v17
  • only support node ^12.22.0 || ^14.17.0 || >=16.13.0

Bug Fixes

  • compatible with typescript node16 module resolution (734187d)
  • use TypeError for unknown returnType/thresholdType errors (7fd3e3e)

  • bump node version requirement (e656c70)

  • drop node.js v12 and v17 (e56e7d4)
  • drop nodejs 14.17 and 18.11 (a9d907e)
  • require node ^16.14.0 || >=18.12.0 (7e04b0b)

5.0.0 (2021-05-27)

⚠ BREAKING CHANGES

4.2.0 (2021-04-25)

Features

4.1.0 (2020-06-18)

Features

  • return different type depends on options.returnType (1a2c0ce)

Bug Fixes

  • do not use object type as eslint adviced (27315f6)

4.0.0 (2020-02-06)

⚠ BREAKING CHANGES

  • default enable deburr
  • drop nodejs < 10.13

Features

3.1.2 (2019-07-29)

Bug Fixes

  • do not throw error when matched item is not string (656f555)

3.1.1 (2019-06-09)

Build System

  • greatly reduce bundle size by rely less on ramda (69ec2b6)

3.1.0 (2019-06-07)

Build System

  • set moduleResolution to node (e7e6fb4)

Features

Tests

  • add type-coverage to ensure typescript coverage (a19cf85)

3.0.0 (2019-04-05)

Bug Fixes

chore

  • only support node >= 6.9 (8bff8be)

Features

  • accept readonly arguments (c182207)

BREAKING CHANGES

  • require TypeScript >=3.4
  • drop node >= 4.2 < 6.9

2.0.4 (2019-03-17)

Bug Fixes

  • Revert "fix(deps): update dependency leven to v3 (#132))" (b500e58) as leven v3 doesn't support node 4

2.0.3 (2019-03-17)

Bug Fixes

2.0.2 (2019-02-06)

Bug Fixes

  • do not include test files in built package (e35bbac)
  • only allow object and string for function matchItemProcessor (ce656ee)

2.0.1 (2019-02-02)

Bug Fixes

  • only allow object and string for matchList (1e58e70)

2.0.0 (2018-10-29)

Features

  • export ReturnTypeEnums and ThresholdTypeEnums (27a6b37)
  • support typescript (1cff536)
  • added "sideEffects": false flag to support tree shaking (fb31fac)

BREAKING CHANGES

  • matchPath use Array of string/number instead of string concatenated by .
  • rename trimSpace to trimSpaces
  • trimSpaces default value changed to true
  • 'random-closest-match' is removed from ReturnTypeEnums
  • schema on arguments are removed as it now depends on typescript checking
  • didYouMean function is exported under default key in export object