fast-tree-builder 2.0.0-beta.4 → 2.0.0-beta.6
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/README.md +20 -7
- package/index.d.cts +3 -3
- package/index.d.mts +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,18 +61,29 @@ Builds a tree structure from an iterable list of items.
|
|
|
61
61
|
|
|
62
62
|
##### One of
|
|
63
63
|
|
|
64
|
-
- `parentId`: A key or function that
|
|
65
|
-
- `childIds`: A key or function that
|
|
64
|
+
- `parentId`: A key or function that accesses the parent ID of the item.
|
|
65
|
+
- `childIds`: A key or function that accesses an iterable of child IDs for the item.
|
|
66
66
|
|
|
67
67
|
##### Optional
|
|
68
68
|
|
|
69
69
|
- `valueResolver`: Function to transform an item to a custom value stored in the node. Defaults to use the input item as is.
|
|
70
|
-
- `valueKey`: Key where the item is stored in the output node. Set to `false` to
|
|
70
|
+
- `valueKey`: Key where the item is stored in the output node. Set to `false` to merge the item's properties directly into the node (shallow copy). Defaults to `'value'`.
|
|
71
71
|
- `parentKey`: Key where the node's parent reference is stored in the output node. Set to `false` to omit parent links. Defaults to `'parent'`.
|
|
72
72
|
- `childrenKey`: Key where the node's children are stored in the output node. Defaults to `'children'`.
|
|
73
|
-
- `depthKey`: Key where the node's depth (with root = 0) is stored in the output node. Set to `false` to omit depth values.
|
|
74
|
-
- `validateReferences`: When `true`, verifies all `parentId` or `childIds` resolve to real items. Errors are thrown on invalid references. Defaults to `false`.
|
|
75
|
-
- `validateTree`: When `true`, verifies that the final structure is a valid tree (no cycles or nodes reachable via
|
|
73
|
+
- `depthKey`: Key where the node's depth (with root = 0) is stored in the output node. Set to `false` to omit depth values. Setting this enables validateTree implicitly, as depth calculation requires full tree validation. Defaults to `false`.
|
|
74
|
+
- `validateReferences`: When `true`, verifies all `parentId` or `childIds` resolve to real items. Only `null` and `undefined` are acceptable parent ids for root nodes when enabled. Errors are thrown on invalid references. Defaults to `false`.
|
|
75
|
+
- `validateTree`: When `true`, verifies that the final structure is a valid tree (no cycles or nodes reachable via multiple paths). Errors are thrown if the check fails. Defaults to `false`.
|
|
76
|
+
|
|
77
|
+
> **Input Accessors vs. Output Keys**
|
|
78
|
+
>
|
|
79
|
+
> * `id`, `parentId`, `childIds` works on the input item and can be property names or functions. The library does not make any assumption what an id should be so we purposely allow `null` and `undefined` as a valid id too!
|
|
80
|
+
> * `valueKey`, `parentKey`, `childrenKey`, `depthKey` are always strings or `false` and are used as keys in the output nodes.
|
|
81
|
+
|
|
82
|
+
> **'validateReferences' option**
|
|
83
|
+
>
|
|
84
|
+
> Validation operates differently when in `parentId` mode and in `childIds` mode!
|
|
85
|
+
> * in `parentId` mode: validates that the parent ids of root nodes was `null` or `undefined` and nothing else. If you expect these parent ids to be other than `null` or `undefined`, you can safely turn off this validation and loop trough on the roots manually to check the original parentId values are the ones you expect.
|
|
86
|
+
> * in `childIds` mode: validates that every referenced child is resolved. Even if the child list contains `undefined`, a node with an `undefined` as ID must exist in the input.
|
|
76
87
|
|
|
77
88
|
#### Returns
|
|
78
89
|
|
|
@@ -250,7 +261,7 @@ const items = [
|
|
|
250
261
|
|
|
251
262
|
const { roots, nodes } = buildTree(items, {
|
|
252
263
|
id: item => item.substring(2, 4),
|
|
253
|
-
|
|
264
|
+
parentId: item => item.substring(0, 2),
|
|
254
265
|
valueResolver: item => ({ name: item.substring(4) }),
|
|
255
266
|
valueKey: false, // merge item data into node
|
|
256
267
|
});
|
|
@@ -282,6 +293,8 @@ console.log(roots);
|
|
|
282
293
|
type TreeNode = typeof roots[number];
|
|
283
294
|
```
|
|
284
295
|
|
|
296
|
+
We intentionally do not expose a generic `TreeNode` type from the package. It is harder to parameterize correctly by hand than to write a recursive type from scratch.
|
|
297
|
+
|
|
285
298
|
## Comparison with other tree building libraries
|
|
286
299
|
|
|
287
300
|
The package aims to be feature complete and highly customizable, which usually opposes with performance. There are other packages that may be more *performant* but lacks features that I really needed in my daily coding. In standard scenarios this package should perform more than enough and nearly as good as any other package.
|
package/index.d.cts
CHANGED
|
@@ -25,7 +25,7 @@ declare function buildTree<TIdAccessor extends (keyof NoInfer<TInputValue>) | ((
|
|
|
25
25
|
/**
|
|
26
26
|
* Key where the item is stored in the output node.
|
|
27
27
|
*
|
|
28
|
-
* Set to `false` to
|
|
28
|
+
* Set to `false` to merge the item's properties directly into the node (shallow copy).
|
|
29
29
|
*
|
|
30
30
|
* Defaults to `'value'`.
|
|
31
31
|
*/
|
|
@@ -50,8 +50,8 @@ declare function buildTree<TIdAccessor extends (keyof NoInfer<TInputValue>) | ((
|
|
|
50
50
|
*
|
|
51
51
|
* Set to `false` to omit depth values.
|
|
52
52
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
53
|
+
* Setting this enables validateTree implicitly, as depth calculation requires full tree validation.
|
|
54
|
+
* Both operations share the same traversal logic so the additional tree validation is not an overhead.
|
|
55
55
|
*
|
|
56
56
|
* Defaults to `false`.
|
|
57
57
|
*/
|
package/index.d.mts
CHANGED
|
@@ -25,7 +25,7 @@ export default function buildTree<TIdAccessor extends (keyof NoInfer<TInputValue
|
|
|
25
25
|
/**
|
|
26
26
|
* Key where the item is stored in the output node.
|
|
27
27
|
*
|
|
28
|
-
* Set to `false` to
|
|
28
|
+
* Set to `false` to merge the item's properties directly into the node (shallow copy).
|
|
29
29
|
*
|
|
30
30
|
* Defaults to `'value'`.
|
|
31
31
|
*/
|
|
@@ -50,8 +50,8 @@ export default function buildTree<TIdAccessor extends (keyof NoInfer<TInputValue
|
|
|
50
50
|
*
|
|
51
51
|
* Set to `false` to omit depth values.
|
|
52
52
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
53
|
+
* Setting this enables validateTree implicitly, as depth calculation requires full tree validation.
|
|
54
|
+
* Both operations share the same traversal logic so the additional tree validation is not an overhead.
|
|
55
55
|
*
|
|
56
56
|
* Defaults to `false`.
|
|
57
57
|
*/
|
package/package.json
CHANGED