treenode.ts 0.5.0 → 0.7.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.
@@ -3,12 +3,17 @@ import { IParseable } from "./IParseable";
3
3
  * Search callback passed to `.pre`, `.post`, and `.breadth`.
4
4
  * @public
5
5
  */
6
- export declare type SearchCallback<T> = (node: TreeNode<T>) => boolean | void;
6
+ export type SearchCallback<T> = (node: TreeNode<T>) => boolean | void;
7
7
  /**
8
8
  * Search methods on TreeNode, passed to `.flatten`.
9
9
  * @public
10
10
  */
11
- export declare type SearchStrategy = "pre" | "post" | "breadth";
11
+ export type SearchStrategy = "pre" | "post" | "breadth";
12
+ /**
13
+ * Nested array representation of a tree.
14
+ * @public
15
+ */
16
+ export type NestedArray<T> = T | [T, ...(NestedArray<T> | T[])[]];
12
17
  /**
13
18
  * @public
14
19
  */
@@ -16,6 +21,7 @@ export declare class TreeNode<T> {
16
21
  model: T;
17
22
  parent: TreeNode<T> | null;
18
23
  children: TreeNode<T>[];
24
+ private _index;
19
25
  constructor(model: T, parent?: TreeNode<T> | null, children?: TreeNode<T>[]);
20
26
  /**
21
27
  * Parses object into a tree and returns the root node.
@@ -29,10 +35,35 @@ export declare class TreeNode<T> {
29
35
  * Indices from the root to the node.
30
36
  */
31
37
  get indices(): number[];
38
+ /**
39
+ * Compressed path key: positive = run of first-children, negative = child index.
40
+ * e.g. [0,0,0,1,0,0] -> "3,-1,2"
41
+ */
42
+ get pathKey(): string;
32
43
  /**
33
44
  * Returns true if the node has children.
34
45
  */
35
46
  get hasChildren(): boolean;
47
+ /**
48
+ * Returns true if the node is the root (has no parent).
49
+ */
50
+ get isRoot(): boolean;
51
+ /**
52
+ * Returns true if the node is a leaf (has no children).
53
+ */
54
+ get isLeaf(): boolean;
55
+ /**
56
+ * Returns the root node of the tree.
57
+ */
58
+ get root(): TreeNode<T>;
59
+ /**
60
+ * Returns the depth of the node (root is 0).
61
+ */
62
+ get depth(): number;
63
+ /**
64
+ * Returns siblings of this node (excluding self).
65
+ */
66
+ get siblings(): TreeNode<T>[];
36
67
  /**
37
68
  * Add node as a child.
38
69
  */
@@ -46,13 +77,18 @@ export declare class TreeNode<T> {
46
77
  */
47
78
  drop(): TreeNode<T>;
48
79
  /**
49
- * Returns a shallow-copy.
80
+ * Returns a deep copy of structure, shallow copy of model.
50
81
  */
51
82
  clone(): TreeNode<T>;
52
83
  /**
53
84
  * Returns a node given a list of indices
54
85
  */
55
86
  fetch(indices: number[]): TreeNode<T> | null;
87
+ /**
88
+ * Returns a node given a pathKey string.
89
+ * @see pathKey
90
+ */
91
+ fetchByPathKey(pathKey: string): TreeNode<T> | null;
56
92
  /**
57
93
  * Returns list of nodes to the root.
58
94
  */
@@ -61,6 +97,10 @@ export declare class TreeNode<T> {
61
97
  * Iterates over a node's children and returns a new root node.
62
98
  */
63
99
  map<U>(callback: (node: TreeNode<T>) => U): TreeNode<U>;
100
+ /**
101
+ * Iterates over a node's children and returns a new root node.
102
+ */
103
+ mapAsync<U>(callback: (node: TreeNode<T>, parent: TreeNode<U> | undefined) => Promise<U>, parent?: TreeNode<U>): Promise<TreeNode<U>>;
64
104
  /**
65
105
  * Breadth-first search, return true in the callback to end iteration.
66
106
  */
@@ -73,10 +113,31 @@ export declare class TreeNode<T> {
73
113
  * Depth-first post-order search, return true in the callback to end iteration.
74
114
  */
75
115
  post(callback: SearchCallback<T>): TreeNode<T> | null;
116
+ /**
117
+ * Find the first node matching the predicate.
118
+ */
119
+ find(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T> | null;
120
+ /**
121
+ * Find all nodes matching the predicate.
122
+ */
123
+ findAll(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T>[];
76
124
  /**
77
125
  * Returns a list of nodes.
78
126
  */
79
127
  flatten(method: SearchStrategy): TreeNode<T>[];
128
+ /**
129
+ * Returns a nested array representation of the tree.
130
+ * - Leaf -> value
131
+ * - Single child -> [model, ...childResult]
132
+ * - Multiple leaf children -> [model, [leaves...]]
133
+ * - Multiple mixed children -> [model, child1Result, child2Result, ...]
134
+ */
135
+ toNestedArray(): NestedArray<T>;
136
+ /**
137
+ * Creates a tree from a nested array representation.
138
+ * @see toNestedArray
139
+ */
140
+ static fromNestedArray<T>(input: NestedArray<T>): TreeNode<T>;
80
141
  /**
81
142
  * Returns an object representation of the tree.
82
143
  */
@@ -1 +1 @@
1
- {"version":3,"file":"TreeNode.d.ts","sourceRoot":"","sources":["../src/TreeNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C;;;GAGG;AACH,oBAAY,cAAc,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,OAAO,GAAG,IAAI,CAAC;AAEtE;;;GAGG;AACH,oBAAY,cAAc,GAAG,KAAK,GAAG,MAAM,GAAG,SAAS,CAAC;AAExD;;GAEG;AACH,qBAAa,QAAQ,CAAC,CAAC;IAEZ,KAAK,EAAE,CAAC;IACR,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI;IAC1B,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE;gBAFvB,KAAK,EAAE,CAAC,EACR,MAAM,GAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAW,EACjC,QAAQ,GAAE,QAAQ,CAAC,CAAC,CAAC,EAAO;IAGrC;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC;IAMjD;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,CAGlB;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,MAAM,EAAE,CAGtB;IAED;;OAEG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CA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,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI;IAWrD;;OAEG;IACH,IAAI,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE,MAAM,GAAE,cAAsB,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI;IAInG;;OAEG;IACH,OAAO,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE,MAAM,GAAE,cAAsB,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE;IAQjG;;OAEG;IACH,OAAO,CAAC,MAAM,EAAE,cAAc,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE;IAQ9C;;;;;;OAMG;IACH,aAAa,IAAI,WAAW,CAAC,CAAC,CAAC;IAqB/B;;;OAGG;IACH,MAAM,CAAC,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC;IA8C7D;;OAEG;IACH,QAAQ,IAAI,UAAU,CAAC,CAAC,CAAC;CAM1B"}
package/dist/TreeNode.js CHANGED
@@ -1,8 +1,48 @@
1
1
  "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
3
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
4
- to[j] = from[i];
5
- return to;
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
6
46
  };
7
47
  Object.defineProperty(exports, "__esModule", { value: true });
8
48
  exports.TreeNode = void 0;
@@ -16,6 +56,7 @@ var TreeNode = /** @class */ (function () {
16
56
  this.model = model;
17
57
  this.parent = parent;
18
58
  this.children = children;
59
+ this._index = 0;
19
60
  }
20
61
  /**
21
62
  * Parses object into a tree and returns the root node.
@@ -30,9 +71,7 @@ var TreeNode = /** @class */ (function () {
30
71
  * Index of the node among its siblings.
31
72
  */
32
73
  get: function () {
33
- if (!this.parent)
34
- return 0;
35
- return this.parent.children.indexOf(this);
74
+ return this._index;
36
75
  },
37
76
  enumerable: false,
38
77
  configurable: true
@@ -42,9 +81,45 @@ var TreeNode = /** @class */ (function () {
42
81
  * Indices from the root to the node.
43
82
  */
44
83
  get: function () {
45
- if (!this.parent)
46
- return []; // Root
47
- return __spreadArray(__spreadArray([], this.parent.indices), [this.index]);
84
+ var indices = [];
85
+ var node = this;
86
+ while (node.parent) {
87
+ indices.push(node.index);
88
+ node = node.parent;
89
+ }
90
+ return indices.reverse();
91
+ },
92
+ enumerable: false,
93
+ configurable: true
94
+ });
95
+ Object.defineProperty(TreeNode.prototype, "pathKey", {
96
+ /**
97
+ * Compressed path key: positive = run of first-children, negative = child index.
98
+ * e.g. [0,0,0,1,0,0] -> "3,-1,2"
99
+ */
100
+ get: function () {
101
+ var indices = this.indices;
102
+ if (indices.length === 0)
103
+ return '';
104
+ var parts = [];
105
+ var zeroCount = 0;
106
+ for (var _i = 0, indices_1 = indices; _i < indices_1.length; _i++) {
107
+ var idx = indices_1[_i];
108
+ if (idx === 0) {
109
+ zeroCount++;
110
+ }
111
+ else {
112
+ if (zeroCount > 0) {
113
+ parts.push(zeroCount);
114
+ zeroCount = 0;
115
+ }
116
+ parts.push(-idx);
117
+ }
118
+ }
119
+ if (zeroCount > 0) {
120
+ parts.push(zeroCount);
121
+ }
122
+ return parts.join(',');
48
123
  },
49
124
  enumerable: false,
50
125
  configurable: true
@@ -59,11 +134,70 @@ var TreeNode = /** @class */ (function () {
59
134
  enumerable: false,
60
135
  configurable: true
61
136
  });
137
+ Object.defineProperty(TreeNode.prototype, "isRoot", {
138
+ /**
139
+ * Returns true if the node is the root (has no parent).
140
+ */
141
+ get: function () {
142
+ return this.parent === null;
143
+ },
144
+ enumerable: false,
145
+ configurable: true
146
+ });
147
+ Object.defineProperty(TreeNode.prototype, "isLeaf", {
148
+ /**
149
+ * Returns true if the node is a leaf (has no children).
150
+ */
151
+ get: function () {
152
+ return !this.hasChildren;
153
+ },
154
+ enumerable: false,
155
+ configurable: true
156
+ });
157
+ Object.defineProperty(TreeNode.prototype, "root", {
158
+ /**
159
+ * Returns the root node of the tree.
160
+ */
161
+ get: function () {
162
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
163
+ var node = this;
164
+ while (node.parent) {
165
+ node = node.parent;
166
+ }
167
+ return node;
168
+ },
169
+ enumerable: false,
170
+ configurable: true
171
+ });
172
+ Object.defineProperty(TreeNode.prototype, "depth", {
173
+ /**
174
+ * Returns the depth of the node (root is 0).
175
+ */
176
+ get: function () {
177
+ return this.parent ? this.parent.depth + 1 : 0;
178
+ },
179
+ enumerable: false,
180
+ configurable: true
181
+ });
182
+ Object.defineProperty(TreeNode.prototype, "siblings", {
183
+ /**
184
+ * Returns siblings of this node (excluding self).
185
+ */
186
+ get: function () {
187
+ var _this = this;
188
+ if (this.isRoot)
189
+ return [];
190
+ return this.parent.children.filter(function (child) { return child !== _this; });
191
+ },
192
+ enumerable: false,
193
+ configurable: true
194
+ });
62
195
  /**
63
196
  * Add node as a child.
64
197
  */
65
198
  TreeNode.prototype.add = function (child) {
66
199
  child.parent = this;
200
+ child._index = this.children.length;
67
201
  this.children.push(child);
68
202
  return child;
69
203
  };
@@ -77,21 +211,27 @@ var TreeNode = /** @class */ (function () {
77
211
  * Remove current node and its children from the tree and return.
78
212
  */
79
213
  TreeNode.prototype.drop = function () {
80
- if (this.parent !== null) {
81
- var idx = this.parent.children.indexOf(this);
214
+ if (!this.isRoot) {
215
+ var idx = this._index;
82
216
  this.parent.children.splice(idx, 1);
217
+ // Update indices of subsequent siblings
218
+ for (var i = idx; i < this.parent.children.length; i++) {
219
+ this.parent.children[i]._index = i;
220
+ }
83
221
  this.parent = null;
222
+ this._index = 0;
84
223
  }
85
224
  return this;
86
225
  };
87
226
  /**
88
- * Returns a shallow-copy.
227
+ * Returns a deep copy of structure, shallow copy of model.
89
228
  */
90
229
  TreeNode.prototype.clone = function () {
91
230
  var node = new TreeNode(this.model);
92
- node.children = this.children.map(function (child) {
231
+ node.children = this.children.map(function (child, i) {
93
232
  var newChild = child.clone();
94
233
  newChild.parent = node;
234
+ newChild._index = i;
95
235
  return newChild;
96
236
  });
97
237
  return node;
@@ -100,47 +240,106 @@ var TreeNode = /** @class */ (function () {
100
240
  * Returns a node given a list of indices
101
241
  */
102
242
  TreeNode.prototype.fetch = function (indices) {
243
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
103
244
  var node = this;
104
- for (var _i = 0, indices_1 = indices; _i < indices_1.length; _i++) {
105
- var i = indices_1[_i];
245
+ for (var _i = 0, indices_2 = indices; _i < indices_2.length; _i++) {
246
+ var i = indices_2[_i];
106
247
  node = node.children[i];
107
248
  if (!node)
108
249
  return null;
109
250
  }
110
- return node || null;
251
+ return node;
252
+ };
253
+ /**
254
+ * Returns a node given a pathKey string.
255
+ * @see pathKey
256
+ */
257
+ TreeNode.prototype.fetchByPathKey = function (pathKey) {
258
+ if (pathKey === '')
259
+ return this;
260
+ var indices = [];
261
+ var parts = pathKey.split(',').map(Number);
262
+ for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
263
+ var part = parts_1[_i];
264
+ if (part >= 0) {
265
+ for (var i = 0; i < part; i++) {
266
+ indices.push(0);
267
+ }
268
+ }
269
+ else {
270
+ indices.push(-part);
271
+ }
272
+ }
273
+ return this.fetch(indices);
111
274
  };
112
275
  /**
113
276
  * Returns list of nodes to the root.
114
277
  */
115
278
  TreeNode.prototype.path = function () {
116
279
  var path = [];
117
- var addToPath = function (node) {
118
- path.unshift(node);
119
- if (node.parent)
120
- addToPath(node.parent);
121
- };
122
- addToPath(this);
123
- return path;
280
+ var node = this;
281
+ while (node) {
282
+ path.push(node);
283
+ node = node.parent;
284
+ }
285
+ return path.reverse();
124
286
  };
125
287
  /**
126
288
  * Iterates over a node's children and returns a new root node.
127
289
  */
128
290
  TreeNode.prototype.map = function (callback) {
129
291
  var node = new TreeNode(callback(this));
130
- node.children = this.children.map(function (child) {
292
+ node.children = this.children.map(function (child, i) {
131
293
  var newChild = child.map(callback);
132
294
  newChild.parent = node;
295
+ newChild._index = i;
133
296
  return newChild;
134
297
  });
135
298
  return node;
136
299
  };
300
+ /**
301
+ * Iterates over a node's children and returns a new root node.
302
+ */
303
+ TreeNode.prototype.mapAsync = function (callback, parent) {
304
+ return __awaiter(this, void 0, void 0, function () {
305
+ var node, _a, _b;
306
+ var _this = this;
307
+ return __generator(this, function (_c) {
308
+ switch (_c.label) {
309
+ case 0:
310
+ _a = TreeNode.bind;
311
+ return [4 /*yield*/, callback(this, parent)];
312
+ case 1:
313
+ node = new (_a.apply(TreeNode, [void 0, _c.sent()]))();
314
+ _b = node;
315
+ return [4 /*yield*/, Promise.all(this.children.map(function (child, i) { return __awaiter(_this, void 0, void 0, function () {
316
+ var newChild;
317
+ return __generator(this, function (_a) {
318
+ switch (_a.label) {
319
+ case 0: return [4 /*yield*/, child.mapAsync(callback, node)];
320
+ case 1:
321
+ newChild = _a.sent();
322
+ newChild.parent = node;
323
+ newChild._index = i;
324
+ return [2 /*return*/, newChild];
325
+ }
326
+ });
327
+ }); }))];
328
+ case 2:
329
+ _b.children = _c.sent();
330
+ return [2 /*return*/, node];
331
+ }
332
+ });
333
+ });
334
+ };
137
335
  /**
138
336
  * Breadth-first search, return true in the callback to end iteration.
139
337
  */
140
338
  TreeNode.prototype.breadth = function (callback) {
141
339
  var queue = [this];
142
- while (queue.length) {
143
- var node = queue.shift();
340
+ var head = 0;
341
+ while (head < queue.length) {
342
+ var node = queue[head++];
144
343
  if (callback(node))
145
344
  return node;
146
345
  for (var i = 0, childCount = node.children.length; i < childCount; i++) {
@@ -175,6 +374,25 @@ var TreeNode = /** @class */ (function () {
175
374
  return this;
176
375
  return null;
177
376
  };
377
+ /**
378
+ * Find the first node matching the predicate.
379
+ */
380
+ TreeNode.prototype.find = function (predicate, method) {
381
+ if (method === void 0) { method = "pre"; }
382
+ return this[method](function (node) { return predicate(node); });
383
+ };
384
+ /**
385
+ * Find all nodes matching the predicate.
386
+ */
387
+ TreeNode.prototype.findAll = function (predicate, method) {
388
+ if (method === void 0) { method = "pre"; }
389
+ var results = [];
390
+ this[method](function (node) {
391
+ if (predicate(node))
392
+ results.push(node);
393
+ });
394
+ return results;
395
+ };
178
396
  /**
179
397
  * Returns a list of nodes.
180
398
  */
@@ -185,6 +403,77 @@ var TreeNode = /** @class */ (function () {
185
403
  });
186
404
  return list;
187
405
  };
406
+ /**
407
+ * Returns a nested array representation of the tree.
408
+ * - Leaf -> value
409
+ * - Single child -> [model, ...childResult]
410
+ * - Multiple leaf children -> [model, [leaves...]]
411
+ * - Multiple mixed children -> [model, child1Result, child2Result, ...]
412
+ */
413
+ TreeNode.prototype.toNestedArray = function () {
414
+ if (this.isLeaf) {
415
+ return this.model;
416
+ }
417
+ if (this.children.length === 1) {
418
+ var childResult = this.children[0].toNestedArray();
419
+ if (Array.isArray(childResult)) {
420
+ return __spreadArray([this.model], childResult, true);
421
+ }
422
+ return [this.model, childResult];
423
+ }
424
+ // Multiple children
425
+ var allLeaves = this.children.every(function (c) { return c.isLeaf; });
426
+ if (allLeaves) {
427
+ return [this.model, this.children.map(function (c) { return c.model; })];
428
+ }
429
+ return __spreadArray([this.model], this.children.map(function (c) { return c.toNestedArray(); }), true);
430
+ };
431
+ /**
432
+ * Creates a tree from a nested array representation.
433
+ * @see toNestedArray
434
+ */
435
+ TreeNode.fromNestedArray = function (input) {
436
+ if (!Array.isArray(input)) {
437
+ return new TreeNode(input);
438
+ }
439
+ var model = input[0], rest = input.slice(1);
440
+ var node = new TreeNode(model);
441
+ if (rest.length === 0) {
442
+ return node;
443
+ }
444
+ if (rest.length === 1 && Array.isArray(rest[0])) {
445
+ var inner = rest[0];
446
+ var hasArrays_1 = inner.some(function (x) { return Array.isArray(x); });
447
+ if (!hasArrays_1) {
448
+ // Multiple leaf children
449
+ for (var _i = 0, _a = inner; _i < _a.length; _i++) {
450
+ var leaf = _a[_i];
451
+ node.addModel(leaf);
452
+ }
453
+ }
454
+ else {
455
+ // Single non-leaf child
456
+ node.add(TreeNode.fromNestedArray(inner));
457
+ }
458
+ return node;
459
+ }
460
+ var hasArrays = rest.some(function (x) { return Array.isArray(x); });
461
+ if (!hasArrays) {
462
+ // Chain: [model, a, b, c] = model -> a -> b -> c
463
+ var current = node;
464
+ for (var _b = 0, _c = rest; _b < _c.length; _b++) {
465
+ var val = _c[_b];
466
+ current = current.addModel(val);
467
+ }
468
+ return node;
469
+ }
470
+ // Multiple children with at least one non-leaf
471
+ for (var _d = 0, rest_1 = rest; _d < rest_1.length; _d++) {
472
+ var childResult = rest_1[_d];
473
+ node.add(TreeNode.fromNestedArray(childResult));
474
+ }
475
+ return node;
476
+ };
188
477
  /**
189
478
  * Returns an object representation of the tree.
190
479
  */
@@ -1 +1 @@
1
- {"version":3,"file":"TreeNode.js","sourceRoot":"","sources":["../src/TreeNode.ts"],"names":[],"mappings":";;;;;;;;AAcA;;GAEG;AACH;IACE,kBACS,KAAQ,EACR,MAAiC,EACjC,QAA4B;QAD5B,uBAAA,EAAA,aAAiC;QACjC,yBAAA,EAAA,aAA4B;QAF5B,UAAK,GAAL,KAAK,CAAG;QACR,WAAM,GAAN,MAAM,CAA2B;QACjC,aAAQ,GAAR,QAAQ,CAAoB;IAClC,CAAC;IAEJ;;OAEG;IACI,cAAK,GAAZ,UAA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AC;YACf,iDAAiD;YACjD,IAAI,OAAO,GAAG,IAAI,CAAC;YACnB,KAAkB,UAAW,EAAX,KAAA,IAAW,EAAX,cAAW,EAAX,IAAW,EAAE,CAAC;gBAA3B,IAAM,GAAG,SAAA;gBACZ,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YAClC,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,+CAA+C;QAC/C,KAA0B,UAAI,EAAJ,aAAI,EAAJ,kBAAI,EAAJ,IAAI,EAAE,CAAC;YAA5B,IAAM,WAAW,aAAA;YACpB,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,eAAe,CAAI,WAA6B,CAAC,CAAC,CAAC;QACvE,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,2BAAQ,GAAR;QACE,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,QAAQ,EAAE,EAAhB,CAAgB,CAAC;SACzD,CAAC;IACJ,CAAC;IACH,eAAC;AAAD,CAAC,AAnZD,IAmZC;AAnZY,4BAAQ"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { TreeNode, SearchCallback, SearchStrategy } from './TreeNode';
1
+ export { TreeNode, SearchCallback, SearchStrategy, NestedArray } from './TreeNode';
2
2
  export { IParseable } from './IParseable';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACnF,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAAsE;AAA7D,oGAAA,QAAQ,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAAmF;AAA1E,oGAAA,QAAQ,OAAA"}
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * @public
4
3
  */
@@ -7,6 +6,12 @@ export declare interface IParseable<T> {
7
6
  children: IParseable<T>[];
8
7
  }
9
8
 
9
+ /**
10
+ * Nested array representation of a tree.
11
+ * @public
12
+ */
13
+ export declare type NestedArray<T> = T | [T, ...(NestedArray<T> | T[])[]];
14
+
10
15
  /**
11
16
  * Search callback passed to `.pre`, `.post`, and `.breadth`.
12
17
  * @public
@@ -26,6 +31,7 @@ export declare class TreeNode<T> {
26
31
  model: T;
27
32
  parent: TreeNode<T> | null;
28
33
  children: TreeNode<T>[];
34
+ private _index;
29
35
  constructor(model: T, parent?: TreeNode<T> | null, children?: TreeNode<T>[]);
30
36
  /**
31
37
  * Parses object into a tree and returns the root node.
@@ -39,10 +45,35 @@ export declare class TreeNode<T> {
39
45
  * Indices from the root to the node.
40
46
  */
41
47
  get indices(): number[];
48
+ /**
49
+ * Compressed path key: positive = run of first-children, negative = child index.
50
+ * e.g. [0,0,0,1,0,0] -> "3,-1,2"
51
+ */
52
+ get pathKey(): string;
42
53
  /**
43
54
  * Returns true if the node has children.
44
55
  */
45
56
  get hasChildren(): boolean;
57
+ /**
58
+ * Returns true if the node is the root (has no parent).
59
+ */
60
+ get isRoot(): boolean;
61
+ /**
62
+ * Returns true if the node is a leaf (has no children).
63
+ */
64
+ get isLeaf(): boolean;
65
+ /**
66
+ * Returns the root node of the tree.
67
+ */
68
+ get root(): TreeNode<T>;
69
+ /**
70
+ * Returns the depth of the node (root is 0).
71
+ */
72
+ get depth(): number;
73
+ /**
74
+ * Returns siblings of this node (excluding self).
75
+ */
76
+ get siblings(): TreeNode<T>[];
46
77
  /**
47
78
  * Add node as a child.
48
79
  */
@@ -56,13 +87,18 @@ export declare class TreeNode<T> {
56
87
  */
57
88
  drop(): TreeNode<T>;
58
89
  /**
59
- * Returns a shallow-copy.
90
+ * Returns a deep copy of structure, shallow copy of model.
60
91
  */
61
92
  clone(): TreeNode<T>;
62
93
  /**
63
94
  * Returns a node given a list of indices
64
95
  */
65
96
  fetch(indices: number[]): TreeNode<T> | null;
97
+ /**
98
+ * Returns a node given a pathKey string.
99
+ * @see pathKey
100
+ */
101
+ fetchByPathKey(pathKey: string): TreeNode<T> | null;
66
102
  /**
67
103
  * Returns list of nodes to the root.
68
104
  */
@@ -71,6 +107,10 @@ export declare class TreeNode<T> {
71
107
  * Iterates over a node's children and returns a new root node.
72
108
  */
73
109
  map<U>(callback: (node: TreeNode<T>) => U): TreeNode<U>;
110
+ /**
111
+ * Iterates over a node's children and returns a new root node.
112
+ */
113
+ mapAsync<U>(callback: (node: TreeNode<T>, parent: TreeNode<U> | undefined) => Promise<U>, parent?: TreeNode<U>): Promise<TreeNode<U>>;
74
114
  /**
75
115
  * Breadth-first search, return true in the callback to end iteration.
76
116
  */
@@ -83,10 +123,31 @@ export declare class TreeNode<T> {
83
123
  * Depth-first post-order search, return true in the callback to end iteration.
84
124
  */
85
125
  post(callback: SearchCallback<T>): TreeNode<T> | null;
126
+ /**
127
+ * Find the first node matching the predicate.
128
+ */
129
+ find(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T> | null;
130
+ /**
131
+ * Find all nodes matching the predicate.
132
+ */
133
+ findAll(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T>[];
86
134
  /**
87
135
  * Returns a list of nodes.
88
136
  */
89
137
  flatten(method: SearchStrategy): TreeNode<T>[];
138
+ /**
139
+ * Returns a nested array representation of the tree.
140
+ * - Leaf -> value
141
+ * - Single child -> [model, ...childResult]
142
+ * - Multiple leaf children -> [model, [leaves...]]
143
+ * - Multiple mixed children -> [model, child1Result, child2Result, ...]
144
+ */
145
+ toNestedArray(): NestedArray<T>;
146
+ /**
147
+ * Creates a tree from a nested array representation.
148
+ * @see toNestedArray
149
+ */
150
+ static fromNestedArray<T>(input: NestedArray<T>): TreeNode<T>;
90
151
  /**
91
152
  * Returns an object representation of the tree.
92
153
  */
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.15.1"
8
+ "packageVersion": "7.55.2"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "treenode.ts",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Tree-like data structure for typescript",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -10,7 +10,8 @@
10
10
  "build:docs": "npm run build:api && api-documenter markdown --input ./temp --output ./docs",
11
11
  "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
12
12
  "prepare": "tsc",
13
- "test": "jest"
13
+ "test": "jest",
14
+ "test:coverage": "jest --coverage"
14
15
  },
15
16
  "repository": {
16
17
  "type": "git",
@@ -26,15 +27,15 @@
26
27
  },
27
28
  "homepage": "https://github.com/lubert/treenode.ts#readme",
28
29
  "devDependencies": {
29
- "@microsoft/api-documenter": "^7.17.17",
30
- "@microsoft/api-extractor": "^7.15.1",
31
- "@types/jest": "^26.0.23",
32
- "@typescript-eslint/eslint-plugin": "^4.23.0",
33
- "@typescript-eslint/parser": "^4.23.0",
34
- "eslint": "^7.26.0",
35
- "jest": "^26.6.3",
36
- "ts-jest": "^26.5.6",
37
- "typescript": "^4.2.4"
30
+ "@microsoft/api-documenter": "^7.28.2",
31
+ "@microsoft/api-extractor": "^7.55.2",
32
+ "@types/jest": "^29.5.14",
33
+ "@typescript-eslint/eslint-plugin": "^7.18.0",
34
+ "@typescript-eslint/parser": "^7.18.0",
35
+ "eslint": "^8.57.1",
36
+ "jest": "^29.7.0",
37
+ "ts-jest": "^29.2.6",
38
+ "typescript": "^5.7.3"
38
39
  },
39
40
  "dependencies": {}
40
41
  }