@peter.naydenov/walk-async 6.0.0 → 7.0.0

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.
Files changed (42) hide show
  1. package/Changelog.md +34 -2
  2. package/Migration.guide.md +274 -0
  3. package/README.md +501 -311
  4. package/dist/walk-async.cjs +1 -1
  5. package/dist/walk-async.esm.mjs +1 -1
  6. package/dist/walk-async.umd.js +1 -1
  7. package/package.json +19 -8
  8. package/src/copyObject.js +145 -0
  9. package/src/createCallbackArgs.js +15 -0
  10. package/src/findType.js +28 -0
  11. package/src/finish.js +38 -0
  12. package/src/ignore.js +24 -0
  13. package/src/main.js +230 -285
  14. package/src/pass.js +36 -0
  15. package/src/runCallback.js +55 -0
  16. package/src/setKey.js +12 -0
  17. package/src/validateForInsertion.js +12 -0
  18. package/types/copyObject.d.ts +8 -0
  19. package/types/copyObject.d.ts.map +1 -0
  20. package/types/createCallbackArgs.d.ts +32 -0
  21. package/types/createCallbackArgs.d.ts.map +1 -0
  22. package/types/findType.d.ts +3 -0
  23. package/types/findType.d.ts.map +1 -0
  24. package/types/finish.d.ts +25 -0
  25. package/types/finish.d.ts.map +1 -0
  26. package/types/ignore.d.ts +10 -0
  27. package/types/ignore.d.ts.map +1 -0
  28. package/types/main.d.ts +147 -61
  29. package/types/main.d.ts.map +1 -1
  30. package/types/pass.d.ts +23 -0
  31. package/types/pass.d.ts.map +1 -0
  32. package/types/runCallback.d.ts +3 -0
  33. package/types/runCallback.d.ts.map +1 -0
  34. package/types/setKey.d.ts +3 -0
  35. package/types/setKey.d.ts.map +1 -0
  36. package/types/validateForInsertion.d.ts +3 -0
  37. package/types/validateForInsertion.d.ts.map +1 -0
  38. package/rollup.config.js +0 -41
  39. package/tsconfig.json +0 -11
  40. package/types/src/main.d.ts +0 -35
  41. package/types/src/main.d.ts.map +0 -1
  42. package/vitest.config.js +0 -8
