-
Notifications
You must be signed in to change notification settings - Fork 0
/
treewithpath.d.ts
227 lines (206 loc) · 7.31 KB
/
treewithpath.d.ts
1
2
3
4
5
6
7
8
9
10
11
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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* A node that is more convenient to store in JSON
*/
declare interface NodeJson {
name: string;
data: any;
children: NodeJson[];
}
/**
* The class of the TreeWithPath tree
*/
declare class Tree {
/**
* Creates a tree
*
* @param data Tree root node data. The name of the root node is always root
* @constructor
* @example
* const Tree = require("treewithpath");
* const tree = new Tree({ text: "Hello, world!", "otherText": "hoI!" });
*/
public constructor(data: any);
/**
* Root node of this tree
*
* @returns {Node} Root node
* @this {Tree}
*/
public get root(): Node;
/**
* Adds a node to the tree and returns it
*
* @param {string} name The name of the node to add
* @param data The data of the node to be created
* @param {string} path The path to the parent of the node to create
* @this {Tree} Tree
* @return {Node} Created node
* @throws {TreeError} In case the node already exists
* @example
* tree.add("node2", { text: "Hello, world!", "otherText": "hoI!" }, "/node1");
*/
public add(name: string, data: any, path: string): Node;
/**
* Gets the node at the specified path
*
* @param {string} path The path to the node to receive
* @param {boolean} error Optional parameter. The default is true. If true, an exception will be thrown if the path is incorrect. Otherwise, null will be returned
* @this {Tree} Tree
* @returns {Node | null} The resulting node or null if error = false and node not found
* @throws {TreeError} In case the node is not found and error = true
* @example
* tree.get("/node1");
*/
public get(path: string, error: boolean = true): Node | null;
/**
* Deletes the node and returns it at the specified path
*
* @param {string} path The path to the node to be deleted
* @this {Tree} Tree
* @returns {Node} A deleted node that no longer contains children. Children are permanently deleted
* @throws {TreeError} In case the node is not found
* @example
* tree.remove("/node1");
*/
public remove(path: string): Node;
/**
* Calls a callback for each node in the tree
*
* @param {Function} callback A function called for each node of the tree. The node in the first argument is passed to the function
* @this {Tree} Tree
* @example
* tree.traverse(node => {
* console.log(node.name);
* });
*/
public traverse(callback: (node: Node) => void): void;
/**
* Returns a tree object suitable for storage in JSON format. This method is mainly used by the JSON.stringify function
*
* @this {Tree} Tree
* @returns {object} A tree object suitable for storage in JSON format
* @example
* tree.toJSON(); // { name: "root", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node1", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node2", data: {text: "Hello, world!", "otherText": "hoI!" }, children: [] }] }
*/
public toJSON(): NodeJson;
/**
* Checks a node for existence in a tree
*
* @param {string} path The path to the node to check
* @returns {boolean} True if the node exists and false if it does not exist
* @this {Tree} Tree
* @example
* tree.has("/notExists/child") // false
* tree.has("/exists") // true
*/
public has(path: string): boolean;
/**
* Creates a tree from an object that returns the toJSON() method
*
* @param {object} json A tree object suitable for storage in JSON format
* @returns {Tree} Created tree
* @example
* const tree = Tree.fromJSON({ name: "root", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{name: "node1", data: {text: "Hello, world!", "otherText": "hoI!"}, children: [{name: "node2", data: {text: "Hello, world!", "otherText": "hoI!"}, children: [] }] });
*/
public static fromJSON(json: NodeJson): Tree;
/**
* Connects the two specified paths into one.
*
* @param {string} firstPath First path
* @param {sting} secondPath Second path
* @returns {string} United path
* @example
* Tree.joinPath("/node1", "node2") // /node1/node2
*/
public static joinPath(firstPath: string, secondPath: string): string;
}
/**
* Node class
*/
declare class Node {
/**
* Creates an instance of a node. Do not use this constructor yourself. To add nodes, use the add and addChild methods
*
* @param {string} name The name of the node
* @param data Node data
* @param {Tree} tree The tree to which the node will belong
* @constructor
*/
public constructor(name: string, data: any, tree: Tree);
/**
* Name of this node
*/
public name: string;
/**
* Data of this node
*/
public data: any;
/**
* The tree to which the node will belong
*
* @returns {Tree} Tree to which the node will belong
* @this {Node}
*/
public get tree(): Tree;
/**
* Getter to get the path to this node
*
* @returns {string} The path to this node
* @this {Node} Node
* @throws {TreeError} In case this node does not belong to any tree
*/
public get path(): string;
/**
* Getter to get the parent of this node
*
* @returns {Node} Parent of this node
* @this {Node} Node
* @throws {TreeError} In case this node does not belong to any tree
*/
public get parent(): Node;
/**
* A getter that returns an array of children of the given node
*
* @this {Node} Node
* @returns {Array} Array of children for this node
*/
public get children(): Node[];
/**
* Deletes the given node and its children
*
* @this {Node} Node
* @throws {TreeError} In case this node does not belong to any tree
* @throws {TreeError} If this node is the root
*/
public remove(): void;
/**
* Adds a child to this node
*
* @param {string} name The name of the node to add
* @param data The data of the node being added
* @this {Node} Node
* @throws {TreeError} In case this node does not belong to any tree
* @throws {TreeError} In case the node already exists
*/
public addChild(name: string, data: any): Node;
/**
* Returns a node object suitable for storage in JSON format. This method is mainly used by the JSON.stringify function
*
* @this {Node} Node
* @returns {object} A node object suitable for storage in JSON format
* @example
* node.toJSON(); // { name: "root", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node1", data: {text: "Hello, world!", "otherText": "hoI!" }, children: [{ name: "node2", data: { text: "Hello, world!", "otherText": "hoI!" }, children: [] }] }
*/
public toJSON(): NodeJson;
/**
* Calls a callback for each child node of this node
*
* @param {Function} callback A function called for child node of this node The node in the first argument is passed to the function
* @this {Node} Node
* @example
* node.traverse(node => {
* console.log(node.name);
* });
*/
public traverse(callback: (node: Node) => void): void;
}