@c-rex/utils 0.1.20 → 0.1.22

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.
@@ -1,8 +1,199 @@
1
1
  import { ObjectRefModel, TreeOfContent, DirectoryNodeModel } from '@c-rex/interfaces';
2
2
 
3
+ /**
4
+ * DIRECTORY NODES MODULE
5
+ *
6
+ * OVERVIEW:
7
+ * This module manages the hierarchical structure of directory/documentation nodes,
8
+ * transforming API data into a navigable tree structure.
9
+ *
10
+ * KEY CONCEPTS:
11
+ *
12
+ * 1. NODE STRUCTURE:
13
+ * - Each node (DirectoryNode) can have parents (nodes above) and children (nodes below)
14
+ * - Each node has labels (title) and informationUnits (content/links)
15
+ * - The structure forms a bidirectional tree: children point to parents and vice-versa
16
+ *
17
+ * 2. "ACTIVE" CONCEPT:
18
+ * - A node is "active: true" when it is part of the current/selected path
19
+ * - Only ONE path is active at a time (from selected item to root)
20
+ * - Other nodes at the same level are "active: false" (unselected siblings)
21
+ * - Used to highlight current navigation in the UI
22
+ *
23
+ * 3. NAVIGATION:
24
+ * - We start with a specific node (user entry point)
25
+ * - We traverse upward (parents) until we find the root (node without parents)
26
+ * - We capture all siblings at each level to display as navigation options
27
+ * - We build a tree with the active path highlighted
28
+ *
29
+ * 4. VALIDATION:
30
+ * - Each node MUST have: labels[0] (title) and informationUnits[0] (link/content)
31
+ * - If either is missing, the node is ignored or marks end of tree (root)
32
+ * - ShortId is always required to identify nodes
33
+ *
34
+ * 5. ERROR HANDLING:
35
+ * - If API fails to fetch a node, we continue with nodes we have
36
+ * - Errors are logged but do not interrupt the flow
37
+ * - We return the maximum information possible
38
+ */
39
+
40
+ /**
41
+ * FUNCTION: generateTreeOfContent
42
+ *
43
+ * PURPOSE:
44
+ * Generates a complete navigation tree starting from an initial node,
45
+ * including parents, siblings, and marking the active path.
46
+ *
47
+ * EXECUTION FLOW:
48
+ * 1. Validates input (non-empty array with shortId)
49
+ * 2. Fetches the initial node from API
50
+ * 3. Loads children (siblings) of the initial node
51
+ * 4. Creates active item with children
52
+ * 5. Enters loop: for each parent found...
53
+ * a. Fetches the parent node
54
+ * b. Loads its children (which include the previous node)
55
+ * c. Replaces the inactive version of previous node with active version
56
+ * d. Creates new active item with the parent
57
+ * 6. Loop terminates when:
58
+ * - No more parents (reached root), OR
59
+ * - Node is missing labels/informationUnits (incomplete data)
60
+ * 7. Returns array with root as single item containing entire tree
61
+ *
62
+ * EXAMPLE OUTPUT STRUCTURE:
63
+ * Input: node "article-123" inside "category-xyz" inside "root"
64
+ * Output: [
65
+ * {
66
+ * id: 'root',
67
+ * label: 'Root',
68
+ * active: true,
69
+ * children: [
70
+ * {
71
+ * id: 'category-xyz',
72
+ * label: 'Category XYZ',
73
+ * active: true,
74
+ * children: [
75
+ * { id: 'article-123', label: 'Article', active: true, children: [] }
76
+ * ]
77
+ * },
78
+ * { id: 'other-category', label: 'Other', active: false, children: [] }
79
+ * ]
80
+ * }
81
+ * ]
82
+ *
83
+ * @param directoryNodes - Array with the initial node
84
+ * @returns Array with single item (root) containing entire structure
85
+ */
3
86
  declare const generateTreeOfContent: (directoryNodes: ObjectRefModel[]) => Promise<TreeOfContent[]>;