@@ -0,0 +1,55 @@
1
+ "use strict"
2
+
3
+ import IGNORE from "./ignore.js";
4
+ import PASS, { isPass } from "./pass.js";
5
+ import FINISH, { isFinish } from "./finish.js";
6
+
7
+
8
+
9
+ async function runCallback ( callback, context, pending, name, ...args ) {
10
+ let
11
+ task
12
+ , done
13
+ , tag = `${name} at '${context.breadcrumbs ?? context.key}'`
14
+ ;
15
+
16
+ // Reading resolve or reject selects the v6 explicit-settlement API.
17
+ // Return-based callbacks need no extra settlement promise.
18
+ function explicit () {
19
+ if ( !task ) {
20
+ task = new Promise ( resolve => { done = resolve } )
21
+ // Observe rejected values immediately, even while the callback
22
+ // is still awaiting other work. The original rejection is
23
+ // propagated when task is awaited below.
24
+ task.catch ( () => {} )
25
+ }
26
+ } // explicit func.
27
+
28
+ Object.defineProperties ( context, {
29
+ resolve : {
30
+ enumerable : true
31
+ , get : () => { explicit (); return done }
32
+ }
33
+ , reject : {
34
+ enumerable : true
35
+ , get : () => { explicit (); return () => done ( IGNORE() ) }
36
+ }
37
+ })
38
+
39
+ pending.add ( tag )
40
+ try {
41
+ const returned = await callback ( context, ...args );
42
+ const value = task ? await task : returned;
43
+ // Keep the callback pending while an instruction replacement settles too.
44
+ if ( name === 'objectCallback' && isPass ( value ) && value.hasValue ) return PASS ( await value.value )
45
+ if ( isFinish ( value ) && value.hasValue ) return FINISH ( await value.value )
46
+ return value
47
+ }
48
+ finally {
49
+ pending.delete ( tag )
50
+ }
51
+ } // runCallback func.
52
+
53
+
54
+
55
+ export default runCallback
package/src/setKey.js ADDED
@@ -0,0 +1,12 @@
1
+ // Plain assignment of a '__proto__' key triggers the inherited setter and
2
+ // replaces the prototype of 'target' instead of creating an own property.
3
+ function setKey ( target, k, value ) {
4
+ if ( k === '__proto__' ) Object.defineProperty ( target, k, { value, enumerable:true, writable:true, configurable:true })
5
+ else target[k] = value
6
+ } // setKey func.
7
+
8
+
9
+
10
+ export default setKey
11
+
12
+
@@ -0,0 +1,12 @@
1
+ function validateForInsertion ( k, result ) {
2
+ if ( !Array.isArray ( result ) ) return false
3
+ const index = Number ( k );
4
+ // Array indexes are canonical decimal strings from 0 through 2^32 - 2.
5
+ return Number.isInteger ( index ) && index >= 0 && index < 4294967295 && String ( index ) === k
6
+ } // validateForInsertion func.
7
+
8
+
9
+
10
+ export default validateForInsertion
11
+
12
+
@@ -0,0 +1,8 @@
1
+ declare function copyObject(resource: any, result: any, extend: any, cb: any, breadcrumbs: any, settings: any, control: any, parentPath: any, pass: any, parent: any, ancestors: any, pending: any, ...args: any[]): Promise<{
2
+ data: any;
3
+ location: any;
4
+ parent: any;
5
+ depth: any;
6
+ } | undefined>;
7
+ export default copyObject;
8
+ //# sourceMappingURL=copyObject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copyObject.d.ts","sourceRoot":"","sources":["../src/copyObject.js"],"names":[],"mappings":"AAaA,iBAAe,UAAU,CAAG,QAAQ,KAAA,EAAE,MAAM,KAAA,EAAE,MAAM,KAAA,EAAE,EAAE,KAAA,EAAE,WAAW,KAAA,EAAE,QAAQ,KAAA,EAAE,OAAO,KAAA,EAAE,UAAU,KAAA,EAAE,IAAI,KAAA,EAAE,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,OAAO,KAAA,EAAE,GAAG,IAAI,OAAA;;;;;eAuG9I;eA4Bc,UAAU"}
@@ -0,0 +1,32 @@
1
+ import FINISH from "./finish.js";
2
+ declare function createCallbackArgs(value: any, key: any, IGNORE: any, breadcrumbs: any, parentPath: any, settings: any): {
3
+ value: any;
4
+ key: any;
5
+ breadcrumbs: any;
6
+ parentPath: any;
7
+ IGNORE: any;
8
+ FINISH: typeof FINISH;
9
+ } | {
10
+ value: any;
11
+ key: any;
12
+ breadcrumbs: any;
13
+ IGNORE: any;
14
+ FINISH: typeof FINISH;
15
+ parentPath?: undefined;
16
+ } | {
17
+ breadcrumbs?: undefined;
18
+ value: any;
19
+ key: any;
20
+ parentPath: any;
21
+ IGNORE: any;
22
+ FINISH: typeof FINISH;
23
+ } | {
24
+ breadcrumbs?: undefined;
25
+ parentPath?: undefined;
26
+ value: any;
27
+ key: any;
28
+ IGNORE: any;
29
+ FINISH: typeof FINISH;
30
+ };
31
+ export default createCallbackArgs;
32
+ //# sourceMappingURL=createCallbackArgs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createCallbackArgs.d.ts","sourceRoot":"","sources":["../src/createCallbackArgs.js"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAIjC,iBAAS,kBAAkB,CAAG,KAAK,KAAA,EAAE,GAAG,KAAA,EAAE,MAAM,KAAA,EAAE,WAAW,KAAA,EAAE,UAAU,KAAA,EAAE,QAAQ,KAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAKlF;eAIc,kBAAkB"}
@@ -0,0 +1,3 @@
1
+ declare function findType(x: any): "array" | "object" | "simple";
2
+ export default findType;
3
+ //# sourceMappingURL=findType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"findType.d.ts","sourceRoot":"","sources":["../src/findType.js"],"names":[],"mappings":"AAAA,iBAAS,QAAQ,CAAG,CAAC,KAAA,iCAoBnB;eAIc,QAAQ"}
@@ -0,0 +1,25 @@
1
+ export type FinishInstruction = {
2
+ value: any;
3
+ hasValue: boolean;
4
+ };
5
+ /**
6
+ * Private instruction created by the FINISH helper.
7
+ *
8
+ * @typedef {object} FinishInstruction
9
+ * @property {*} value
10
+ * @property {boolean} hasValue
11
+ */
12
+ /**
13
+ * Stop the entire walk. Omit the current value when no argument is
14
+ * supplied. From objectCallback, a supplied value selects the final
15
+ * branch for key callbacks with isFinished true. From keyCallback,
16
+ * include the supplied value directly and stop immediately.
17
+ *
18
+ * @param {*} [value] - Final value or branch.
19
+ * @returns {FinishInstruction}
20
+ */
21
+ declare function FINISH(value?: any): FinishInstruction;
22
+ declare function isFinish(value: any): boolean;
23
+ export { isFinish };
24
+ export default FINISH;
25
+ //# sourceMappingURL=finish.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finish.d.ts","sourceRoot":"","sources":["../src/finish.js"],"names":[],"mappings":"AAQI,YAAkB,iBAAiB,GACnC;IAAc,KAAK,EAAR,GAAC,CACZ;IAAoB,QAAQ,EAAjB,OAAO,CACrB;CAAA,CAAA;AAND;;;;;;GAMG;AAEH;;;;;;;;GAQG;AACH,iBAAS,MAAM,CAAG,KAAK,AAHnB,CACA,EADQ,GAGW,GAFT,iBAAiB,CAM9B;AAID,iBAAS,QAAQ,CAAG,KAAK,KAAA,WAExB;AAID,OAAO,EAAE,QAAQ,EAAE,CAAA;eACJ,MAAM"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Remove the current key or the entire current object/array branch.
3
+ *
4
+ * @returns {symbol}
5
+ */
6
+ declare function IGNORE(): symbol;
7
+ declare function isIgnore(value: any): boolean;
8
+ export { isIgnore };
9
+ export default IGNORE;
10
+ //# sourceMappingURL=ignore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ignore.d.ts","sourceRoot":"","sources":["../src/ignore.js"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,iBAAS,MAAM,IAFD,MAAM,CAInB;AAID,iBAAS,QAAQ,CAAG,KAAK,KAAA,WAExB;AAID,OAAO,EAAE,QAAQ,EAAE,CAAA;eACJ,MAAM"}
package/types/main.d.ts CHANGED
@@ -1,18 +1,9 @@
1
- /**
2
- * walk-async
3
- *
4
- * Alternative of deep-copy that provides much better control during creation of immutable
5
- * copies of javascript data structures.
6
- * Library is using 'generator functions'. If support for old browsers is required,
7
- * add a polyfill for 'generators'.
8
- *
9
- * History notes:
10
- * - Walk-async was born on September 18th, 2022
11
- * - Converted to ES module on January 1st, 2024
12
- *
13
- */
14
1
  export type Resolve = (value: any) => void;
