fast-tree-builder 2.0.0-beta.4 → 2.0.0-beta.5
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 +13 -6
- package/index.d.cts +3 -3
- package/index.d.mts +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,18 +61,23 @@ 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.
|
|
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
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
|
|
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
|
+
> **Accessors vs. Output Keys**
|
|
78
|
+
>
|
|
79
|
+
> * `id`, `parentId`, `childIds` works on the input item and can be property names or functions.
|
|
80
|
+
> * `valueKey`, `parentKey`, `childrenKey`, `depthKey` are always strings or `false` and are used as keys in the output nodes.
|
|
76
81
|
|
|
77
82
|
#### Returns
|
|
78
83
|
|
|
@@ -250,7 +255,7 @@ const items = [
|
|
|
250
255
|
|
|
251
256
|
const { roots, nodes } = buildTree(items, {
|
|
252
257
|
id: item => item.substring(2, 4),
|
|
253
|
-
|
|
258
|
+
parentId: item => item.substring(0, 2),
|
|
254
259
|
valueResolver: item => ({ name: item.substring(4) }),
|
|
255
260
|
valueKey: false, // merge item data into node
|
|
256
261
|
});
|
|
@@ -282,6 +287,8 @@ console.log(roots);
|
|
|
282
287
|
type TreeNode = typeof roots[number];
|
|
283
288
|
```
|
|
284
289
|
|
|
290
|
+
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.
|
|
291
|
+
|
|
285
292
|
## Comparison with other tree building libraries
|
|
286
293
|
|
|
287
294
|
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