@c-rex/utils 0.1.20 → 0.1.21
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/directoryNodes.d.mts +187 -0
- package/dist/directoryNodes.d.ts +187 -0
- package/dist/directoryNodes.js.map +1 -1
- package/dist/directoryNodes.mjs.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +16 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,195 @@
|
|
|
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
|
+
/**
|
|
88
|
+
* FUNCTION: getChildrenInfo
|
|
89
|
+
*
|
|
90
|
+
* PURPOSE:
|
|
91
|
+
* Fetches detailed information for multiple child nodes in parallel,
|
|
92
|
+
* converting them to TreeOfContent structure.
|
|
93
|
+
*
|
|
94
|
+
* FLOW:
|
|
95
|
+
* 1. Validates if children exist
|
|
96
|
+
* 2. Makes parallel requests for ALL children (Promise.all)
|
|
97
|
+
* - If one fails, continues with others (silent catch)
|
|
98
|
+
* - Logs error but does not interrupt
|
|
99
|
+
* 3. Filters valid results:
|
|
100
|
+
* a. Removes undefined responses (failed requests)
|
|
101
|
+
* b. Removes nodes without labels (incomplete data)
|
|
102
|
+
* 4. For each valid node:
|
|
103
|
+
* a. Extracts label (title) from first label
|
|
104
|
+
* b. Extracts linkId (content reference) from first informationUnit
|
|
105
|
+
* c. Creates TreeOfContent with active=false (unselected siblings)
|
|
106
|
+
* d. Initializes children as empty array (will be filled in next call)
|
|
107
|
+
* 5. Returns array of formatted nodes
|
|
108
|
+
*
|
|
109
|
+
* IMPORTANT:
|
|
110
|
+
* - ALL returned children have active=false
|
|
111
|
+
* - The active node will be marked LATER by generateTreeOfContent
|
|
112
|
+
* - Parallel requests = better performance than sequential
|
|
113
|
+
*
|
|
114
|
+
* @param childNodes - Array of references to child nodes
|
|
115
|
+
* @returns Array of TreeOfContent with active=false for all
|
|
116
|
+
*/
|
|
4
117
|
declare const getChildrenInfo: (childNodes: ObjectRefModel[]) => Promise<TreeOfContent[]>;
|
|
118
|
+
/**
|
|
119
|
+
* FUNCTION: getRootNode
|
|
120
|
+
*
|
|
121
|
+
* PURPOSE:
|
|
122
|
+
* Finds the root node of the hierarchy from any node,
|
|
123
|
+
* climbing up the ancestor chain to reach the top.
|
|
124
|
+
*
|
|
125
|
+
* FLOW:
|
|
126
|
+
* 1. Validates input (non-empty array with valid shortId)
|
|
127
|
+
* 2. Fetches the initial node
|
|
128
|
+
* 3. Checks if it is already root:
|
|
129
|
+
* - If it has no ancestors, returns this node as root
|
|
130
|
+
* 4. Validates node data:
|
|
131
|
+
* - MUST have informationUnits[0]
|
|
132
|
+
* - MUST have labels[0]
|
|
133
|
+
* - Returns null if missing
|
|
134
|
+
* 5. Navigates through ancestors:
|
|
135
|
+
* a. Ancestors is an array of arrays (path history?)
|
|
136
|
+
* b. Gets first element [0] (main path)
|
|
137
|
+
* c. Gets last element of this path [-1] (top)
|
|
138
|
+
* d. This is the root node
|
|
139
|
+
* 6. Fetches and returns the root node
|
|
140
|
+
*
|
|
141
|
+
* MULTIPLE VALIDATIONS:
|
|
142
|
+
* - Each access is validated to avoid TypeError
|
|
143
|
+
* - Returns null if any access fails
|
|
144
|
+
* - Ensures returned node has complete data
|
|
145
|
+
*
|
|
146
|
+
* @param directoryNodes - Array with the starting node
|
|
147
|
+
* @returns Root node with complete data, or null if invalid
|
|
148
|
+
*/
|
|
5
149
|
declare const getRootNode: (directoryNodes: ObjectRefModel[]) => Promise<DirectoryNodeModel | null>;
|
|
150
|
+
/**
|
|
151
|
+
* FUNCTION: getLastLabel
|
|
152
|
+
*
|
|
153
|
+
* PURPOSE:
|
|
154
|
+
* Recursively extracts the title (label) of the deepest node in the active path.
|
|
155
|
+
* Useful for displaying "you are at: [final breadcrumb]" in navigation UI.
|
|
156
|
+
*
|
|
157
|
+
* ALGORITHM (Recursive):
|
|
158
|
+
* 1. Filters the tree to find the node with active=true
|
|
159
|
+
* 2. If not found, returns empty string
|
|
160
|
+
* 3. If found:
|
|
161
|
+
* a. Stores its label
|
|
162
|
+
* b. Checks if it has children with any active node
|
|
163
|
+
* c. If yes, recursively searches at the next level
|
|
164
|
+
* d. Returns the deepest label found in the active path
|
|
165
|
+
*
|
|
166
|
+
* EXAMPLE EXECUTION:
|
|
167
|
+
* Input tree:
|
|
168
|
+
* [
|
|
169
|
+
* {
|
|
170
|
+
* label: 'Home',
|
|
171
|
+
* active: true,
|
|
172
|
+
* children: [
|
|
173
|
+
* {
|
|
174
|
+
* label: 'Documentation',
|
|
175
|
+
* active: true,
|
|
176
|
+
* children: [
|
|
177
|
+
* { label: 'API Reference', active: true, children: [] }
|
|
178
|
+
* ]
|
|
179
|
+
* }
|
|
180
|
+
* ]
|
|
181
|
+
* }
|
|
182
|
+
* ]
|
|
183
|
+
* Output: 'API Reference' (the deepest node in active path)
|
|
184
|
+
*
|
|
185
|
+
* EDGE CASES:
|
|
186
|
+
* - Empty tree: returns ""
|
|
187
|
+
* - No active nodes: returns ""
|
|
188
|
+
* - Only root is active: returns root label
|
|
189
|
+
*
|
|
190
|
+
* @param tree - Complete content tree structure
|
|
191
|
+
* @returns String containing the label of the deepest active node
|
|
192
|
+
*/
|
|
6
193
|
declare const getLastLabel: (tree: TreeOfContent[]) => string;
|
|
7
194
|
|
|
8
195
|
export { generateTreeOfContent, getChildrenInfo, getLastLabel, getRootNode };
|
package/dist/directoryNodes.d.ts
CHANGED
|
@@ -1,8 +1,195 @@
|
|
|
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
|
+
/**
|
|
88
|
+
* FUNCTION: getChildrenInfo
|
|
89
|
+
*
|
|
90
|
+
* PURPOSE:
|
|
91
|
+
* Fetches detailed information for multiple child nodes in parallel,
|
|
92
|
+
* converting them to TreeOfContent structure.
|
|
93
|
+
*
|
|
94
|
+
* FLOW:
|
|
95
|
+
* 1. Validates if children exist
|
|
96
|
+
* 2. Makes parallel requests for ALL children (Promise.all)
|
|
97
|
+
* - If one fails, continues with others (silent catch)
|
|
98
|
+
* - Logs error but does not interrupt
|
|
99
|
+
* 3. Filters valid results:
|
|
100
|
+
* a. Removes undefined responses (failed requests)
|
|
101
|
+
* b. Removes nodes without labels (incomplete data)
|
|
102
|
+
* 4. For each valid node:
|
|
103
|
+
* a. Extracts label (title) from first label
|
|
104
|
+
* b. Extracts linkId (content reference) from first informationUnit
|
|
105
|
+
* c. Creates TreeOfContent with active=false (unselected siblings)
|
|
106
|
+
* d. Initializes children as empty array (will be filled in next call)
|
|
107
|
+
* 5. Returns array of formatted nodes
|
|
108
|
+
*
|
|
109
|
+
* IMPORTANT:
|
|
110
|
+
* - ALL returned children have active=false
|
|
111
|
+
* - The active node will be marked LATER by generateTreeOfContent
|
|
112
|
+
* - Parallel requests = better performance than sequential
|
|
113
|
+
*
|
|
114
|
+
* @param childNodes - Array of references to child nodes
|
|
115
|
+
* @returns Array of TreeOfContent with active=false for all
|
|
116
|
+
*/
|
|
4
117
|
declare const getChildrenInfo: (childNodes: ObjectRefModel[]) => Promise<TreeOfContent[]>;
|
|
118
|
+
/**
|
|
119
|
+
* FUNCTION: getRootNode
|
|
120
|
+
*
|
|
121
|
+
* PURPOSE:
|
|
122
|
+
* Finds the root node of the hierarchy from any node,
|
|
123
|
+
* climbing up the ancestor chain to reach the top.
|
|
124
|
+
*
|
|
125
|
+
* FLOW:
|
|
126
|
+
* 1. Validates input (non-empty array with valid shortId)
|
|
127
|
+
* 2. Fetches the initial node
|
|
128
|
+
* 3. Checks if it is already root:
|
|
129
|
+
* - If it has no ancestors, returns this node as root
|
|
130
|
+
* 4. Validates node data:
|
|
131
|
+
* - MUST have informationUnits[0]
|
|
132
|
+
* - MUST have labels[0]
|
|
133
|
+
* - Returns null if missing
|
|
134
|
+
* 5. Navigates through ancestors:
|
|
135
|
+
* a. Ancestors is an array of arrays (path history?)
|
|
136
|
+
* b. Gets first element [0] (main path)
|
|
137
|
+
* c. Gets last element of this path [-1] (top)
|
|
138
|
+
* d. This is the root node
|
|
139
|
+
* 6. Fetches and returns the root node
|
|
140
|
+
*
|
|
141
|
+
* MULTIPLE VALIDATIONS:
|
|
142
|
+
* - Each access is validated to avoid TypeError
|
|
143
|
+
* - Returns null if any access fails
|
|
144
|
+
* - Ensures returned node has complete data
|
|
145
|
+
*
|
|
146
|
+
* @param directoryNodes - Array with the starting node
|
|
147
|
+
* @returns Root node with complete data, or null if invalid
|
|
148
|
+
*/
|
|
5
149
|
declare const getRootNode: (directoryNodes: ObjectRefModel[]) => Promise<DirectoryNodeModel | null>;
|
|
150
|
+
/**
|
|
151
|
+
* FUNCTION: getLastLabel
|
|
152
|
+
*
|
|
153
|
+
* PURPOSE:
|
|
154
|
+
* Recursively extracts the title (label) of the deepest node in the active path.
|
|
155
|
+
* Useful for displaying "you are at: [final breadcrumb]" in navigation UI.
|
|
156
|
+
*
|
|
157
|
+
* ALGORITHM (Recursive):
|
|
158
|
+
* 1. Filters the tree to find the node with active=true
|
|
159
|
+
* 2. If not found, returns empty string
|
|
160
|
+
* 3. If found:
|
|
161
|
+
* a. Stores its label
|
|
162
|
+
* b. Checks if it has children with any active node
|
|
163
|
+
* c. If yes, recursively searches at the next level
|
|
164
|
+
* d. Returns the deepest label found in the active path
|
|
165
|
+
*
|
|
166
|
+
* EXAMPLE EXECUTION:
|
|
167
|
+
* Input tree:
|
|
168
|
+
* [
|
|
169
|
+
* {
|
|
170
|
+
* label: 'Home',
|
|
171
|
+
* active: true,
|
|
172
|
+
* children: [
|
|
173
|
+
* {
|
|
174
|
+
* label: 'Documentation',
|
|
175
|
+
* active: true,
|
|
176
|
+
* children: [
|
|
177
|
+
* { label: 'API Reference', active: true, children: [] }
|
|
178
|
+
* ]
|
|
179
|
+
* }
|
|
180
|
+
* ]
|
|
181
|
+
* }
|
|
182
|
+
* ]
|
|
183
|
+
* Output: 'API Reference' (the deepest node in active path)
|
|
184
|
+
*
|
|
185
|
+
* EDGE CASES:
|
|
186
|
+
* - Empty tree: returns ""
|
|
187
|
+
* - No active nodes: returns ""
|
|
188
|
+
* - Only root is active: returns root label
|
|
189
|
+
*
|
|
190
|
+
* @param tree - Complete content tree structure
|
|
191
|
+
* @returns String containing the label of the deepest active node
|
|
192
|
+
*/
|
|
6
193
|
declare const getLastLabel: (tree: TreeOfContent[]) => string;
|
|
7
194
|
|
|
8
195
|
export { generateTreeOfContent, getChildrenInfo, getLastLabel, getRootNode };
|