15
2
  export type Reject = (reason?: any) => void;
3
+ export type IgnoreToken = symbol;
4
+ export type IgnoreFunction = () => IgnoreToken;
5
+ export type PassFunction = (value?: any) => import('./pass.js').PassInstruction;
6
+ export type FinishFunction = (value?: any) => import('./finish.js').FinishInstruction;
16
7
  export type CallbackArgs = {
17
8
  /**
18
9
  * - The current value being processed.
@@ -23,23 +14,59 @@ export type CallbackArgs = {
23
14
  */
24
15
  key: string;
25
16
  /**
26
- * - Slash-delimited path to the current key, starting with `root` (e.g. `"root/props/age"`).
17
+ * - Slash-delimited path including the current key. Absent when `settings.breadcrumbs` is false.
18
+ */
19
+ breadcrumbs?: string;
20
+ /**
21
+ * - Read-only path to the parent, excluding the current key. Absent when `settings.parentPath` is false.
27
22
  */
28
- breadcrumbs: string;
23
+ parentPath?: ReadonlyArray<string>;
29
24
  /**
30
- * - Resolve the callback with the new value (see `Resolve` typedef).
25
+ * - Reading this field opts into explicit settlement with resolve(value) or reject().
31
26
  */
32
27
  resolve: Resolve;
33
28
  /**
34
- * - Reject the callback to drop the current key (see `Reject` typedef).
29
+ * - Drop the current key or branch; does not reject the walk promise.
35
30
  */
36
31
  reject: Reject;
32
+ /**
33
+ * - Return FINISH() to omit the current value and stop, or FINISH(value) to include a final value or branch.
34
+ */
35
+ FINISH: FinishFunction;
36
+ /**
37
+ * - Return IGNORE() from the callback to drop the current key from the result.
38
+ */
39
+ IGNORE: IgnoreFunction;
40
+ };
41
+ export type KeyCallbackArgs = CallbackArgs & {
42
+ isFinished: boolean;
43
+ };
44
+ export type ObjectCallbackArgs = CallbackArgs & {
45
+ PASS: PassFunction;
46
+ };
47
+ export type KeyCallback = (args: KeyCallbackArgs, ...rest: any) => any | Promise<any>;
48
+ export type ObjectCallback = (args: ObjectCallbackArgs, ...rest: any) => any | Promise<any>;
49
+ export type Settings = {
50
+ /**
51
+ * - Defaults to true. Only false disables result creation; callbacks still run and walk resolves undefined.
52
+ */
53
+ copy?: boolean;
54
+ /**
55
+ * - Defaults to true. Only false disables breadcrumbs preparation.
56
+ */
57
+ breadcrumbs?: boolean;
58
+ /**
59
+ * - Defaults to true. Only false disables parent-path preparation.
60
+ */
61
+ parentPath?: boolean;
62
+ /**
63
+ * - Defaults to true. Only false disables circular-reference checks and their bookkeeping; visited data and replacements must then be acyclic or callbacks must prune cyclic branches.
64
+ */
65
+ detectCycles?: boolean;
37
66
  };
