Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

leven

> 编程语言
Open source

Measure the difference between two strings with the fastest JS implementation of the Levenshtein distance algorithm

734 stars0 likes3 views
WebsiteGitHub

About

Measure the difference between two strings with the fastest JS implementation of the Levenshtein distance algorithm

leven

Measure the difference between two strings using the Levenshtein distance algorithm

Install

bash
npm install leven

Usage

javascript
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.

javascript
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.

javascript
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

  • leven-cli - CLI for this module

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言