Skip to content

Commit 8a4b318

Browse files
committed
Add comprehensive JSDoc documentation for tree manipulation functions
1 parent 665bcee commit 8a4b318

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

src/index.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,40 @@ import { computed, isReactive, reactive } from "vue";
33

44
export type unObject = Record<string, unknown>;
55

6+
/**
7+
* Creates an array of node objects with parent and siblings
8+
*
9+
* @param {unObject[]} siblings - Array of sibling nodes
10+
* @param {unObject} [parent] - Parent node
11+
* @returns {{ node: unObject; parent: unObject; siblings: unObject }[]} Array
12+
* of objects containing node, parent, and siblings
13+
*/
614
const configurable = true,
715
getItems = (siblings: unObject[], parent?: unObject) =>
816
[...siblings].reverse().map((node) => ({ node, parent, siblings }));
917

18+
/**
19+
* Creates a flat representation of a JSON tree with helper functions to
20+
* manipulate the tree
21+
*
22+
* @param {unObject[]} tree - The tree structure to flatten
23+
* @param {object} [root0] - Configuration object for key names
24+
* @param {string} [root0.branch] - Key name for branch property (default:
25+
* "branch")
26+
* @param {string} [root0.children] - Key name for children property (default:
27+
* "children")
28+
* @param {string} [root0.id] - Key name for id property (default: "id")
29+
* @param {string} [root0.index] - Key name for index property (default:
30+
* "index")
31+
* @param {string} [root0.next] - Key name for next property (default: "next")
32+
* @param {string} [root0.parent] - Key name for parent property (default:
33+
* "parent")
34+
* @param {string} [root0.prev] - Key name for prev property (default: "prev")
35+
* @param {string} [root0.siblings] - Key name for siblings property (default:
36+
* "siblings")
37+
* @returns {object} Object containing nodes, nodesMap and manipulation
38+
* functions
39+
*/
1040
export default (
1141
tree: unObject[],
1242
{
@@ -22,34 +52,62 @@ export default (
2252
) => {
2353
const properties = {
2454
[keyBranch]: {
55+
/**
56+
* Gets the branch (path from root) for the current node
57+
*
58+
* @returns {unObject[]} Array of nodes from root to current node
59+
*/
2560
get(this: unObject): unObject[] {
2661
const ret = [this];
2762
while (ret[0]?.[keyParent]) ret.unshift(ret[0][keyParent] as unObject);
2863
return ret;
2964
},
3065
},
3166
[keyIndex]: {
67+
/**
68+
* Gets the index of the current node in its siblings array
69+
*
70+
* @returns {number} Index of the node in its siblings array
71+
*/
3272
get(this: unObject): number {
3373
return (this[keySiblings] as unObject[]).findIndex(
3474
(sibling) => this[keyId] === sibling[keyId],
3575
);
3676
},
3777
},
3878
[keyNext]: {
79+
/**
80+
* Gets the next sibling node
81+
*
82+
* @returns {undefined | unObject} Next sibling node or undefined if none
83+
*/
3984
get(this: unObject): undefined | unObject {
4085
return (this[keySiblings] as unObject[])[
4186
(this[keyIndex] as number) + 1
4287
];
4388
},
4489
},
4590
[keyPrev]: {
91+
/**
92+
* Gets the previous sibling node
93+
*
94+
* @returns {undefined | unObject} Previous sibling node or undefined if
95+
* none
96+
*/
4697
get(this: unObject): undefined | unObject {
4798
return (this[keySiblings] as unObject[])[
4899
(this[keyIndex] as number) - 1
49100
];
50101
},
51102
},
52103
};
104+
/**
105+
* Generator function that traverses the tree and yields each node
106+
*
107+
* @param {unObject[]} nodes - Array of nodes to traverse
108+
* @yields {unObject} Each node in the tree
109+
* @returns {Generator<unObject>} Generator that yields nodes
110+
*/
53111
const getNodes = function* (nodes: unObject[]) {
54112
const stack = getItems(nodes);
55113
while (stack.length) {
@@ -82,6 +140,14 @@ export default (
82140
nodes.value.map((node) => [node[keyId] as string, node]),
83141
),
84142
),
143+
/**
144+
* Function to run actions on nodes
145+
*
146+
* @param {string} pId - ID of the node to perform action on
147+
* @param {string} action - Action to perform (add, addChild, remove, up,
148+
* down, left, right)
149+
* @returns {string | undefined} ID of the affected node or undefined
150+
*/
85151
run = (pId: string, action: string) => {
86152
const the = nodesMap.value[pId];
87153
if (the) {
@@ -155,14 +221,58 @@ export default (
155221
};
156222

157223
return {
224+
/**
225+
* Adds a new sibling node after the specified node
226+
*
227+
* @param {string} pId - ID of the node to add a sibling to
228+
* @returns {string | undefined} ID of the newly added node
229+
*/
158230
add: (pId: string) => run(pId, "add"),
231+
/**
232+
* Adds a new child node to the specified node
233+
*
234+
* @param {string} pId - ID of the node to add a child to
235+
* @returns {string | undefined} ID of the newly added child node
236+
*/
159237
addChild: (pId: string) => run(pId, "addChild"),
238+
/**
239+
* Moves the specified node one position down within its siblings
240+
*
241+
* @param {string} pId - ID of the node to move down
242+
* @returns {void}
243+
*/
160244
down: (pId: string) => run(pId, "down"),
245+
/**
246+
* Moves the specified node one level up in the hierarchy, making it a
247+
* sibling of its parent
248+
*
249+
* @param {string} pId - ID of the node to move left
250+
* @returns {string | undefined} ID of the parent node if successful
251+
*/
161252
left: (pId: string) => run(pId, "left"),
162253
nodes,
163254
nodesMap,
255+
/**
256+
* Removes the specified node from the tree
257+
*
258+
* @param {string} pId - ID of the node to remove
259+
* @returns {string | undefined} ID of the next node that gets focus after
260+
* removal
261+
*/
164262
remove: (pId: string) => run(pId, "remove"),
263+
/**
264+
* Moves the specified node as a child of the previous sibling
265+
*
266+
* @param {string} pId - ID of the node to move right
267+
* @returns {string | undefined} ID of the new parent node if successful
268+
*/
165269
right: (pId: string) => run(pId, "right"),
270+
/**
271+
* Moves the specified node one position up within its siblings
272+
*
273+
* @param {string} pId - ID of the node to move up
274+
* @returns {void}
275+
*/
166276
up: (pId: string) => run(pId, "up"),
167277
};
168278
};

0 commit comments

Comments
 (0)