38
- export type KeyCallback = (args: CallbackArgs, ...rest: any) => void;
39
- export type ObjectCallback = (args: CallbackArgs, ...rest: any) => void;
40
67
  export type Options = {
41
68
  /**
42
- * - Required. Any JS data structure that will be copied.
69
+ * - Required. Any JS data structure that will be walked.
43
70
  */
44
71
  data: any;
45
72
  /**
@@ -51,97 +78,156 @@ export type Options = {
51
78
  */
52
79
  objectCallback?: ObjectCallback;
53
80
  /**
54
- * - Optional. Milliseconds. When set, the promise is rejected if callbacks do not resolve in time. Error lists the pending callbacks.
81
+ * - Optional milliseconds. Reject the walk with pending-callback diagnostics when time expires.
55
82
  */
56
83
  timeout?: number;
84
+ /**
85
+ * - Optional. Disable copying, unused callback metadata, or cycle detection with false.
86
+ */
87
+ settings?: Settings;
57
88
  };
58
89
  /**
59
- * Resolve the callback with the new value to store at the current key:
60
- * - resolving a primitive (or a built-in like `Date` / `Map` / `Set`) → stored as-is by reference;
61
- * - resolving a plain object or array → walk continues into it with the other callback applied to its children;
62
- *
63
- * You must call `resolve` or `reject` on every code path — otherwise the walk promise never settles
64
- * (use the `timeout` option to turn that silent hang into a rejection with diagnostics).
90
+ * Explicitly settle a callback with its current value or a replacement.
91
+ * Promises supplied to resolve are awaited before traversal continues.
65
92
  *
66
93
  * @callback Resolve
67
94
  * @param {*} value
68
95
  * @returns {void}
69
96
  */
70
97
  /**
71
- * Reject the callback. The current key is dropped from the result.
98
+ * Drop the current key or entire branch. Retained for v6 callbacks.
99
+ * To reject the walk itself, throw or return a rejected promise.
72
100
  *
73
101
  * @callback Reject
74
102
  * @param {*} [reason]
75
103
  * @returns {void}
76
104
  */
