@weborigami/language 0.0.72 → 0.1.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,71 @@
1
+ /**
2
+ * Returns true if the two strings have a Damerau-Levenshtein distance of 1.
3
+ * This will be true if the strings differ by a single insertion, deletion,
4
+ * substitution, or transposition.
5
+ *
6
+ * @param {string} s1
7
+ * @param {string} s2
8
+ */
9
+ export function isTypo(s1, s2) {
10
+ const length1 = s1.length;
11
+ const length2 = s2.length;
12
+
13
+ // If the strings are identical, distance is 0
14
+ if (s1 === s2) {
15
+ return false;
16
+ }
17
+
18
+ // If length difference is more than 1, distance can't be 1
19
+ if (Math.abs(length1 - length2) > 1) {
20
+ return false;
21
+ }
22
+
23
+ if (length1 === length2) {
24
+ // Check for one substitution
25
+ let differences = 0;
26
+ for (let i = 0; i < length1; i++) {
27
+ if (s1[i] !== s2[i]) {
28
+ differences++;
29
+ if (differences > 1) {
30
+ break;
31
+ }
32
+ }
33
+ }
34
+ if (differences === 1) {
35
+ return true;
36
+ }
37
+
38
+ // Check for one transposition
39
+ for (let i = 0; i < length1 - 1; i++) {
40
+ if (s1[i] !== s2[i]) {
41
+ // Check if swapping s1[i] and s1[i+1] matches s2
42
+ if (s1[i] === s2[i + 1] && s1[i + 1] === s2[i]) {
43
+ return s1.slice(i + 2) === s2.slice(i + 2);
44
+ } else {
45
+ return false;
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ // Check for one insertion/deletion
52
+ const longer = length1 > length2 ? s1 : s2;
53
+ const shorter = length1 > length2 ? s2 : s1;
54
+ for (let i = 0; i < shorter.length; i++) {
55
+ if (shorter[i] !== longer[i]) {
56
+ // If we skip this character, do the rest match?
57
+ return shorter.slice(i) === longer.slice(i + 1);
58
+ }
59
+ }
60
+ return shorter === longer.slice(0, shorter.length);
61
+ }
62
+
63
+ /**
64
+ * Return any strings that could be a typo of s
65
+ *
66
+ * @param {string} s
67
+ * @param {string[]} strings
68
+ */
69
+ export function typos(s, strings) {
70
+ return strings.filter((str) => isTypo(s, str));
71
+ }
@@ -77,11 +77,11 @@ describe("compile", () => {
77
77
  return strings[0] + values[0] + strings[1];
78
78
  },
79
79
  });
80
- const fn = compile.expression("=tag`Hello, ${_}!`");
81
- const templateFn = await fn.call(null);
82
- const alice = await templateFn.call(scope, "Alice");
80
+ const program = compile.expression("=tag`Hello, ${_}!`");
81
+ const lambda = await program.call(scope);
82
+ const alice = await lambda("Alice");
83
83
  assert.equal(alice, "Hello, Alice!");
84
- const bob = await templateFn.call(scope, "Bob");
84
+ const bob = await lambda("Bob");
85
85
  assert.equal(bob, "Hello, Bob!");
86
86
  });
87
87