smsmslib 1.0.79 → 1.0.80

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,5 +1,7 @@
1
1
  /**
2
2
  * @file
3
+ * Provides utility functions for **non-recursive deep traversal** of objects and
4
+ * arrays.
3
5
  *
4
6
  * @module javascr/system/deepwalk
5
7
  */
@@ -8,12 +10,73 @@
8
10
  * Imports.
9
11
  * --------------------------------------------------------------------------- */
10
12
 
13
+ import
14
+ {
15
+ go_typeof,
16
+ sj_is_object,
17
+ }
18
+ from "./type.js";
19
+
20
+ import
21
+ {
22
+ sj_array_new,
23
+ }
24
+ from "./arraynew.js";
25
+
26
+ import
27
+ {
28
+ sj_array_push,
29
+ }
30
+ from "./arraypush.js";
31
+
32
+ import
33
+ {
34
+ sj_prop_get_own,
35
+ sj_prop_set_own,
36
+ }
37
+ from "./prop.js";
38
+
39
+ import
40
+ {
41
+ go_tree_walk_ret,
42
+ sj_tree_walk,
43
+ }
44
+ from "./tree.js";
11
45
 
12
46
 
13
47
  /** ---------------------------------------------------------------------------
14
48
  * Functions.
15
49
  * --------------------------------------------------------------------------- */
16
50
 
51
+ /**
52
+ * Performs a deep traversal of an object or array.
53
+ *
54
+ * This function initiates a non-recursive tree walk starting from the given
55
+ * `value`. It uses a stack-based approach to navigate through nested
56
+ * properties up to a specified depth.
57
+ *
58
+ * @param {any} value [in]
59
+ * The root value to start the traversal from.
60
+ *
61
+ * @param {number} i_depth [in]
62
+ * The maximum depth to traverse (1-based).
63
+ * If 1, only the root is visited. If 0, the traversal is unlimited.
64
+ *
65
+ * @param {function} f_cb [in]
66
+ * The user-defined callback function called at each node.
67
+ * Expected signature: `f_cb(ao_TreeStack, i_depth, user)`
68
+ * - ao_TreeStack: The current traversal stack.
69
+ * - i_depth: The maximum depth limit passed to `sj_deep_walk`.
70
+ * - user: The user data provided.
71
+ * Must return `true` to continue or `false` to abort.
72
+ *
73
+ * @param {any} user [in]
74
+ * Arbitrary user data passed to the callback function.
75
+ *
76
+ * @returns {number}
77
+ * Status code defined in `go_tree_walk_ret`. Same as `sj_tree_walk`.
78
+ * @see sj_tree_walk
79
+ */
17
80
  export function sj_deep_walk(value, i_depth, f_cb, user)
18
81
  {
19
82
  let i_ret = go_tree_walk_ret.i_noncb;
@@ -39,7 +102,30 @@ export function sj_deep_walk(value, i_depth, f_cb, user)
39
102
  }
40
103
 
41
104
 
42
-
105
+ /**
106
+ * Callback function for `sj_tree_walk` to perform a deep traversal of an object.
107
+ *
108
+ * This function serves two primary purposes:
109
+ * 1. **Node Adaptation**: It wraps the next child value into a node object
110
+ * using `deep_walk_node_t` for the next step of traversal.
111
+ * 2. **User Callback Execution**: It triggers the user-provided callback
112
+ * (`o_user.f_cb`) in "leading order".
113
+ *
114
+ * @param {object[]} ao_TreeStack [in]
115
+ * The stack of tree nodes representing the current traversal state.
116
+ *
117
+ * @param {boolean} from_child [in]
118
+ * Any information returning from a child node. (Unused)
119
+ *
120
+ * @param {object} o_user [in]
121
+ * A context object containing:
122
+ * - i_depth: Maximum traversal depth (1-based, 0 for unlimited).
123
+ * - f_cb : The user-provided callback function to execute at each node.
124
+ * - user : Arbitrary user data to pass to `f_cb`.
125
+ *
126
+ * @returns {boolean}
127
+ * true to continue the traversal; otherwise false to abort.
128
+ */
43
129
  function deep_walk_cb(ao_TreeStack, from_child, o_user)
44
130
  {
45
131
  let b_continue = true;
@@ -57,7 +143,7 @@ function deep_walk_cb(ao_TreeStack, from_child, o_user)
57
143
  b_continue &&= (!!o_TreeStack.child);
58
144
  }
59
145
 
60
- if (b_continue && (o_TreeStack.i_child === 0))
146
+ if (b_continue && (o_TreeStack.i_child === 0)) /* If leading order */
61
147
  {
62
148
  b_continue = o_user.f_cb(ao_TreeStack, o_user.i_depth, o_user.user);
63
149
  }
@@ -65,7 +151,27 @@ function deep_walk_cb(ao_TreeStack, from_child, o_user)
65
151
  return b_continue;
66
152
  }
67
153
 
68
- // return path to ao_TreeStack[tail]
154
+
155
+ /**
156
+ * Retrieves the property path (array of keys/indices) leading to the current
157
+ * node in a deep traversal.
158
+ *
159
+ * This function reconstructs the path by iterating through the tree stack
160
+ * from the root up to the parent of the current node. It is designed to be
161
+ * called within a callback of `sj_deep_walk`.
162
+ *
163
+ * Since the last element of `ao_TreeStack` represents the current node itself,
164
+ * this function collects keys from all elements except the last to form the
165
+ * complete path.
166
+ *
167
+ * @param {object[]} ao_TreeStack [in]
168
+ * The stack of tree nodes representing the current traversal state.
169
+ *
170
+ * @returns {Array|null}
171
+ * An array of keys/indices representing the path to the current value;
172
+ * otherwise null if the stack is invalid or an internal error occurs.
173
+ * Note: Returns an empty array if the current node is the root.
174
+ */
69
175
  export function sj_deep_path(ao_TreeStack)
70
176
  {
71
177
  let as_path = sj_array_new(0);
@@ -12,6 +12,7 @@ export * from "./arraypush.js";
12
12
  export * from "./arraysplice.js";
13
13
  export * from "./dataset.js";
14
14
  export * from "./deep.js";
15
+ export * from "./deepwalk.js";
15
16
  export * from "./document.js";
16
17
  export * from "./error.js";
17
18
  export * from "./html.js";
@@ -127,6 +127,8 @@ export const go_tree_walk_ret = Object.freeze(
127
127
  * - go_tree_walk_ret.i_noncb : Provided f_cb is not a function.
128
128
  * - go_tree_walk_ret.i_stop : Traversal was explicitly stopped by the callback.
129
129
  * - go_tree_walk_ret.i_nostack: Failed to allocate or push to the traversal stack.
130
+ * - go_tree_walk_ret.i_fatal : Fatal error(not issued by this function, but should
131
+ * be handled by the caller).
130
132
  */
131
133
  export function sj_tree_walk(f_cb, root, user)
132
134
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smsmslib",
3
- "version": "1.0.79",
3
+ "version": "1.0.80",
4
4
  "description": "Reusable functions for me.",
5
5
  "files": [
6
6
  "javascr/**/*.js",
@@ -19,6 +19,6 @@
19
19
  "author": "",
20
20
  "license": "ISC",
21
21
  "dependencies": {
22
- "smsmslib": "^1.0.79"
22
+ "smsmslib": "^1.0.80"
23
23
  }
24
24
  }