leven
Measure the difference between two strings using the Levenshtein distance algorithm
Last updated a year ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install leven 
SYNC missed versions from official npm registry.

leven

Measure the difference between two strings using the Levenshtein distance algorithm

Install

npm install leven

Usage

import leven from 'leven';

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

API

leven(first, second, options?)

first

Type: string

First string.

second

Type: string

Second string.

options

Type: object

maxDistance

Type: number

Maximum distance to calculate.

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

import leven from 'leven';

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

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

closestMatch(target, candidates, options?)

Find the closest matching string from an array of candidates.

target

Type: string

The string to find matches for.

candidates

Type: string[]

Array of candidate strings to search through.

options

Type: object

Same options as leven().

maxDistance

Type: number

Maximum distance to consider. Candidates with a distance greater than this value will be ignored.

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

import {closestMatch} from 'leven';

closestMatch('kitten', ['sitting', 'kitchen', 'mittens']);
//=> 'kitchen'

closestMatch('hello', ['jello', 'yellow', 'bellow'], {maxDistance: 2});
//=> 'jello'

// No match within distance threshold
closestMatch('abcdef', ['123456', '1234567890'], {maxDistance: 2});
//=> undefined

Related

Current Tags

  • 4.1.0                                ...           latest (a year ago)

9 Versions

  • 4.1.0                                ...           a year ago
  • 4.0.0                                ...           5 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.1.0                                ...           10 years ago
  • 2.0.0                                ...           11 years ago
  • 1.0.2                                ...           12 years ago
  • 1.0.1                                ...           12 years ago
  • 1.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 29
Today 0
This Week 4
This Month 15
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (11)

© 2010 - cnpmjs.org x YWFE | Home | YWFE