MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / levenshtein

Function levenshtein

src/utils/fuzzy.js:1–14  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

1export const levenshtein = (a, b) => {
2 const m = a.length,
3 n = b.length;
4 const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
5 for (let i = 0; i <= m; i++) dp[i][0] = i;
6 for (let j = 0; j <= n; j++) dp[0][j] = j;
7 for (let i = 1; i <= m; i++) {
8 for (let j = 1; j <= n; j++) {
9 dp[i][j] =
10 a[i - 1] === b[j - 1] ? dp[i - 1][j - 1] : Math.min(dp[i - 1][j - 1] + 1, dp[i][j - 1] + 1, dp[i - 1][j] + 1);
11 }
12 }
13 return dp[m][n];
14};
15
16export const fuzzyMatch = (candidate, query) => {
17 const lowerCandidate = (candidate || '').toLowerCase();

Callers 1

fuzzyMatchFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected