npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tingxin_cy/edit-distance

v1.0.0

Published

字符串最短编辑距离计算

Downloads

3

Readme

edit-distance

编辑距离(Edit Distance),又称 Levenshtein 距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。 许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。 一般来说,编辑距离越小,两个串的相似度越大。

特性

  • 支持计算最短编辑距离。
  • 支持计算最短编辑路径,基于编辑路径可以实现对文本进行 diff 高亮渲染。

安装

npm install @tingxin_cy/edit-distance -S

or

yarn add @tingxin_cy/edit-distance -S

Function:editDistance

import editDistance from '@tingxin_cy/edit-distance';

const a = 'AABCDE';
const b = 'AB3DEG';
const { distance, path } = editDistance(a, b);

/*
  distance = 3;
  path = [
    {
      type: "del",
      aChar: "A",
      bChar: "",
      aIndex: 1,
      bIndex: 1
    },
    {
      type: "sub",
      aChar: "C",
      bChar: "3",
      aIndex: 3,
      bIndex: 2
    },
    {
      type: "ins",
      aChar: "",
      bChar: "G",
      aIndex: 6,
      bIndex: 5
    }
  ]

  - a文本中 index = 1 的字符 A 被执行了删除操作
  - a文本中 index = 3 的字符 C 被替换为 b文本中 index = 2 的字符 3
  - a文本中 index = 6 的位置插入了字符 G,对应在b文本中 index = 5 
*/

参数说明

| 参数 | 说明 | 类型 | | ---- | ---------------- | ------ | | a | 文本 a,原始文本 | string | | b | 文本 b,目标文本 | string |

结果说明:{ distance, path }

| 参数 | 说明 | 类型 | | -------- | ------------------------------------ | ------ | | distance | 最短编辑距离 | number | | path | 编辑路径,对应最短编辑距离的操作路径 | path[] |

path

| 参数 | 说明 | 类型 | | ------ | ---------------------------------------------------------- | ------------------- | | type | 操作类型,插入字符(ins),删除字符(del),替换字符(sub) | 'ins'|'del'|'sub' | | aChar | a 文本中的原始字符 | string | | bChar | b 文本中的目标字符 | string | | aIndex | a 文本中原始字符的坐标 | number | | bIndex | b 文本中原始字符的坐标 | number |