clarity-pattern-parser 4.0.2 → 5.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.
Files changed (132) hide show
  1. package/README.md +176 -1
  2. package/TODO.md +22 -2
  3. package/dist/ast/Node.d.ts +43 -11
  4. package/dist/ast/Visitor.d.ts +31 -31
  5. package/dist/index.browser.js +1248 -1495
  6. package/dist/index.browser.js.map +1 -1
  7. package/dist/index.d.ts +12 -16
  8. package/dist/index.esm.js +1218 -1460
  9. package/dist/index.esm.js.map +1 -1
  10. package/dist/index.js +1217 -1464
  11. package/dist/index.js.map +1 -1
  12. package/dist/patterns/And.d.ts +37 -24
  13. package/dist/patterns/Cursor.d.ts +35 -0
  14. package/dist/patterns/CursorHistory.d.ts +30 -0
  15. package/dist/patterns/Literal.d.ts +36 -19
  16. package/dist/patterns/Not.d.ts +26 -11
  17. package/dist/patterns/Or.d.ts +31 -22
  18. package/dist/patterns/ParseError.d.ts +6 -8
  19. package/dist/patterns/ParseResult.d.ts +6 -0
  20. package/dist/patterns/Pattern.d.ts +17 -26
  21. package/dist/patterns/Reference.d.ts +31 -12
  22. package/dist/patterns/Regex.d.ts +42 -21
  23. package/dist/patterns/Repeat.d.ts +38 -20
  24. package/dist/patterns/clonePatterns.d.ts +2 -0
  25. package/dist/patterns/filterOutNull.d.ts +2 -0
  26. package/dist/patterns/findPattern.d.ts +2 -0
  27. package/dist/patterns/getNextPattern.d.ts +2 -0
  28. package/jest.config.js +2 -1
  29. package/package.json +4 -5
  30. package/rollup.config.js +1 -1
  31. package/src/ast/Node.test.ts +254 -0
  32. package/src/ast/Node.ts +171 -23
  33. package/src/index.ts +11 -19
  34. package/src/intellisense/AutoComplete.test.ts +72 -0
  35. package/src/intellisense/AutoComplete.ts +146 -0
  36. package/src/intellisense/Suggestion.ts +13 -0
  37. package/src/intellisense/SuggestionOption.ts +4 -0
  38. package/src/{tests/cssPatterns → intellisense/css}/cssValue.ts +1 -1
  39. package/src/{tests/cssPatterns → intellisense/css}/divider.ts +2 -1
  40. package/src/intellisense/css/hex.ts +6 -0
  41. package/src/{tests/cssPatterns → intellisense/css}/method.ts +8 -9
  42. package/src/intellisense/css/name.ts +5 -0
  43. package/src/{tests/javascriptPatterns → intellisense/css}/number.ts +3 -3
  44. package/src/intellisense/css/spaces.ts +6 -0
  45. package/src/intellisense/css/unit.ts +10 -0
  46. package/src/{tests/cssPatterns → intellisense/css}/value.ts +1 -1
  47. package/src/{tests/cssPatterns → intellisense/css}/values.ts +1 -1
  48. package/src/intellisense/javascript/Javascript.test.ts +203 -0
  49. package/src/intellisense/javascript/arrayLiteral.ts +25 -0
  50. package/src/intellisense/javascript/deleteStatement.ts +14 -0
  51. package/src/intellisense/javascript/escapedCharacter.ts +50 -0
  52. package/src/intellisense/javascript/exponent.ts +26 -0
  53. package/src/intellisense/javascript/expression.ts +87 -0
  54. package/src/intellisense/javascript/expressionStatement.ts +29 -0
  55. package/src/intellisense/javascript/fraction.ts +13 -0
  56. package/src/intellisense/javascript/infixOperator.ts +36 -0
  57. package/src/intellisense/javascript/integer.ts +7 -0
  58. package/src/intellisense/javascript/invocation.ts +28 -0
  59. package/src/intellisense/javascript/literal.ts +14 -0
  60. package/src/intellisense/javascript/name.ts +3 -0
  61. package/src/intellisense/javascript/numberLiteral.ts +10 -0
  62. package/src/intellisense/javascript/objectLiteral.ts +30 -0
  63. package/src/intellisense/javascript/optionalSpaces.ts +3 -0
  64. package/src/intellisense/javascript/parameters.ts +20 -0
  65. package/src/intellisense/javascript/prefixOperator.ts +13 -0
  66. package/src/intellisense/javascript/propertyAccess.ts +23 -0
  67. package/src/intellisense/javascript/stringLiteral.ts +28 -0
  68. package/src/patterns/And.test.ts +299 -0
  69. package/src/patterns/And.ts +222 -119
  70. package/src/patterns/Cursor.test.ts +93 -0
  71. package/src/patterns/Cursor.ts +130 -0
  72. package/src/patterns/CursorHistory.test.ts +54 -0
  73. package/src/patterns/CursorHistory.ts +95 -0
  74. package/src/patterns/Literal.test.ts +134 -0
  75. package/src/patterns/Literal.ts +151 -61
  76. package/src/patterns/Not.test.ts +88 -0
  77. package/src/patterns/Not.ts +74 -33
  78. package/src/patterns/Or.test.ts +105 -0
  79. package/src/patterns/Or.ts +106 -98
  80. package/src/patterns/ParseError.ts +3 -7
  81. package/src/patterns/ParseResult.ts +7 -0
  82. package/src/patterns/Pattern.ts +18 -150
  83. package/src/patterns/Reference.test.ts +104 -0
  84. package/src/patterns/Reference.ts +94 -94
  85. package/src/patterns/Regex.test.ts +101 -0
  86. package/src/patterns/Regex.ts +129 -60
  87. package/src/patterns/Repeat.test.ts +196 -0
  88. package/src/patterns/Repeat.ts +208 -104
  89. package/src/patterns/clonePatterns.ts +5 -0
  90. package/src/patterns/filterOutNull.ts +13 -0
  91. package/src/patterns/findPattern.ts +25 -0
  92. package/src/patterns/getNextPattern.test.ts +39 -0
  93. package/src/patterns/getNextPattern.ts +18 -0
  94. package/src/Cursor.ts +0 -141
  95. package/src/CursorHistory.ts +0 -146
  96. package/src/TextSuggester.ts +0 -317
  97. package/src/ast/Visitor.ts +0 -271
  98. package/src/patterns/LookAhead.ts +0 -32
  99. package/src/patterns/Recursive.ts +0 -92
  100. package/src/tests/And.test.ts +0 -180
  101. package/src/tests/ComplexExamples.test.ts +0 -86
  102. package/src/tests/CssPatterns.test.ts +0 -90
  103. package/src/tests/CursorHistory.test.ts +0 -107
  104. package/src/tests/Cusor.test.ts +0 -174
  105. package/src/tests/HtmlPatterns.test.ts +0 -34
  106. package/src/tests/Literal.test.ts +0 -79
  107. package/src/tests/LookAhead.test.ts +0 -44
  108. package/src/tests/Not.test.ts +0 -51
  109. package/src/tests/Or.test.ts +0 -113
  110. package/src/tests/Pattern.test.ts +0 -290
  111. package/src/tests/Recursive.test.ts +0 -64
  112. package/src/tests/Reference.test.ts +0 -16
  113. package/src/tests/Repeat.test.ts +0 -75
  114. package/src/tests/SpeedTest.test.ts +0 -31
  115. package/src/tests/TextSuggester.test.ts +0 -297
  116. package/src/tests/Visitor.test.ts +0 -331
  117. package/src/tests/cssPatterns/hex.ts +0 -5
  118. package/src/tests/cssPatterns/name.ts +0 -5
  119. package/src/tests/cssPatterns/number.ts +0 -8
  120. package/src/tests/cssPatterns/spaces.ts +0 -5
  121. package/src/tests/cssPatterns/unit.ts +0 -8
  122. package/src/tests/htmlPatterns/element.ts +0 -49
  123. package/src/tests/javascriptPatterns/boolean.ts +0 -10
  124. package/src/tests/javascriptPatterns/json.ts +0 -67
  125. package/src/tests/javascriptPatterns/name.ts +0 -5
  126. package/src/tests/javascriptPatterns/objectLiteral.ts +0 -40
  127. package/src/tests/javascriptPatterns/string.ts +0 -84
  128. package/src/tests/javascriptPatterns/unit.ts +0 -8
  129. package/src/tests/javascriptPatterns/whitespace.ts +0 -44
  130. package/src/tests/naturalLanguage/filter.ts +0 -37
  131. package/src/tests/patterns/sentence.ts +0 -37
  132. /package/src/{tests/cssPatterns → intellisense/css}/optionalSpaces.ts +0 -0