77
105
  /**
78
- * Arguments object received by both `keyCallback` and `objectCallback`.
106
+ * Private sentinel returned by IGNORE().
107
+ *
108
+ * @typedef {symbol} IgnoreToken
109
+ */
110
+ /**
111
+ * Call and return the result to drop the current key or entire branch.
112
+ *
113
+ * @callback IgnoreFunction
114
+ * @returns {IgnoreToken}
115
+ */
116
+ /**
117
+ * Call with no arguments to keep the current value, or pass a replacement.
118
+ * Supplied replacement promises are awaited before traversal continues.
119
+ * Return the instruction from `objectCallback` to skip `keyCallback` on
120
+ * its immediate properties. Nested objects and arrays continue normally.
121
+ *
122
+ * @callback PassFunction
123
+ * @param {*} [value]
124
+ * @returns {import('./pass.js').PassInstruction}
125
+ */
126
+ /**
127
+ * Stop the walk, optionally including a final value or branch.
128
+ * Supplied replacement promises are awaited before traversal continues.
129
+ *
130
+ * @callback FinishFunction
131
+ * @param {*} [value]
132
+ * @returns {import('./finish.js').FinishInstruction}
133
+ */
134
+ /**
135
+ * Shared arguments received by `keyCallback` and `objectCallback`.
79
136
  *
80
137
  * @typedef {object} CallbackArgs
81
138
  * @property {*} value - The current value being processed.
82
139
  * @property {string} key - Property key as a string.
83
- * @property {string} breadcrumbs - Slash-delimited path to the current key, starting with `root` (e.g. `"root/props/age"`).
84
- * @property {Resolve} resolve - Resolve the callback with the new value (see `Resolve` typedef).
85
- * @property {Reject} reject - Reject the callback to drop the current key (see `Reject` typedef).
140
+ * @property {string} [breadcrumbs] - Slash-delimited path including the current key. Absent when `settings.breadcrumbs` is false.
141
+ * @property {ReadonlyArray<string>} [parentPath] - Read-only path to the parent, excluding the current key. Absent when `settings.parentPath` is false.
142
+ * @property {Resolve} resolve - Reading this field opts into explicit settlement with resolve(value) or reject().
143
+ * @property {Reject} reject - Drop the current key or branch; does not reject the walk promise.
144
+ * @property {FinishFunction} FINISH - Return FINISH() to omit the current value and stop, or FINISH(value) to include a final value or branch.
145
+ * @property {IgnoreFunction} IGNORE - Return IGNORE() from the callback to drop the current key from the result.
146
+ */
147
+ /**
148
+ * @typedef {CallbackArgs & { isFinished: boolean }} KeyCallbackArgs
86
149
  */
87
150
  /**
88
- * Called once per primitive property (string, number, bigint, boolean,
151
+ * @typedef {CallbackArgs & { PASS: PassFunction }} ObjectCallbackArgs
152
+ */
153
+ /**
154
+ * Called once per primitive property unless its immediate parent returns
155
+ * `PASS()` or `PASS(value)` from `objectCallback`. Types: string, number, bigint, boolean,
89
156
  * symbol, null, undefined, function, Date, RegExp, Map, Set, WeakMap,
90
- * WeakSet, ArrayBuffer, DataView, typed arrays, DOM nodes).
157
+ * WeakSet, ArrayBuffer, DataView, typed arrays, DOM nodes.
91
158
  *
92
- * Resolve with the new value to store, or reject to drop the key:
93
- * - resolve a primitive (or a built-in like `Date` / `Map` / `Set`) → stored as-is by reference;
94
- * - resolve a plain object or array → walk continues into it with the other callback applied to its children;
95
- * - reject → that key is dropped from the result.
159
+ * Return the new value to store, or `IGNORE()` to drop the key:
160
+ * - return a primitive (or a built-in like `Date`/`Map`/`Set`) → stored as-is by reference;
161
+ * - return a plain object or array → walk continues into it with the other callback applied to its children;
162
+ * - return `IGNORE()` → that key is dropped from the result;
163
+ * - return `FINISH()` → omit the current key and stop the entire walk;
164
+ * - return `FINISH(value)` → include the supplied value directly and stop, without descending into it.
165
+ * isFinished is true for keys in a final branch selected by objectCallback.
166
+ * With settings.copy false, no values are stored; returned containers
167
+ * still control what gets visited.
96
168
  *
97
169
  * @callback KeyCallback
98
- * @param {CallbackArgs} args
170
+ * @param {KeyCallbackArgs} args
99
171
  * @param {...*} rest - Any extra arguments passed to `walk()` are forwarded to the callback.
100
- * @returns {void}
172
+ * @returns {* | Promise<*>}
101
173
  */
