@lutrin/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The "did you mean …?" suggestion (edit distance) — shared by deck
3
+ * validation (layouts, directives, animate) and by the validation of theme /
4
+ * user layout files (theme.mjs, layout.mjs).
5
+ */
6
+
7
+ export function editDistance(a, b) {
8
+ const dp = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
9
+ for (let j = 1; j <= b.length; j++) dp[0][j] = j;
10
+ for (let i = 1; i <= a.length; i++) {
11
+ for (let j = 1; j <= b.length; j++) {
12
+ dp[i][j] = Math.min(
13
+ dp[i - 1][j] + 1,
14
+ dp[i][j - 1] + 1,
15
+ dp[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
16
+ );
17
+ }
18
+ }
19
+ return dp[a.length][b.length];
20
+ }
21
+
22
+ export function closest(name, candidates) {
23
+ let best = null;
24
+ let bestD = Number.POSITIVE_INFINITY;
25
+ for (const c of candidates) {
26
+ const d = editDistance(name.toLowerCase(), c.toLowerCase());
27
+ if (d < bestD) {
28
+ bestD = d;
29
+ best = c;
30
+ }
31
+ }
32
+ return bestD <= 2 ? best : null;
33
+ }