leven

  • Version 4.1.0
  • Published
  • 10.2 kB
  • No dependencies
  • MIT license

Install

npm i leven
yarn add leven
pnpm add leven

Overview

Measure the difference between two strings using the Levenshtein distance algorithm

Index

Functions

function closestMatch

closestMatch: (
target: string,
candidates: readonly string[],
options?: Options
) => string | undefined;
  • Find the closest matching string from an array of candidates.

    Parameter target

    The string to find matches for.

    Parameter candidates

    Array of candidate strings to search through.

    Parameter options

    Options.

    Returns

    The closest matching string from candidates, or undefined if no candidates are provided or if no match is found within maxDistance.

    Example 1

    import {closestMatch} from 'leven';
    closestMatch('kitten', ['sitting', 'kitchen', 'mittens']);
    //=> 'kitchen'
    closestMatch('hello', ['jello', 'yellow', 'bellow'], {maxDistance: 2});
    //=> 'jello'

function leven

leven: (first: string, second: string, options?: Options) => number;
  • Measure the difference between two strings using the Levenshtein distance algorithm.

    Parameter first

    First string.

    Parameter second

    Second string.

    Parameter options

    Options.

    Returns

    Distance between first and second. If maxDistance is provided and the actual distance exceeds it, returns maxDistance.

    Example 1

    import leven from 'leven';
    leven('cat', 'cow');
    //=> 2

Interfaces

interface Options

interface Options {}

    property maxDistance

    readonly maxDistance?: number;
    • Maximum Levenshtein distance to calculate.

      If the actual distance exceeds this value, the function will return the maximum distance instead of the actual distance. This can significantly improve performance when you only care about matches within a certain threshold.

      Example 1

      ``` import leven from 'leven';

      leven('abcdef', '123456', {maxDistance: 3}); //=> 3

      leven('cat', 'cow', {maxDistance: 5}); //=> 2 ```

    Package Files (1)

    Dependencies (0)

    No dependencies.

    Dev Dependencies (11)

    Peer Dependencies (0)

    No peer dependencies.

    Badge

    To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

    You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/leven.

    • Markdown
      [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/leven)
    • HTML
      <a href="https://www.jsdocs.io/package/leven"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>