MCPcopy
hub / github.com/vercel/next.js / minDistance

Function minDistance

packages/eslint-plugin-next/src/rules/no-typos.ts:14–41  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

12
13// the minimum number of operations required to convert string a to string b.
14function minDistance(a, b) {
15 const m = a.length
16 const n = b.length
17
18 if (m < n) {
19 return minDistance(b, a)
20 }
21
22 if (n === 0) {
23 return m
24 }
25
26 let previousRow = Array.from({ length: n + 1 }, (_, i) => i)
27
28 for (let i = 0; i < m; i++) {
29 const s1 = a[i]
30 let currentRow = [i + 1]
31 for (let j = 0; j < n; j++) {
32 const s2 = b[j]
33 const insertions = previousRow[j + 1] + 1
34 const deletions = currentRow[j] + 1
35 const substitutions = previousRow[j] + Number(s1 !== s2)
36 currentRow.push(Math.min(insertions, deletions, substitutions))
37 }
38 previousRow = currentRow
39 }
40 return previousRow[previousRow.length - 1]
41}
42
43export default defineRule({
44 meta: {

Callers 1

checkTyposFunction · 0.70

Calls 3

pushMethod · 0.65
fromMethod · 0.45
minMethod · 0.45

Tested by

no test coverage detected