102
174
  /**
103
175
  * Called once per object or array property, including the root.
104
- * The resolved value becomes the new value at that key:
105
- * - resolve an object or array → walk continues into it with the other callbacks applied to its children;
106
- * - resolve a primitive → it is stored at that key as-is, and walk does not descend into it (primitives have no children to walk);
107
- * - reject → the key is dropped from the result.
176
+ * The returned value becomes the new value at that key:
177
+ * - return an object or array → walk continues into it with the other callbacks;
178
+ * - return a primitive → passed to `keyCallback` when active, otherwise stored as-is;
179
+ * - return `IGNORE()` → the key is dropped from the result.
180
+ * - return `PASS()` → keep the current object/array without its immediate key callbacks; nested callbacks continue normally.
181
+ * - return `PASS(value)` → use the replacement with the same rule; simple replacements are stored directly.
182
+ * - return `FINISH()` → omit the branch and stop the entire walk.
183
+ * - return `FINISH(value)` → process only the final branch with keyCallback and isFinished true, suppressing later object callbacks; without keyCallback, include it directly.
184
+ * With settings.copy false, returned values control traversal without
185
+ * being stored in a result or assigned to the source.
108
186
  *
109
187
  * @callback ObjectCallback
110
- * @param {CallbackArgs} args
188
+ * @param {ObjectCallbackArgs} args
111
189
  * @param {...*} rest
112
- * @returns {void}
190
+ * @returns {* | Promise<*>}
191
+ */
192
+ /**
193
+ * @typedef {object} Settings
194
+ * @property {boolean} [copy] - Defaults to true. Only false disables result creation; callbacks still run and walk resolves undefined.
195
+ * @property {boolean} [breadcrumbs] - Defaults to true. Only false disables breadcrumbs preparation.
196
+ * @property {boolean} [parentPath] - Defaults to true. Only false disables parent-path preparation.
197
+ * @property {boolean} [detectCycles] - Defaults to true. Only false disables circular-reference checks and their bookkeeping; visited data and replacements must then be acyclic or callbacks must prune cyclic branches.
113
198
  */
114
199
  /**
115
200
  * @typedef {object} Options
116
- * @property {*} data - Required. Any JS data structure that will be copied.
201
+ * @property {*} data - Required. Any JS data structure that will be walked.
117
202
  * @property {KeyCallback} [keyCallback] - Optional. Executed on each primitive property.
118
203
  * @property {ObjectCallback} [objectCallback] - Optional. Executed on each object/array property, including the root.
119
- * @property {number} [timeout] - Optional. Milliseconds. When set, the promise is rejected if callbacks do not resolve in time. Error lists the pending callbacks.
204
+ * @property {number} [timeout] - Optional milliseconds. Reject the walk with pending-callback diagnostics when time expires.
205
+ * @property {Settings} [settings] - Optional. Disable copying, unused callback metadata, or cycle detection with false.
120
206
  */
121
207
  /**
122
208
  * Walk-async
123
209
  *
124
- * Async sibling of `@peter.naydenov/walk`. Visits every member of a deep
125
- * JavaScript data structure once, in a single pass, and runs two optional
126
- * callbacks during the visit that can mask, filter, or substitute values
127
- * as the result is built.
128
- *
129
- * The deep copy of the original data is mostly a side-effect: the real
130
- * power is the ability to do all your modifications in one pass, with full
131
- * freedom inside a single callback — now with async work, awaits, and
132
- * timeouts supported.
210
+ * Walks through a deep JavaScript data structure, building a copy by default.
211
+ * Two optional callbacks can mask, filter, substitute, or collect values.
212
+ * Callback results are awaited in traversal order; thrown errors and rejected promises reject the walk.
213
+ * Set settings.copy to false to walk without building a result.
214
+ * By default, circular references point to the ancestor copy without visiting its contents again.
133
215
  *
134
216
  * @function walk
135
- * @param {Options} options - Required. Object with required `data` property, two optional callback functions (`keyCallback`, `objectCallback`), and an optional `timeout`.
217
+ * @param {Options} options - Required. Object with `data`, optional callbacks, and optional `settings`.
136
218
  * @param {...*} args - Optional. Additional arguments forwarded to both callbacks.
137
- * @returns {Promise<*>} - A promise that resolves to the immutable copy of `options.data` (with the callbacks' transformations applied).
219
+ * @returns {Promise<*>} - Resolves to the created result, or undefined when settings.copy is false.
138
220
  * @example
139
- * const result = await walk ({
221
+ * let result = await walk ({
140
222
  * data: someData,
141
223
  * keyCallback: keyCallbackFn,
142
224
  * objectCallback: objectCallbackFn
143
225
  * })
226
+ *
227
+ * // Note: objectCallback is executed before keyCallback.
228
+ * // If you modify an object with objectCallback, keyCallback will be
229
+ * // executed on the result of objectCallback.
144
230
  */
