leven

中文文档
4.1.0发布于 2025/09/11MITTypeScriptES Module零依赖近 30 天下载 2.7亿

Measure the difference between two strings using the Levenshtein distance algorithm

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. API
  4. leven(first, second, options?)
  5. closestMatch(target, candidates, options?)
  6. Related

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

版本与发布记录

leven 共发布 9 个版本,首次发布于 2014/08/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.0latest2025/09/111.1 年前
4.0.02021/08/105 年前
3.1.02019/04/047 年前
3.0.02019/03/127 年前
2.1.02017/02/189 年前
2.0.02015/09/0711 年前

关于 leven 的常见问题

leven 是什么?

Measure the difference between two strings using the Levenshtein distance algorithm

如何安装 leven?

在项目目录运行 npm install leven,或使用 pnpm add / yarn add / bun add。

leven 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

leven 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

leven 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^12.20.0 || ^14.13.1 || >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

leven 可以商用吗?

leven 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

leven 的下载量有多少?

根据 npm 官方下载统计,leven 在 2026/10/05 – 2026/11/03 这 30 天内被下载 266,845,444 次,日均约 8,894,848 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

leven 还在维护吗?

最新版本 4.1.0 发布于 2025/09/11(1.1 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2014/08/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。