@@ -1,297 +0,0 @@
1
- /** @jest-environment node */
2
- import TextSuggester from "../TextSuggester";
3
- import sentence from "./patterns/sentence";
4
- import Literal from "../patterns/Literal";
5
- import And from "../patterns/And";
6
- import Or from "../patterns/Or";
7
- import json from "./javascriptPatterns/json";
8
- import Repeat from "../patterns/Repeat";
9
- import Recursive from "../patterns/Recursive";
10
-
11
- function generateFlagFromList(flagNames: string[]) {
12
- return flagNames.map((flagName) => {
13
- return new Literal("flag-name", flagName);
14
- });
15
- }
16
-
17
- function generateExpression(flagNames: string[]) {
18
- const openParen = new Literal("open-paren", "(");
19
- const closeParen = new Literal("close-paren", ")");
20
- const andLiteral = new Literal("and-literal", "AND");
21
- const space = new Literal("space", " ");
22
- const orLiteral = new Literal("or-literal", "OR");
23
- const and = new And("and", [space, andLiteral, space]);
24
- const or = new And("or", [space, orLiteral, space]);
25
-
26
- const booleanOperator = new Or("booleanOperator", [and, or]);
27
- const flag = new Or("flags", generateFlagFromList(flagNames));
28
- const group = new And("group", [
29
- openParen,
30
- new Recursive("flag-expression"),
31
- closeParen,
32
- ]);
33
- const flagOrGroup = new Or("flag-or-group", [flag, group]);
34
- const flagExpression = new Repeat(
35
- "flag-expression",
36
- flagOrGroup,
37
- booleanOperator
38
- );
39
-
40
- return flagExpression;
41
- }
42
-
43
- describe("TextInspector", () => {
44
- test("Partial Match", () => {
45
- const text = "Pat ";
46
- const textInspector = new TextSuggester();
47
-
48
- const inspection = textInspector.suggest(text, sentence);
49
- expect(inspection.options.values.length).toBe(2);
50
- expect(inspection.options.startIndex).toBe(4);
51
- expect(inspection.match?.text).toBe("Pat ");
52
- expect(inspection.error).toBe(null);
53
- expect(inspection.isComplete).toBe(false);
54
- });
55
-
56
- test("Partial Match, with error?.", () => {
57
- const text = "Pat wzoo";
58
- const textInspector = new TextSuggester();
59
-
60
- const inspection = textInspector.suggest(text, sentence);
61
-
62
- expect(inspection.error?.startIndex).toBe(4);
63
- expect(inspection.error?.endIndex).toBe(7);
64
- expect(inspection.error?.text).toBe("wzoo");
65
- expect(inspection.match?.startIndex).toBe(0);
66
- expect(inspection.match?.endIndex).toBe(3);
67
- expect(inspection.match?.text).toBe("Pat ");
68
- expect(inspection.options).toBe(null);
69
- expect(inspection.isComplete).toBe(false);
70
- });
71
-
72
- test("No auto complete so fallback to search.", () => {
73
- const text = "bank";
74
- const textInspector = new TextSuggester();
75
-
76
- const inspection = textInspector.suggest(text, sentence);
77
-
78
- expect(inspection.error?.startIndex).toBe(0);
79
- expect(inspection.error?.endIndex).toBe(3);
80
- expect(inspection.error?.text).toBe("bank");
81
- expect(inspection.options).toBe(null);
82
- expect(inspection.pattern).toBe(null);
83
- expect(inspection.match).toBe(null);
84
- expect(inspection.isComplete).toBe(false);
85
- });
86
-
87
- test("No auto complete so fallback to search with two token.", () => {
88
- const text = "store bank";
89
- const textInspector = new TextSuggester();
90
-
91
- const inspection = textInspector.suggest(text, sentence);
92
-
93
- expect(inspection.error?.startIndex).toBe(0);
94
- expect(inspection.error?.endIndex).toBe(9);
95
- expect(inspection.error?.text).toBe("store bank");
96
- expect(inspection.options).toBe(null);
97
- expect(inspection.pattern).toBe(null);
98
- expect(inspection.match).toBe(null);
99
- expect(inspection.isComplete).toBe(false);
100
- });
101
-
102
- test("Partial Half Match", () => {
103
- const text = "Pat wen";
104
- const textInspector = new TextSuggester();
105
-
106
- const inspection = textInspector.suggest(text, sentence);
107
-
108
- expect(inspection.match?.startIndex).toBe(0);
109
- expect(inspection.match?.endIndex).toBe(6);
110
- expect(inspection.match?.text).toBe("Pat wen");
111
- expect(inspection.options.values.length).toBe(1);
112
- expect(inspection.options.values[0]).toBe("t to");
113
- expect(inspection.isComplete).toBe(false);
114
- expect(inspection.error).toBe(null);
115
- });
116
-
117
- test("Partial Half Match On First Token", () => {
118
- const text = "Pa";
119
- const textInspector = new TextSuggester();
120
-
121
- const inspection = textInspector.suggest(text, sentence);
122
-
123
- expect(inspection.match?.startIndex).toBe(0);
124
- expect(inspection.match?.endIndex).toBe(1);
125
- expect(inspection.match?.text).toBe("Pa");
126
- expect(inspection.options.values.length).toBe(1);
127
- expect(inspection.options.values[0]).toBe("t");
128
- expect(inspection.isComplete).toBe(false);
129
- expect(inspection.error).toBe(null);
130
- });
131
-
132
- test("Empty String", () => {
133
- const text = "";
134
- const textInspector = new TextSuggester();
135
-
136
- const inspection = textInspector.suggest(text, sentence);
137
-
138
- expect(inspection.error).toBe(null);
139
- expect(inspection.options.values.length).toBe(2);
140
- expect(inspection.options.values[0]).toBe("Pat");
141
- expect(inspection.options.values[1]).toBe("Dan");
142
- expect(inspection.pattern).toBe(null);
143
- expect(inspection.match).toBe(null);
144
- expect(inspection.isComplete).toBe(false);
145
- });
146
-
147
- test("Complete Match.", () => {
148
- const text = "Pat went to a big store";
149
- const textInspector = new TextSuggester();
150
-
151
- const inspection = textInspector.suggest(text, sentence);
152
-
153
- expect(inspection.error).toBe(null);
154
- expect(inspection.options).toBe(null);
155
- expect(inspection.match?.startIndex).toBe(0);
156
- expect(inspection.match?.endIndex).toBe(22);
157
- expect(inspection.match?.text).toBe("Pat went to a big store");
158
- expect(inspection.isComplete).toBe(true);
159
- });
160
-
161
- test("static inspect.", () => {
162
- const text = "Pat went to a big store";
163
- const inspection = TextSuggester.suggest(text, sentence);
164
-
165
- expect(inspection.error).toBe(null);
166
- expect(inspection.options).toBe(null);
167
- expect(inspection.match?.startIndex).toBe(0);
168
- expect(inspection.match?.endIndex).toBe(22);
169
- expect(inspection.match?.text).toBe("Pat went to a big store");
170
- expect(inspection.isComplete).toBe(true);
171
- });
172
-
173
- test("static inspect.", () => {
174
- const show = new Literal("show", "Show");
175
- const me = new Literal("me", "me");
176
- const theMoney = new Literal("the-money", "the money");
177
- const yourLicense = new Literal("your-license", "your license");
178
- const space = new Literal("space", " ");
179
- const first = new And("first", [show, space, me, space, theMoney]);
180
- const second = new And("second", [show, space, me, space, yourLicense]);
181
- const either = new Or("either", [first, second]);
182
-
183
- const inspection = TextSuggester.suggest("Show me ", either);
184
-
185
- expect(inspection.error).toBe(null);
186
- expect(inspection.options.startIndex).toBe(8);
187
- expect(inspection.options.values.length).toBe(2);
188
- expect(inspection.options.values[0]).toBe("the money");
189
- expect(inspection.options.values[1]).toBe("your license");
190
- expect(inspection.match?.startIndex).toBe(0);
191
- expect(inspection.match?.endIndex).toBe(7);
192
- expect(inspection.match?.text).toBe("Show me ");
193
- expect(inspection.isComplete).toBe(false);
194
- });
195
-
196
- test("json inspect.", () => {
197
- let inspection = TextSuggester.suggest("{", json);
198
-
199
- inspection = TextSuggester.suggest(`{"blah":`, json);
200
-
201
- expect(inspection.error).toBe(null);
202
- expect(inspection.options.startIndex).toBe(8);
203
- expect(inspection.options.values.length).toBe(9);
204
- expect(inspection.options.values[0]).toBe(" ");
205
- expect(inspection.options.values[1]).toBe("number");
206
- expect(inspection.options.values[2]).toBe("'");
207
- expect(inspection.options.values[3]).toBe('"');
208
- expect(inspection.options.values[4]).toBe("true");
209
- expect(inspection.options.values[5]).toBe("false");
210
- expect(inspection.options.values[6]).toBe("null");
211
- expect(inspection.options.values[7]).toBe("{");
212
- expect(inspection.options.values[8]).toBe("[");
213
- expect(inspection.match?.startIndex).toBe(0);
214
- expect(inspection.match?.endIndex).toBe(7);
215
- expect(inspection.match?.text).toBe(`{"blah":`);
216
- expect(inspection.isComplete).toBe(false);
217
-
218
- inspection = TextSuggester.suggest(`{"blah":{`, json);
219
-
220
- expect(inspection.error).toBe(null);
221
- expect(inspection.options.startIndex).toBe(9);
222
- expect(inspection.options.values.length).toBe(4);
223
- expect(inspection.options.values[0]).toBe(" ");
224
- expect(inspection.options.values[1]).toBe("'");
225
- expect(inspection.options.values[2]).toBe('"');
226
- expect(inspection.options.values[3]).toBe("}");
227
- expect(inspection.match?.startIndex).toBe(0);
228
- expect(inspection.match?.endIndex).toBe(8);
229
- expect(inspection.match?.text).toBe(`{"blah":{`);
230
- expect(inspection.isComplete).toBe(false);
231
-
232
- inspection = TextSuggester.suggest(`{"blah":0.9`, json);
233
-
234
- expect(inspection.error).toBe(null);
235
- expect(inspection.options.startIndex).toBe(11);
236
- expect(inspection.options.values.length).toBe(3);
237
- expect(inspection.options.values[0]).toBe(" ");
238
- expect(inspection.options.values[1]).toBe(",");
239
- expect(inspection.options.values[2]).toBe("}");
240
- expect(inspection.match?.startIndex).toBe(0);
241
- expect(inspection.match?.endIndex).toBe(10);
242
- expect(inspection.match?.text).toBe(`{"blah":0.9`);
243
- expect(inspection.isComplete).toBe(false);
244
- });
245
-
246
- test("Suggest another item in the repeat.", () => {
247
- const a = new Literal("a", "A");
248
- const b = new Literal("b", "B");
249
- const space = new Literal("space", " ");
250
- const or = new Or("names", [a, b]);
251
-
252
- const repeat = new Repeat("repeat", or, space);
253
-
254
- const result = TextSuggester.suggest("A B ", repeat);
255
-
256
- expect(result.isComplete).toBe(false);
257
- expect(result.options.values[0]).toBe("A");
258
- expect(result.options.values[1]).toBe("B");
259
- expect(result.options.values.length).toBe(2);
260
- });
261
-
262
- test("Suggest another item when its complete but is on a repeat.", () => {
263
- const a = new Literal("a", "A");
264
- const b = new Literal("b", "B");
265
- const space = new Literal("space", " ");
266
- const or = new Or("names", [a, b]);
267
-
268
- const repeat = new Repeat("repeat", or, space);
269
-
270
- const result = TextSuggester.suggest("A B", repeat);
271
-
272
- expect(result.isComplete).toBe(true);
273
- expect(result.options.values.length).toBe(1);
274
- expect(result.options.values[0]).toBe(" ");
275
- });
276
-
277
- test("Repeating pattern.", () => {
278
- const expression = generateExpression(["FlagX", "FlagY", "FlagZ"]);
279
- const result = TextSuggester.suggest("(FlagX AND ", expression);
280
-
281
- expect(result.options.values.length).toBe(4);
282
- expect(result.options.values[0]).toBe("FlagX");
283
- expect(result.options.values[1]).toBe("FlagY");
284
- expect(result.options.values[2]).toBe("FlagZ");
285
- expect(result.options.values[3]).toBe("(");
286
- });
287
-
288
- test("Do not match on first partial token.", () => {
289
- const expression = generateExpression(["FlagX", "FlagY", "FlagZ"]);
290
- const result = TextSuggester.suggest("l", expression);
291
-
292
- expect(result.options).toBe(null);
293
- expect(result.error?.startIndex).toBe(0);
294
- expect(result.error?.endIndex).toBe(0);
295
- expect(result.error?.text).toBe("l");
296
- });
297
- });
@@ -1,331 +0,0 @@
1
- /** @jest-environment node */
2
- import { Visitor, Node } from "../index";
3
- import cssValue from "./cssPatterns/cssValue";
4
-
5
- function createContext(amount: number = 10) {
6
- const grandParent = new Node("grand-parent", "grand-parent", 0, 0);
7
- const parent = new Node("parent", "parent", 0, 0);
8
- const children: Node[] = [];
9
-
10
- for (let x = 0; x < amount; x++) {
11
- const child = new Node("child", "child", 0, 0, [], x.toString());
12
- children.push(child);
13
- }
14
-
15
- parent.children = children;
16
- grandParent.children.push(parent);
17
-
18
- return grandParent;
19
- }
20
-
21
- describe("Visitor", () => {
22
- test("flatten", () => {
23
- const node = cssValue.exec(
24
- "linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%), linear-gradient(to bottom, #555, #555 50%, #eee 75%, #555 75%)"
25
- );
26
-
27
- if (node != null) {
28
- const visitor = new Visitor(node);
29
- visitor.selectRoot().flatten();
30
-
31
- expect(node.children.length).toBe(47);
32
- }
33
- });
34
-
35
- test("remove", () => {
36
- const node = cssValue.exec(
37
- "linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%), linear-gradient(to bottom, #555, #555 50%, #eee 75%, #555 75%)"
38
- );
39
-
40
- if (node != null) {
41
- const visitor = new Visitor(node);
42
- visitor
43
- .selectRoot()
44
- .flatten()
45
- .select((node) => {
46
- return node.name === "spaces" || node.value === " ";
47
- })
48
- .remove();
49
-
50
- expect(node.children.length).toBe(39);
51
- }
52
- });
53
-
54
- test("wrap and unwrap", () => {
55
- const node = createContext();
56
-
57
- if (node != null) {
58
- const visitor = new Visitor(node);
59
- const stringResult = visitor
60
- .select((n) => n.name === "child")
61
- .wrap((node) => {
62
- const wrapper = new Node("wrapper", "wrapper", 0, 0);
63
- wrapper.children.push(node);
64
- return wrapper;
65
- });
66
-
67
- let wrappers = Visitor.select(
68
- node,
69
- (n) => n.type === "wrapper"
70
- ).selectedNodes;
71
- let children = Visitor.select(
72
- node,
73
- (n) => n.type === "child"
74
- ).selectedNodes;
75
-
76
- expect(wrappers.length).toBe(10);
77
- expect(children.length).toBe(10);
78
-
79
- visitor.select((n) => n.type === "child").unwrap();
80
-
81
- wrappers = visitor.select((n) => n.type === "wrapper").selectedNodes;
82
- children = visitor.select((n) => n.type === "child").selectedNodes;
83
-
84
- expect(wrappers.length).toBe(0);
85
- expect(children.length).toBe(10);
86
- }
87
- });
88
-
89
- test("prepend", () => {
90
- const node = createContext();
91
-
92
- if (node != null) {
93
- const visitor = new Visitor(node);
94
- visitor
95
- .select((n) => n.type === "child")
96
- .prepend((n) => {
97
- const sibling = new Node(
98
- "sibling",
99
- "sibling",
100
- 0,
101
- 0,
102
- [],
103
- n.value || ""
104
- );
105
-
106
- return sibling;
107
- });
108
-
109
- const siblings = visitor.select(
110
- (n) => n.type === "sibling"
111
- ).selectedNodes;
112
- const parent = visitor.select((n) => n.type === "parent")
113
- .selectedNodes[0];
114
-
115
- const children = parent.children;
116
-
117
- expect(siblings.length).toBe(10);
118
-
119
- expect(children[0]?.type).toBe("sibling");
120
- expect(children[0]?.value).toBe("0");
121
-
122
- expect(children[1]?.type).toBe("child");
123
- expect(children[1]?.value).toBe("0");
124
-
125
- expect(children[2]?.type).toBe("sibling");
126
- expect(children[2]?.value).toBe("1");
127
-
128
- expect(children[3]?.type).toBe("child");
129
- expect(children[3]?.value).toBe("1");
130
-
131
- expect(children[4]?.type).toBe("sibling");
132
- expect(children[4]?.value).toBe("2");
133
-
134
- expect(children[5]?.type).toBe("child");
135
- expect(children[5]?.value).toBe("2");
136
-
137
- expect(children[6]?.type).toBe("sibling");
138
- expect(children[6]?.value).toBe("3");
139
-
140
- expect(children[7]?.type).toBe("child");
141
- expect(children[7]?.value).toBe("3");
142
-
143
- expect(children[8]?.type).toBe("sibling");
144
- expect(children[8]?.value).toBe("4");
145
-
146
- expect(children[9]?.type).toBe("child");
147
- expect(children[9]?.value).toBe("4");
148
-
149
- expect(children[10]?.type).toBe("sibling");
150
- expect(children[10]?.value).toBe("5");
151
-
152
- expect(children[11]?.type).toBe("child");
153
- expect(children[11]?.value).toBe("5");
154
-
155
- expect(children[12]?.type).toBe("sibling");
156
- expect(children[12]?.value).toBe("6");
157
-
158
- expect(children[13]?.type).toBe("child");
159
- expect(children[13]?.value).toBe("6");
160
-
161
- expect(children[14]?.type).toBe("sibling");
162
- expect(children[14]?.value).toBe("7");
163
-
164
- expect(children[15]?.type).toBe("child");
165
- expect(children[15]?.value).toBe("7");
166
-
167
- expect(children[16]?.type).toBe("sibling");
168
- expect(children[16]?.value).toBe("8");
169
-
170
- expect(children[17]?.type).toBe("child");
171
- expect(children[17]?.value).toBe("8");
172
-
173
- expect(children[18]?.type).toBe("sibling");
174
- expect(children[18]?.value).toBe("9");
175
-
176
- expect(children[19]?.type).toBe("child");
177
- expect(children[19]?.value).toBe("9");
178
- }
179
- });
180
-
181
- test("append", () => {
182
- const node = createContext();
183
-
184
- if (node != null) {
185
- const visitor = new Visitor(node);
186
- visitor
187
- .select((n) => n.type === "child")
188
- .append((n) => {
189
- const sibling = new Node(
190
- "sibling",
191
- "sibling",
192
- 0,
193
- 0,
194
- [],
195
- n.value || ""
196
- );
197
-
198
- return sibling;
199
- });
200
-
201
- const siblings = visitor.select(
202
- (n) => n.type === "sibling"
203
- ).selectedNodes;
204
- const parent = visitor.select((n) => n.type === "parent")
205
- .selectedNodes[0];
206
-
207
- const children = parent.children;
208
-
209
- expect(siblings.length).toBe(10);
210
-
211
- expect(children[0]?.type).toBe("child");
212
- expect(children[0]?.value).toBe("0");
213
-
214
- expect(children[1]?.type).toBe("sibling");
215
- expect(children[1]?.value).toBe("0");
216
-
217
- expect(children[2]?.type).toBe("child");
218
- expect(children[2]?.value).toBe("1");
219
-
220
- expect(children[3]?.type).toBe("sibling");
221
- expect(children[3]?.value).toBe("1");
222
-
223
- expect(children[4]?.type).toBe("child");
224
- expect(children[4]?.value).toBe("2");
225
-
226
- expect(children[5]?.type).toBe("sibling");
227
- expect(children[5]?.value).toBe("2");
228
-
229
- expect(children[6]?.type).toBe("child");
230
- expect(children[6]?.value).toBe("3");
231
-
232
- expect(children[7]?.type).toBe("sibling");
233
- expect(children[7]?.value).toBe("3");
234
-
235
- expect(children[8]?.type).toBe("child");
236
- expect(children[8]?.value).toBe("4");
237
-
238
- expect(children[9]?.type).toBe("sibling");
239
- expect(children[9]?.value).toBe("4");
240
-
241
- expect(children[10]?.type).toBe("child");
242
- expect(children[10]?.value).toBe("5");
243
-
244
- expect(children[11]?.type).toBe("sibling");
245
- expect(children[11]?.value).toBe("5");
246
-
247
- expect(children[12]?.type).toBe("child");
248
- expect(children[12]?.value).toBe("6");
249
-
250
- expect(children[13]?.type).toBe("sibling");
251
- expect(children[13]?.value).toBe("6");
252
-
253
- expect(children[14]?.type).toBe("child");
254
- expect(children[14]?.value).toBe("7");
255
-
256
- expect(children[15]?.type).toBe("sibling");
257
- expect(children[15]?.value).toBe("7");
258
-
259
- expect(children[16]?.type).toBe("child");
260
- expect(children[16]?.value).toBe("8");
261
-
262
- expect(children[17]?.type).toBe("sibling");
263
- expect(children[17]?.value).toBe("8");
264
-
265
- expect(children[18]?.type).toBe("child");
266
- expect(children[18]?.value).toBe("9");
267
-
268
- expect(children[19]?.type).toBe("sibling");
269
- expect(children[19]?.value).toBe("9");
270
- }
271
- });
272
-
273
- test("selectAll", () => {
274
- const node = createContext();
275
-
276
- if (node != null) {
277
- const visitor = new Visitor(node);
278
- const all = visitor.selectAll().selectedNodes;
279
-
280
- expect(all.length).toBe(12);
281
- expect(all[0]?.type).toBe("grand-parent");
282
- expect(all[1]?.type).toBe("parent");
283
- expect(all[2]?.type).toBe("child");
284
- expect(all[3]?.type).toBe("child");
285
- expect(all[4]?.type).toBe("child");
286
- expect(all[5]?.type).toBe("child");
287
- expect(all[6]?.type).toBe("child");
288
- expect(all[7]?.type).toBe("child");
289
- expect(all[8]?.type).toBe("child");
290
- expect(all[9]?.type).toBe("child");
291
- }
292
- });
293
-
294
- test("selectNode", () => {
295
- const node = createContext();
296
-
297
- if (node != null) {
298
- const visitor = new Visitor(node);
299
- const selected = visitor.selectNode(node).selectedNodes;
300
-
301
- expect(selected.length).toBe(1);
302
- expect(selected[0]?.type).toBe("grand-parent");
303
- }
304
- });
305
-
306
- test("selectNode then deselectNode", () => {
307
- const node = createContext();
308
-
309
- if (node != null) {
310
- const visitor = new Visitor(node);
311
- const selected = visitor
312
- .selectNode(node)
313
- .deselectNode(node).selectedNodes;
314
-
315
- expect(selected.length).toBe(0);
316
- }
317
- });
318
-
319
- test("forEach", () => {
320
- const node = createContext();
321
- let count = 0;
322
-
323
- if (node != null) {
324
- const visitor = new Visitor(node);
325
- const selected = visitor.selectAll().forEach(() => count++).selectedNodes;
326
-
327
- expect(selected.length).toBe(12);
328
- expect(count).toBe(12);
329
- }
330
- });
331
- });
@@ -1,5 +0,0 @@
1
- import { Regex } from "../../index";
2
-
3
- const hex = new Regex("#______", "#[0-9a-fA-F]{6}|#[0-9a-fA-F]{3}");
4
-
5
- export default hex;
@@ -1,5 +0,0 @@
1
- import { Regex } from "../../index";
2
-
3
- const name = new Regex("name", "[a-zA-Z]+[a-zA-Z0-9_-]*");
4
-
5
- export default name;
@@ -1,8 +0,0 @@
1
- import { Regex } from "../../index";
2
-
3
- const number = new Regex(
4
- "[Number]",
5
- "[-+]?[0-9]*[.]?[0-9]+([eE][-+]?[0-9]+)?"
6
- );
7
-
8
- export default number;
@@ -1,5 +0,0 @@
1
- import { Regex } from "../../index";
2
-
3
- const spaces = new Regex(" ", "\\s+");
4
-
5
- export default spaces;
@@ -1,8 +0,0 @@
1
- import { And, Regex } from "../../index";
2
-
3
- import number from "./number";
4
-
5
- const unitType = new Regex("[unit]", "[a-zA-Z%]+");
6
- const unit = new And("unit", [number, unitType]);
7
-
8
- export default unit;