File size: 1,109 Bytes
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * Simple Levenshtein distance implementation (small N, no perf worries)
 */
const levenshtein = (a, b) => {
  const m = a.length;
  const n = b.length;
  const dp = Array.from({
    length: m + 1
  }, () => new Array(n + 1).fill(0));
  for (let i = 0; i <= m; i++) dp[i][0] = i;
  for (let j = 0; j <= n; j++) dp[0][j] = j;
  for (let i = 1; i <= m; i++) {
    for (let j = 1; j <= n; j++) {
      const cost = a[i - 1] === b[j - 1] ? 0 : 1;
      dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost);
    }
  }
  return dp[m][n];
};
/**
 * Return suggestions with minimum distance found, up to distance 2.
 *
 * @internal
 */
export const suggest = (input, candidates) => {
  const distances = candidates.map(c => [levenshtein(input, c), c]).filter(([d]) => d <= 2) // Stricter threshold
  .sort(([a], [b]) => a - b);
  if (distances.length === 0) return [];
  // Only return suggestions with the minimum distance found
  const minDistance = distances[0][0];
  return distances.filter(([d]) => d === minDistance).map(([, c]) => c);
};
//# sourceMappingURL=auto-suggest.js.map