145
- declare function walk({ data: origin, objectCallback, keyCallback, timeout }: Options, ...args: any[]): Promise<any>;
231
+ declare function walk(options: Options, ...args: any[]): Promise<any>;
146
232
  export default walk;
147
233
  //# sourceMappingURL=main.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.js"],"names":[],"mappings":"AACA;;;;;;;;;;;;GAYG;AAaC,YAAU,OAAO,GACjB,CAAW,KAAK,EAAR,GACR,KAAU,IACb,CAAA;AAKG,YAAU,MAAM,GAChB,CAAY,MAAM,AAAlB,CACA,EADQ,GACR,KAAU,IACb,CAAA;AAKG,YAAkB,YAAY,GAC9B;;;;IAAuB,KAAK,EAAjB,GAAC,CACZ;;;;IAAuB,GAAG,EAAf,MAAM,CACjB;;;;IAAuB,WAAW,EAAvB,MAAM,CACjB;;;;IAAu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;IAA4B,OAAO,AAAnC,CACA,EADW,MAAM,CACjB;;;;IAA4B,QAAQ,AAApC,CACH,EADc,QAAQ,CACtB;CAAA,CAAA;AAjID;;;;;;;GAOG;AAEH;;;;;;;GAOG;AAEH;;;;GAIG;AAEH;;;;;GAKG;AAEH;;;;;;;;;GASG;AAEH;;;;;;;GAOG;AAEH;;;;;;;;;;;;GAYG;AAEH;;GAEG;AAEH;;GAEG;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;;;;GAMG;AAEH;;;;;;;GAOG;AAGH;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,iBAAe,IAAI,CAAE,OAAO,EAdhB,OAcgB,EAAC,GAAG,IAAI,EAbrB,GAAC,EAaoB,GAZtB,OAAO,CAAC,GAAC,CAAC,CA8BvB;eAmHc,IAAI"}
