# didyoumean2

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

Latest version **7.0.4** (published 2024-09-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install didyoumean2
pnpm add didyoumean2
yarn add didyoumean2
bun add didyoumean2
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 7.0.4 |
| Published | 2024-09-17 |
| First published | 2016-01-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^18.12.0 \|\| >=20.9.0 |
| Dependencies | 3 |
| Unpacked size | 75.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 106 |
| Author | foray1010 |
| Maintainers | foray1010 |
| Keywords | closest, compare, comparison, didyoumean, diff, difference, distance, edit, find, fuzzy, leven, levenshtein, match, matching, similar, similarity, string, suggest, suggestion, text, typescript, word |

## Links

- npm: https://www.npmjs.com/package/didyoumean2
- Repository: https://github.com/foray1010/didyoumean2
- Issues: https://github.com/foray1010/didyoumean2/issues
- npm.io page: https://npm.io/package/didyoumean2

## Dependencies (3)

- [lodash.deburr](https://npm.io/package/lodash.deburr.md) ^4.1.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.24.8
- [fastest-levenshtein](https://npm.io/package/fastest-levenshtein.md) ^1.0.16

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 7.0.4 (latest) — 2024-09-17
- 2.0.0-1 (prerelease) — 2018-10-29
- 7.0.3 — 2024-09-17
- 7.0.2 — 2024-07-21
- 7.0.1 — 2024-07-21
- 7.0.0 — 2024-07-21
- 6.0.1 — 2023-09-07
- 6.0.0 — 2023-09-07
- 5.0.0 — 2021-05-27
- 4.2.0 — 2021-04-25
- 4.1.0 — 2020-06-18
- 4.0.0 — 2020-02-06
- 3.1.2 — 2019-07-29
- 3.1.1 — 2019-06-09
- 3.1.0 — 2019-06-07
- … 13 more at https://npm.io/package/didyoumean2/versions

## README

# didyoumean2

[![Build Status](https://img.shields.io/circleci/project/foray1010/didyoumean2/master.svg)](https://circleci.com/gh/foray1010/didyoumean2/tree/master)
[![codecov.io](https://img.shields.io/codecov/c/github/foray1010/didyoumean2.svg)](https://codecov.io/gh/foray1010/didyoumean2)

[![node](https://img.shields.io/node/v/didyoumean2.svg)](https://www.npmjs.com/package/didyoumean2)
[![npm](https://img.shields.io/npm/dm/didyoumean2.svg)](https://www.npmjs.com/package/didyoumean2)
[![npm](https://img.shields.io/npm/l/didyoumean2.svg)](https://www.npmjs.com/package/didyoumean2)

`didyoumean2` is a library for matching human-quality input to a list of potential matches using the [Levenshtein distance algorithm](https://en.wikipedia.org/wiki/Levenshtein_distance).
It is inspired by [didyoumean.js](https://github.com/dcporter/didyoumean.js).

## Why reinventing the wheel

1. Based on [fastest-levenshtein](https://github.com/ka-weihe/fastest-levenshtein), the fastest JS implementation of the [Levenshtein distance algorithm](https://en.wikipedia.org/wiki/Levenshtein_distance)

1. ~100% faster than [didyoumean.js](https://github.com/dcporter/didyoumean.js)

1. Well tested with 100% coverage

1. Static type checking with [TypeScript](https://github.com/Microsoft/TypeScript)

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

1. Support matching object's `path` instead of just `key`

## Installation

```sh
npm install didyoumean2
```

```js
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](https://nodejs.org/api/corepack.html) to manage the `yarn` version

```bash
corepack enable
```

## Usage

```js
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}`

- default: `true`

- Perform [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) insensitive matching

- Refer to [lodash \_.deburr](https://lodash.com/docs#deburr) for how it works

#### `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](https://ramdajs.com/docs/#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](https://en.wikipedia.org/wiki/Levenshtein_distance), 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:_**

```sh
npm install -g yarn
yarn install
```

Unit test and coverage:

```sh
yarn test
```

Linter:

```sh
yarn lint
```

---
_Source: https://npm.io/package/didyoumean2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
