clarity-pattern-parser 8.3.1 → 8.4.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/TODO.md CHANGED
@@ -1 +1,15 @@
1
- * Need to Rename Repeat.ts to InfiniteRepeat and them make a new class that is Repeat.ts that is a fascade to FiniteRepeat and InfiniteRepeat, so its easier to use.
1
+ * We should make a Block, Delimiter Pattern. These will be breadth first patterns. It look something like this.
2
+
3
+ ```ts
4
+ new Block(startPattern,contentPattern,endPattern);
5
+ ```
6
+
7
+ ```
8
+ block = [start-pattern content-pattern end-pattern];
9
+ ```
10
+
11
+ It will count the startPattern and only be done when its ends equal to end Pattern. It will store the internal pattern needed for its content. This is what is lazily done at some point in the future.
12
+
13
+ ```ts
14
+ new Delimiter(pattern, segmentPattern);
15
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "8.3.1",
3
+ "version": "8.4.0",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -188,7 +188,7 @@ describe("Node", () => {
188
188
  const b = new Node("b", "b", 0, 0, [], "B");
189
189
  const parent = new Node("parent", "parent", 0, 0, [a, b]);
190
190
 
191
- parent.find(p=>p.name === "b")?.remove();
191
+ parent.find(p => p.name === "b")?.remove();
192
192
 
193
193
  expect(parent.children.length).toBe(1);
194
194
  expect(parent.value).toBe("A")
@@ -382,4 +382,30 @@ describe("Node", () => {
382
382
  expect(result).toBeNull()
383
383
  });
384
384
 
385
+ test("Normalize values and index", () => {
386
+ const firstChild = new Node("literal", "first", 0, 0, [], "first");
387
+ const secondChild = new Node("literal", "second", 0, 0, [], "second");
388
+ const parent = new Node("literal", "parent", 0, 0);
389
+
390
+ parent.appendChild(firstChild);
391
+ parent.appendChild(secondChild);
392
+
393
+ parent.normalize();
394
+
395
+ expect(parent.startIndex).toBe(0);
396
+ expect(parent.firstIndex).toBe(0);
397
+ expect(parent.lastIndex).toBe(10);
398
+ expect(parent.endIndex).toBe(11);
399
+ });
400
+
401
+ test("Normalize values with no values", () => {
402
+ const node = new Node("literal", "node", 0, 0, [], "");
403
+ node.normalize();
404
+
405
+ expect(node.startIndex).toBe(0);
406
+ expect(node.firstIndex).toBe(0);
407
+ expect(node.lastIndex).toBe(0);
408
+ expect(node.endIndex).toBe(1);
409
+ });
410
+
385
411
  });
package/src/ast/Node.ts CHANGED
@@ -232,6 +232,20 @@ export class Node {
232
232
  );
233
233
  }
234
234
 
235
+ normalize(startIndex = this._firstIndex): number {
236
+ let length = 0;
237
+
238
+ if (this.children.length === 0) {
239
+ length = this._value.length;
240
+ } else {
241
+ length = this.children.reduce((acc, c) => acc + c.normalize(acc+startIndex), startIndex);
242
+ }
243
+
244
+ this._firstIndex = startIndex;
245
+ this._lastIndex = Math.max(startIndex + length - 1, 0);
246
+ return length;
247
+ }
248
+
235
249
  toString(): string {
236
250
  if (this._children.length === 0) {
237
251
  return this._value;