@@ -0,0 +1,23 @@
1
+ export type PassInstruction = {
2
+ value: any;
3
+ hasValue: boolean;
4
+ };
5
+ /**
6
+ * Private instruction created by the PASS helper.
7
+ *
8
+ * @typedef {object} PassInstruction
9
+ * @property {*} value
10
+ * @property {boolean} hasValue
11
+ */
12
+ /**
13
+ * Use the current value, or a supplied replacement, without its
14
+ * immediate key callbacks. Nested object callbacks continue normally.
15
+ *
16
+ * @param {*} [value] - Replacement value. Omit to keep the current value.
17
+ * @returns {PassInstruction}
18
+ */
19
+ declare function PASS(value?: any): PassInstruction;
20
+ declare function isPass(value: any): boolean;
21
+ export { isPass };
22
+ export default PASS;
23
+ //# sourceMappingURL=pass.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pass.d.ts","sourceRoot":"","sources":["../src/pass.js"],"names":[],"mappings":"AAQI,YAAkB,eAAe,GACjC;IAAc,KAAK,EAAR,GAAC,CACZ;IAAoB,QAAQ,EAAjB,OAAO,CACrB;CAAA,CAAA;AAND;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,iBAAS,IAAI,CAAG,KAAK,AAHjB,CACA,EADQ,GAGS,GAFP,eAAe,CAM5B;AAID,iBAAS,MAAM,CAAG,KAAK,KAAA,WAEtB;AAID,OAAO,EAAE,MAAM,EAAE,CAAA;eACF,IAAI"}
@@ -0,0 +1,3 @@
1
+ declare function runCallback(callback: any, context: any, pending: any, name: any, ...args: any[]): Promise<any>;
2
+ export default runCallback;
3
+ //# sourceMappingURL=runCallback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runCallback.d.ts","sourceRoot":"","sources":["../src/runCallback.js"],"names":[],"mappings":"AAQA,iBAAe,WAAW,CAAG,QAAQ,KAAA,EAAE,OAAO,KAAA,EAAE,OAAO,KAAA,EAAE,IAAI,KAAA,EAAE,GAAG,IAAI,OAAA,gBA0CrE;eAIc,WAAW"}
@@ -0,0 +1,3 @@
1
+ declare function setKey(target: any, k: any, value: any): void;
2
+ export default setKey;
3
+ //# sourceMappingURL=setKey.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setKey.d.ts","sourceRoot":"","sources":["../src/setKey.js"],"names":[],"mappings":"AAEA,iBAAS,MAAM,CAAG,MAAM,KAAA,EAAE,CAAC,KAAA,EAAE,KAAK,KAAA,QAGjC;eAIc,MAAM"}
@@ -0,0 +1,3 @@
1
+ declare function validateForInsertion(k: any, result: any): boolean;
2
+ export default validateForInsertion;
3
+ //# sourceMappingURL=validateForInsertion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateForInsertion.d.ts","sourceRoot":"","sources":["../src/validateForInsertion.js"],"names":[],"mappings":"AAAA,iBAAS,oBAAoB,CAAG,CAAC,KAAA,EAAE,MAAM,KAAA,WAKxC;eAIc,oBAAoB"}
package/rollup.config.js DELETED
@@ -1,41 +0,0 @@
1
- import resolve from '@rollup/plugin-node-resolve'
2
- import commonjs from '@rollup/plugin-commonjs'
3
- import terser from '@rollup/plugin-terser';
4
-
5
-
6
- export default [
7
- // browser-friendly UMD build
8
- {
9
- input: 'src/main.js',
10
- output: {
11
- name: 'walkAsync',
12
- file: 'dist/walk-async.umd.js',
13
- format: 'umd',
14
- globals: {
15
- 'ask-for-promise': 'askForPromise'
16
- }
17
- },
18
- external : [ 'ask-for-promise' ],
19
- plugins: [
20
- resolve(), // so Rollup can find `ms`
21
- commonjs() // so Rollup can convert `ms` to an ES module
22
- , terser()
23
- ]
24
- },
25
-
26
- // CommonJS (for Node) and ES module (for bundlers) build.
27
- // (We could have three entries in the configuration array
28
- // instead of two, but it's quicker to generate multiple
29
- // builds from a single configuration where possible, using
30
- // an array for the `output` option, where we can specify
31
- // `file` and `format` for each target)
32
- {
33
- input: 'src/main.js',
34
- external: ['ask-for-promise'],
35
- output: [
36
- { file: 'dist/walk-async.cjs' , format: 'cjs' },
37
- { file: 'dist/walk-async.esm.mjs', format: 'es' }
38
- ],
39
- plugins: [ terser() ]
40
- }
41
- ];
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "include": ["./src/**/*"],
3
- "compilerOptions": {
4
- "allowJs": true,
5
- "declaration": true,
6
- "emitDeclarationOnly": true,
7
- "rootDir": "./src",
8
- "outDir": "types",
9
- "declarationMap": true
10
- }
11
- }
@@ -1,35 +0,0 @@
1
- export default walk;
2
- export type Options = {
3
- /**
4
- * - The data structure to be copied.
5
- */
6
- data: Object;
7
- /**
8
- * - A function called for every object in the data structure.
9
- */
10
- objectCallback: Function | null;
11
- /**
12
- * - A function called for every property in the data structure.
13
- */
14
- keyCallback: Function | null;
15
- };
16
- /**
17
- * @typedef {Object} Options
18
- * @property {Object} data - The data structure to be copied.
19
- * @property {Function|null} objectCallback - A function called for every object in the data structure.
20
- * @property {Function|null} keyCallback - A function called for every property in the data structure.
21
- */
22
- /**
23
- * Walk-async is a deep copy function that executes callback functions on every object and property.
24
- * Callbacks are async functions.
25
- *
26
- * @param {Options} options - Object with following properties:
27
- * - data - The data structure to be copied.
28
- * - objectCallback - A function that will be called for every object in the data structure.
29
- * - keyCallback - A function that will be called for every property in the data structure.
30
- * @param {...any} args - Any additional arguments will be passed to the callback functions.
31
- *
32
- * @returns {Promise} - A promise that resolves to the immutable copy of the data structure.
33
- */
34
- declare function walk({ data: origin, objectCallback, keyCallback }: Options, ...args: any[]): Promise<any>;
35
- //# sourceMappingURL=main.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/main.js"],"names":[],"mappings":";;;;;UAoBc,MAAM;;;;oBACN,WAAS,IAAI;;;;iBACb,WAAS,IAAI;;AAJ3B;;;;;GAKG;AAKH;;;;;;;;;;;GAWG;AACH,qEARW,OAAO,WAIJ,GAAG,EAAA,gBAmChB"}
package/vitest.config.js DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'vitest/config'
2
-
3
-
4
- export default defineConfig ({
5
- test : {
6
- globals : true
7
- }
8
- })