87
+ type GenerateTreeOfContentOptions = {
88
+ includeLinkIds?: boolean;
89
+ };
90
+ declare const generateTreeOfContentWithOptions: (directoryNodes: ObjectRefModel[], options?: GenerateTreeOfContentOptions) => Promise<TreeOfContent[]>;
91
+ /**
92
+ * FUNCTION: getChildrenInfo
93
+ *
94
+ * PURPOSE:
95
+ * Fetches detailed information for multiple child nodes in parallel,
96
+ * converting them to TreeOfContent structure.
97
+ *
98
+ * FLOW:
99
+ * 1. Validates if children exist
100
+ * 2. Makes parallel requests for ALL children (Promise.all)
101
+ * - If one fails, continues with others (silent catch)
102
+ * - Logs error but does not interrupt
103
+ * 3. Filters valid results:
104
+ * a. Removes undefined responses (failed requests)
105
+ * b. Removes nodes without labels (incomplete data)
106
+ * 4. For each valid node:
107
+ * a. Extracts label (title) from first label
108
+ * b. Extracts linkId (content reference) from first informationUnit
109
+ * c. Creates TreeOfContent with active=false (unselected siblings)
110
+ * d. Initializes children as empty array (will be filled in next call)
111
+ * 5. Returns array of formatted nodes
112
+ *
113
+ * IMPORTANT:
114
+ * - ALL returned children have active=false
115
+ * - The active node will be marked LATER by generateTreeOfContent
116
+ * - Parallel requests = better performance than sequential
117
+ *
118
+ * @param childNodes - Array of references to child nodes
119
+ * @returns Array of TreeOfContent with active=false for all
120
+ */
4
121
  declare const getChildrenInfo: (childNodes: ObjectRefModel[]) => Promise<TreeOfContent[]>;
122
+ /**
123
+ * FUNCTION: getRootNode
124
+ *
125
+ * PURPOSE:
126
+ * Finds the root node of the hierarchy from any node,
127
+ * climbing up the ancestor chain to reach the top.
128
+ *
129
+ * FLOW:
130
+ * 1. Validates input (non-empty array with valid shortId)
131
+ * 2. Fetches the initial node
132
+ * 3. Checks if it is already root:
133
+ * - If it has no ancestors, returns this node as root
134
+ * 4. Validates node data:
135
+ * - MUST have informationUnits[0]
136
+ * - MUST have labels[0]
137
+ * - Returns null if missing
138
+ * 5. Navigates through ancestors:
139
+ * a. Ancestors is an array of arrays (path history?)
140
+ * b. Gets first element [0] (main path)
141
+ * c. Gets last element of this path [-1] (top)
142
+ * d. This is the root node
143
+ * 6. Fetches and returns the root node
144
+ *
145
+ * MULTIPLE VALIDATIONS:
146
+ * - Each access is validated to avoid TypeError
147
+ * - Returns null if any access fails
148
+ * - Ensures returned node has complete data
149
+ *
150
+ * @param directoryNodes - Array with the starting node
151
+ * @returns Root node with complete data, or null if invalid
152
+ */
5
153
  declare const getRootNode: (directoryNodes: ObjectRefModel[]) => Promise<DirectoryNodeModel | null>;
154
+ /**
155
+ * FUNCTION: getLastLabel
156
+ *
157
+ * PURPOSE:
158
+ * Recursively extracts the title (label) of the deepest node in the active path.
159
+ * Useful for displaying "you are at: [final breadcrumb]" in navigation UI.
160
+ *
161
+ * ALGORITHM (Recursive):
162
+ * 1. Filters the tree to find the node with active=true
163
+ * 2. If not found, returns empty string
164
+ * 3. If found:
165
+ * a. Stores its label
166
+ * b. Checks if it has children with any active node
167
+ * c. If yes, recursively searches at the next level
168
+ * d. Returns the deepest label found in the active path
169
+ *
170
+ * EXAMPLE EXECUTION:
171
+ * Input tree:
172
+ * [
173
+ * {
174
+ * label: 'Home',
175
+ * active: true,
176
+ * children: [
177
+ * {
178
+ * label: 'Documentation',
179
+ * active: true,
180
+ * children: [
181
+ * { label: 'API Reference', active: true, children: [] }
182
+ * ]
183
+ * }
184
+ * ]
185
+ * }
186
+ * ]
187
+ * Output: 'API Reference' (the deepest node in active path)
188
+ *
189
+ * EDGE CASES:
190
+ * - Empty tree: returns ""
191
+ * - No active nodes: returns ""
192
+ * - Only root is active: returns root label
193
+ *
194
+ * @param tree - Complete content tree structure
195
+ * @returns String containing the label of the deepest active node
196
+ */
6
197
  declare const getLastLabel: (tree: TreeOfContent[]) => string;
7
198
 
8
- export { generateTreeOfContent, getChildrenInfo, getLastLabel, getRootNode };
199
+ export { generateTreeOfContent, generateTreeOfContentWithOptions, getChildrenInfo, getLastLabel, getRootNode };