fast-tree-builder 1.0.0-alpha.2 → 1.0.0-alpha.3
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 +5 -4
- package/index.d.ts +21 -21
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
# fast-tree-builder
|
|
2
2
|
|
|
3
|
+
[](https://npmjs.org/package/fast-tree-builder)
|
|
4
|
+
[](https://npmjs.org/package/fast-tree-builder)
|
|
3
5
|
[](https://github.com/lionel87/fast-tree-builder/actions/workflows/build.yaml)
|
|
4
6
|
[](https://coveralls.io/github/lionel87/fast-tree-builder?branch=master)
|
|
5
|
-

|
|
7
|
+

|
|
7
8
|
|
|
8
9
|
`fast-tree-builder` is an npm package that allows you to efficiently build trees from iterable data structures. With its optimized algorithm, strong TypeScript typings, and customizable node structure, it provides a reliable solution for organizing and manipulating hierarchical data.
|
|
9
10
|
|
|
10
11
|
## Prerequisites
|
|
11
12
|
|
|
12
13
|
- You have a list of items,
|
|
13
|
-
-
|
|
14
|
-
-
|
|
14
|
+
- each item is identifiable by a unique id,
|
|
15
|
+
- the items are connected via a *parent id* OR *children ids*.
|
|
15
16
|
|
|
16
17
|
## Features
|
|
17
18
|
|
package/index.d.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
type TreeNode<
|
|
2
|
-
[k in
|
|
3
|
-
} :
|
|
4
|
-
[k in Exclude<
|
|
1
|
+
type TreeNode<TInputData, TDataKey extends string | number | symbol | false, TParentKey extends string | number | symbol | false, TChildrenKey extends string | number | symbol> = (TDataKey extends false ? (TParentKey extends false ? TInputData & {
|
|
2
|
+
[k in TChildrenKey]?: TreeNode<TInputData, TDataKey, TParentKey, TChildrenKey>[];
|
|
3
|
+
} : TInputData & {
|
|
4
|
+
[k in Exclude<TParentKey, false>]?: TreeNode<TInputData, TDataKey, TParentKey, TChildrenKey>;
|
|
5
5
|
} & {
|
|
6
|
-
[k in
|
|
7
|
-
}) : (
|
|
8
|
-
[k in Exclude<
|
|
6
|
+
[k in TChildrenKey]?: TreeNode<TInputData, TDataKey, TParentKey, TChildrenKey>[];
|
|
7
|
+
}) : (TParentKey extends false ? {
|
|
8
|
+
[k in Exclude<TDataKey, false>]: TInputData;
|
|
9
9
|
} & {
|
|
10
|
-
[k in
|
|
10
|
+
[k in TChildrenKey]?: TreeNode<TInputData, TDataKey, TParentKey, TChildrenKey>[];
|
|
11
11
|
} : {
|
|
12
|
-
[k in Exclude<
|
|
12
|
+
[k in Exclude<TDataKey, false>]: TInputData;
|
|
13
13
|
} & {
|
|
14
|
-
[k in Exclude<
|
|
14
|
+
[k in Exclude<TParentKey, false>]?: TreeNode<TInputData, TDataKey, TParentKey, TChildrenKey>;
|
|
15
15
|
} & {
|
|
16
|
-
[k in
|
|
16
|
+
[k in TChildrenKey]?: TreeNode<TInputData, TDataKey, TParentKey, TChildrenKey>[];
|
|
17
17
|
}));
|
|
18
18
|
type KeyReturnType<T, P extends keyof T | ((item: T) => any)> = P extends ((item: T) => infer R) ? R : P extends keyof T ? T[P] : never;
|
|
19
|
-
declare function buildTree<
|
|
19
|
+
declare function buildTree<TInputData extends (TDataKey extends false ? object : unknown), TKey extends keyof TInputData | ((item: TInputData) => any), TMappedData extends (TDataKey extends false ? object : unknown) = TInputData, TDataKey extends string | number | symbol | false = 'data', TParentKey extends string | number | symbol | false = 'parent', TChildrenKey extends string | number | symbol = 'children'>(items: Iterable<TInputData>, options?: {
|
|
20
20
|
mode?: 'parent' | 'children';
|
|
21
|
-
key?:
|
|
22
|
-
parentKey?: keyof
|
|
23
|
-
childrenKey?: keyof
|
|
24
|
-
nodeDataKey?:
|
|
25
|
-
nodeParentKey?:
|
|
26
|
-
nodeChildrenKey?:
|
|
21
|
+
key?: TKey;
|
|
22
|
+
parentKey?: keyof TInputData | ((item: TInputData) => KeyReturnType<TInputData, TKey>);
|
|
23
|
+
childrenKey?: keyof TInputData | ((item: TInputData) => KeyReturnType<TInputData, TKey>);
|
|
24
|
+
nodeDataKey?: TDataKey;
|
|
25
|
+
nodeParentKey?: TParentKey;
|
|
26
|
+
nodeChildrenKey?: TChildrenKey;
|
|
27
27
|
mapNodeData?: {
|
|
28
|
-
(item:
|
|
28
|
+
(item: TInputData): TMappedData;
|
|
29
29
|
};
|
|
30
30
|
validRootKeys?: Iterable<unknown>;
|
|
31
31
|
validRootParentKeys?: Iterable<unknown>;
|
|
32
32
|
validateTree?: boolean;
|
|
33
33
|
}): {
|
|
34
|
-
roots: TreeNode<
|
|
35
|
-
nodes: Map<KeyReturnType<
|
|
34
|
+
roots: TreeNode<TMappedData extends undefined ? TInputData : TMappedData, TDataKey, TParentKey, TChildrenKey>[];
|
|
35
|
+
nodes: Map<KeyReturnType<TInputData, TKey>, TreeNode<TMappedData extends undefined ? TInputData : TMappedData, TDataKey, TParentKey, TChildrenKey>>;
|
|
36
36
|
};
|
|
37
37
|
export default buildTree;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-tree-builder",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.3",
|
|
4
4
|
"description": "Efficiently construct highly customizable bi-directional tree structures from iterable data.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"coverage": "c8 -r text -r text-summary -r lcov --include \"*.js\" npm test"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"c8": "^
|
|
27
|
-
"chokidar": "^
|
|
28
|
-
"mocha": "^
|
|
26
|
+
"c8": "^10.1.3",
|
|
27
|
+
"chokidar": "^4.0.3",
|
|
28
|
+
"mocha": "^11.1.0",
|
|
29
29
|
"typescript": "^5.3.3"
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/lionel87/fast-tree-builder#readme",
|