@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.
- package/main.js +2 -2
- package/package.json +3 -3
- package/src/compiler/compile.js +28 -11
- package/src/compiler/origami.pegjs +127 -90
- package/src/compiler/parse.js +686 -688
- package/src/compiler/parserHelpers.js +13 -7
- package/src/runtime/HandleExtensionsTransform.js +1 -1
- package/src/runtime/ImportModulesMixin.js +1 -1
- package/src/runtime/codeFragment.js +2 -2
- package/src/runtime/errors.js +104 -0
- package/src/runtime/evaluate.js +3 -3
- package/src/runtime/expressionObject.js +8 -5
- package/src/runtime/{extensions.js → handlers.js} +6 -24
- package/src/runtime/internal.js +1 -0
- package/src/runtime/ops.js +59 -170
- package/src/runtime/typos.js +71 -0
- package/test/compiler/compile.test.js +4 -4
- package/test/compiler/parse.test.js +273 -145
- package/test/runtime/fixtures/templates/greet.orit +1 -1
- package/test/runtime/{extensions.test.js → handlers.test.js} +2 -2
- package/test/runtime/ops.test.js +17 -12
- package/test/runtime/typos.test.js +21 -0
- package/src/runtime/formatError.js +0 -56
|
@@ -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
|
|
81
|
-
const
|
|
82
|
-
const alice = await
|
|
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
|
|
84
|
+
const bob = await lambda("Bob");
|
|
85
85
|
assert.equal(bob, "Hello, Bob!");
|
|
86
86
|
});
|
|
87
87
|
|