treenode.ts 0.7.0 → 0.8.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/dist/{treenode.ts.d.ts → index.d.mts} +170 -157
- package/dist/index.d.ts +170 -3
- package/dist/index.js +415 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +390 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -6
- package/dist/IParseable.d.ts +0 -8
- package/dist/IParseable.d.ts.map +0 -1
- package/dist/IParseable.js +0 -3
- package/dist/IParseable.js.map +0 -1
- package/dist/TreeNode.d.ts +0 -146
- package/dist/TreeNode.d.ts.map +0 -1
- package/dist/TreeNode.js +0 -489
- package/dist/TreeNode.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/tsdoc-metadata.json +0 -11
|
@@ -1,157 +1,170 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @public
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
model: T;
|
|
6
|
-
children: IParseable<T>[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
*
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
*/
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Returns
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
*
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
*
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
*
|
|
149
|
-
*/
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Returns
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*/
|
|
4
|
+
interface IParseable<T> {
|
|
5
|
+
model: T;
|
|
6
|
+
children: IParseable<T>[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Search callback passed to `.pre`, `.post`, and `.breadth`.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
type SearchCallback<T> = (node: TreeNode<T>) => boolean | void;
|
|
14
|
+
/**
|
|
15
|
+
* Search methods on TreeNode, passed to `.flatten`.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
type SearchStrategy = "pre" | "post" | "breadth";
|
|
19
|
+
/**
|
|
20
|
+
* Nested array representation of a tree.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
type NestedArray<T> = T | [T, ...(NestedArray<T> | T[])[]];
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
declare class TreeNode<T> {
|
|
28
|
+
model: T;
|
|
29
|
+
parent: TreeNode<T> | null;
|
|
30
|
+
private _index;
|
|
31
|
+
private _children;
|
|
32
|
+
constructor(model: T, parent?: TreeNode<T> | null, children?: TreeNode<T>[]);
|
|
33
|
+
/**
|
|
34
|
+
* Returns the children of the node as a readonly array.
|
|
35
|
+
* Use add(), drop(), or swap() to modify children.
|
|
36
|
+
*/
|
|
37
|
+
get children(): readonly TreeNode<T>[];
|
|
38
|
+
/**
|
|
39
|
+
* Parses object into a tree and returns the root node.
|
|
40
|
+
*/
|
|
41
|
+
static parse<T>(tree: IParseable<T>): TreeNode<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Index of the node among its siblings.
|
|
44
|
+
*/
|
|
45
|
+
get index(): number;
|
|
46
|
+
/**
|
|
47
|
+
* Indices from the root to the node.
|
|
48
|
+
*/
|
|
49
|
+
get indices(): number[];
|
|
50
|
+
/**
|
|
51
|
+
* Compressed path key: positive = run of first-children, negative = child index.
|
|
52
|
+
* e.g. [0,0,0,1,0,0] -> "3,-1,2"
|
|
53
|
+
*/
|
|
54
|
+
get pathKey(): string;
|
|
55
|
+
/**
|
|
56
|
+
* Returns true if the node has children.
|
|
57
|
+
*/
|
|
58
|
+
get hasChildren(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Returns true if the node is the root (has no parent).
|
|
61
|
+
*/
|
|
62
|
+
get isRoot(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Returns true if the node is a leaf (has no children).
|
|
65
|
+
*/
|
|
66
|
+
get isLeaf(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the root node of the tree.
|
|
69
|
+
*/
|
|
70
|
+
get root(): TreeNode<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the depth of the node (root is 0).
|
|
73
|
+
*/
|
|
74
|
+
get depth(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Returns siblings of this node (excluding self).
|
|
77
|
+
*/
|
|
78
|
+
get siblings(): TreeNode<T>[];
|
|
79
|
+
/**
|
|
80
|
+
* Add node as a child.
|
|
81
|
+
* @param child - The node to add
|
|
82
|
+
* @param index - Optional index to insert at. If omitted, appends to end.
|
|
83
|
+
* @throws RangeError if index is out of bounds (negative or > children.length)
|
|
84
|
+
*/
|
|
85
|
+
add(child: TreeNode<T>, index?: number): TreeNode<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Swap children at indices i and j.
|
|
88
|
+
* @param i - First index
|
|
89
|
+
* @param j - Second index
|
|
90
|
+
*/
|
|
91
|
+
swap(i: number, j: number): void;
|
|
92
|
+
/**
|
|
93
|
+
* Add model as a child.
|
|
94
|
+
* @param model - The model to add
|
|
95
|
+
* @param index - Optional index to insert at. If omitted, appends to end.
|
|
96
|
+
*/
|
|
97
|
+
addModel(model: T, index?: number): TreeNode<T>;
|
|
98
|
+
/**
|
|
99
|
+
* Remove current node and its children from the tree and return.
|
|
100
|
+
*/
|
|
101
|
+
drop(): TreeNode<T>;
|
|
102
|
+
/**
|
|
103
|
+
* Returns a deep copy of structure, shallow copy of model.
|
|
104
|
+
*/
|
|
105
|
+
clone(): TreeNode<T>;
|
|
106
|
+
/**
|
|
107
|
+
* Returns a node given a list of indices
|
|
108
|
+
*/
|
|
109
|
+
fetch(indices: number[]): TreeNode<T> | null;
|
|
110
|
+
/**
|
|
111
|
+
* Returns a node given a pathKey string.
|
|
112
|
+
* @see pathKey
|
|
113
|
+
*/
|
|
114
|
+
fetchByPathKey(pathKey: string): TreeNode<T> | null;
|
|
115
|
+
/**
|
|
116
|
+
* Returns list of nodes to the root.
|
|
117
|
+
*/
|
|
118
|
+
path(): TreeNode<T>[];
|
|
119
|
+
/**
|
|
120
|
+
* Iterates over a node's children and returns a new root node.
|
|
121
|
+
*/
|
|
122
|
+
map<U>(callback: (node: TreeNode<T>) => U): TreeNode<U>;
|
|
123
|
+
/**
|
|
124
|
+
* Iterates over a node's children and returns a new root node.
|
|
125
|
+
*/
|
|
126
|
+
mapAsync<U>(callback: (node: TreeNode<T>, parent: TreeNode<U> | undefined) => Promise<U>, parent?: TreeNode<U>): Promise<TreeNode<U>>;
|
|
127
|
+
/**
|
|
128
|
+
* Breadth-first search, return true in the callback to end iteration.
|
|
129
|
+
*/
|
|
130
|
+
breadth(callback: SearchCallback<T>): TreeNode<T> | null;
|
|
131
|
+
/**
|
|
132
|
+
* Depth-first pre-order search, return true in the callback to end iteration.
|
|
133
|
+
*/
|
|
134
|
+
pre(callback: SearchCallback<T>): TreeNode<T> | null;
|
|
135
|
+
/**
|
|
136
|
+
* Depth-first post-order search, return true in the callback to end iteration.
|
|
137
|
+
*/
|
|
138
|
+
post(callback: SearchCallback<T>): TreeNode<T> | null;
|
|
139
|
+
/**
|
|
140
|
+
* Find the first node matching the predicate.
|
|
141
|
+
*/
|
|
142
|
+
find(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T> | null;
|
|
143
|
+
/**
|
|
144
|
+
* Find all nodes matching the predicate.
|
|
145
|
+
*/
|
|
146
|
+
findAll(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T>[];
|
|
147
|
+
/**
|
|
148
|
+
* Returns a list of nodes.
|
|
149
|
+
*/
|
|
150
|
+
flatten(method: SearchStrategy): TreeNode<T>[];
|
|
151
|
+
/**
|
|
152
|
+
* Returns a nested array representation of the tree.
|
|
153
|
+
* - Leaf -> value
|
|
154
|
+
* - Single child -> [model, ...childResult]
|
|
155
|
+
* - Multiple leaf children -> [model, [leaves...]]
|
|
156
|
+
* - Multiple mixed children -> [model, child1Result, child2Result, ...]
|
|
157
|
+
*/
|
|
158
|
+
toNestedArray(): NestedArray<T>;
|
|
159
|
+
/**
|
|
160
|
+
* Creates a tree from a nested array representation.
|
|
161
|
+
* @see toNestedArray
|
|
162
|
+
*/
|
|
163
|
+
static fromNestedArray<T>(input: NestedArray<T>): TreeNode<T>;
|
|
164
|
+
/**
|
|
165
|
+
* Returns an object representation of the tree.
|
|
166
|
+
*/
|
|
167
|
+
toObject(): IParseable<T>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export { type IParseable, type NestedArray, type SearchCallback, type SearchStrategy, TreeNode };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,170 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*/
|
|
4
|
+
interface IParseable<T> {
|
|
5
|
+
model: T;
|
|
6
|
+
children: IParseable<T>[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Search callback passed to `.pre`, `.post`, and `.breadth`.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
type SearchCallback<T> = (node: TreeNode<T>) => boolean | void;
|
|
14
|
+
/**
|
|
15
|
+
* Search methods on TreeNode, passed to `.flatten`.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
type SearchStrategy = "pre" | "post" | "breadth";
|
|
19
|
+
/**
|
|
20
|
+
* Nested array representation of a tree.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
type NestedArray<T> = T | [T, ...(NestedArray<T> | T[])[]];
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
declare class TreeNode<T> {
|
|
28
|
+
model: T;
|
|
29
|
+
parent: TreeNode<T> | null;
|
|
30
|
+
private _index;
|
|
31
|
+
private _children;
|
|
32
|
+
constructor(model: T, parent?: TreeNode<T> | null, children?: TreeNode<T>[]);
|
|
33
|
+
/**
|
|
34
|
+
* Returns the children of the node as a readonly array.
|
|
35
|
+
* Use add(), drop(), or swap() to modify children.
|
|
36
|
+
*/
|
|
37
|
+
get children(): readonly TreeNode<T>[];
|
|
38
|
+
/**
|
|
39
|
+
* Parses object into a tree and returns the root node.
|
|
40
|
+
*/
|
|
41
|
+
static parse<T>(tree: IParseable<T>): TreeNode<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Index of the node among its siblings.
|
|
44
|
+
*/
|
|
45
|
+
get index(): number;
|
|
46
|
+
/**
|
|
47
|
+
* Indices from the root to the node.
|
|
48
|
+
*/
|
|
49
|
+
get indices(): number[];
|
|
50
|
+
/**
|
|
51
|
+
* Compressed path key: positive = run of first-children, negative = child index.
|
|
52
|
+
* e.g. [0,0,0,1,0,0] -> "3,-1,2"
|
|
53
|
+
*/
|
|
54
|
+
get pathKey(): string;
|
|
55
|
+
/**
|
|
56
|
+
* Returns true if the node has children.
|
|
57
|
+
*/
|
|
58
|
+
get hasChildren(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Returns true if the node is the root (has no parent).
|
|
61
|
+
*/
|
|
62
|
+
get isRoot(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Returns true if the node is a leaf (has no children).
|
|
65
|
+
*/
|
|
66
|
+
get isLeaf(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the root node of the tree.
|
|
69
|
+
*/
|
|
70
|
+
get root(): TreeNode<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the depth of the node (root is 0).
|
|
73
|
+
*/
|
|
74
|
+
get depth(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Returns siblings of this node (excluding self).
|
|
77
|
+
*/
|
|
78
|
+
get siblings(): TreeNode<T>[];
|
|
79
|
+
/**
|
|
80
|
+
* Add node as a child.
|
|
81
|
+
* @param child - The node to add
|
|
82
|
+
* @param index - Optional index to insert at. If omitted, appends to end.
|
|
83
|
+
* @throws RangeError if index is out of bounds (negative or > children.length)
|
|
84
|
+
*/
|
|
85
|
+
add(child: TreeNode<T>, index?: number): TreeNode<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Swap children at indices i and j.
|
|
88
|
+
* @param i - First index
|
|
89
|
+
* @param j - Second index
|
|
90
|
+
*/
|
|
91
|
+
swap(i: number, j: number): void;
|
|
92
|
+
/**
|
|
93
|
+
* Add model as a child.
|
|
94
|
+
* @param model - The model to add
|
|
95
|
+
* @param index - Optional index to insert at. If omitted, appends to end.
|
|
96
|
+
*/
|
|
97
|
+
addModel(model: T, index?: number): TreeNode<T>;
|
|
98
|
+
/**
|
|
99
|
+
* Remove current node and its children from the tree and return.
|
|
100
|
+
*/
|
|
101
|
+
drop(): TreeNode<T>;
|
|
102
|
+
/**
|
|
103
|
+
* Returns a deep copy of structure, shallow copy of model.
|
|
104
|
+
*/
|
|
105
|
+
clone(): TreeNode<T>;
|
|
106
|
+
/**
|
|
107
|
+
* Returns a node given a list of indices
|
|
108
|
+
*/
|
|
109
|
+
fetch(indices: number[]): TreeNode<T> | null;
|
|
110
|
+
/**
|
|
111
|
+
* Returns a node given a pathKey string.
|
|
112
|
+
* @see pathKey
|
|
113
|
+
*/
|
|
114
|
+
fetchByPathKey(pathKey: string): TreeNode<T> | null;
|
|
115
|
+
/**
|
|
116
|
+
* Returns list of nodes to the root.
|
|
117
|
+
*/
|
|
118
|
+
path(): TreeNode<T>[];
|
|
119
|
+
/**
|
|
120
|
+
* Iterates over a node's children and returns a new root node.
|
|
121
|
+
*/
|
|
122
|
+
map<U>(callback: (node: TreeNode<T>) => U): TreeNode<U>;
|
|
123
|
+
/**
|
|
124
|
+
* Iterates over a node's children and returns a new root node.
|
|
125
|
+
*/
|
|
126
|
+
mapAsync<U>(callback: (node: TreeNode<T>, parent: TreeNode<U> | undefined) => Promise<U>, parent?: TreeNode<U>): Promise<TreeNode<U>>;
|
|
127
|
+
/**
|
|
128
|
+
* Breadth-first search, return true in the callback to end iteration.
|
|
129
|
+
*/
|
|
130
|
+
breadth(callback: SearchCallback<T>): TreeNode<T> | null;
|
|
131
|
+
/**
|
|
132
|
+
* Depth-first pre-order search, return true in the callback to end iteration.
|
|
133
|
+
*/
|
|
134
|
+
pre(callback: SearchCallback<T>): TreeNode<T> | null;
|
|
135
|
+
/**
|
|
136
|
+
* Depth-first post-order search, return true in the callback to end iteration.
|
|
137
|
+
*/
|
|
138
|
+
post(callback: SearchCallback<T>): TreeNode<T> | null;
|
|
139
|
+
/**
|
|
140
|
+
* Find the first node matching the predicate.
|
|
141
|
+
*/
|
|
142
|
+
find(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T> | null;
|
|
143
|
+
/**
|
|
144
|
+
* Find all nodes matching the predicate.
|
|
145
|
+
*/
|
|
146
|
+
findAll(predicate: (node: TreeNode<T>) => boolean, method?: SearchStrategy): TreeNode<T>[];
|
|
147
|
+
/**
|
|
148
|
+
* Returns a list of nodes.
|
|
149
|
+
*/
|
|
150
|
+
flatten(method: SearchStrategy): TreeNode<T>[];
|
|
151
|
+
/**
|
|
152
|
+
* Returns a nested array representation of the tree.
|
|
153
|
+
* - Leaf -> value
|
|
154
|
+
* - Single child -> [model, ...childResult]
|
|
155
|
+
* - Multiple leaf children -> [model, [leaves...]]
|
|
156
|
+
* - Multiple mixed children -> [model, child1Result, child2Result, ...]
|
|
157
|
+
*/
|
|
158
|
+
toNestedArray(): NestedArray<T>;
|
|
159
|
+
/**
|
|
160
|
+
* Creates a tree from a nested array representation.
|
|
161
|
+
* @see toNestedArray
|
|
162
|
+
*/
|
|
163
|
+
static fromNestedArray<T>(input: NestedArray<T>): TreeNode<T>;
|
|
164
|
+
/**
|
|
165
|
+
* Returns an object representation of the tree.
|
|
166
|
+
*/
|
|
167
|
+
toObject(): IParseable<T>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export { type IParseable, type NestedArray, type SearchCallback, type SearchStrategy, TreeNode };
|