@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.
- package/Changelog.md +34 -2
- package/Migration.guide.md +274 -0
- package/README.md +501 -311
- package/dist/walk-async.cjs +1 -1
- package/dist/walk-async.esm.mjs +1 -1
- package/dist/walk-async.umd.js +1 -1
- package/package.json +19 -8
- package/src/copyObject.js +145 -0
- package/src/createCallbackArgs.js +15 -0
- package/src/findType.js +28 -0
- package/src/finish.js +38 -0
- package/src/ignore.js +24 -0
- package/src/main.js +230 -285
- package/src/pass.js +36 -0
- package/src/runCallback.js +55 -0
- package/src/setKey.js +12 -0
- package/src/validateForInsertion.js +12 -0
- package/types/copyObject.d.ts +8 -0
- package/types/copyObject.d.ts.map +1 -0
- package/types/createCallbackArgs.d.ts +32 -0
- package/types/createCallbackArgs.d.ts.map +1 -0
- package/types/findType.d.ts +3 -0
- package/types/findType.d.ts.map +1 -0
- package/types/finish.d.ts +25 -0
- package/types/finish.d.ts.map +1 -0
- package/types/ignore.d.ts +10 -0
- package/types/ignore.d.ts.map +1 -0
- package/types/main.d.ts +147 -61
- package/types/main.d.ts.map +1 -1
- package/types/pass.d.ts +23 -0
- package/types/pass.d.ts.map +1 -0
- package/types/runCallback.d.ts +3 -0
- package/types/runCallback.d.ts.map +1 -0
- package/types/setKey.d.ts +3 -0
- package/types/setKey.d.ts.map +1 -0
- package/types/validateForInsertion.d.ts +3 -0
- package/types/validateForInsertion.d.ts.map +1 -0
- package/rollup.config.js +0 -41
- package/tsconfig.json +0 -11
- package/types/src/main.d.ts +0 -35
- package/types/src/main.d.ts.map +0 -1
- package/vitest.config.js +0 -8
package/dist/walk-async.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";function e(e){return null==e||e.nodeType?"simple":Array.isArray(e)?"array":"object"==typeof e?e instanceof Date||e instanceof RegExp||e instanceof Map||e instanceof Set||e instanceof WeakMap||e instanceof WeakSet||e instanceof ArrayBuffer||e instanceof DataView||ArrayBuffer.isView(e)?"simple":"object":"simple"}function t(e,t){if(!Array.isArray(t))return!1;const a=Number(e);return Number.isInteger(a)&&a>=0&&a<4294967295&&String(a)===e}function a(e,t,a){"__proto__"===t?Object.defineProperty(e,t,{value:a,enumerable:!0,writable:!0,configurable:!0}):e[t]=a}const n=new WeakSet;function r(e){const t=Object.freeze({value:e,hasValue:arguments.length>0});return n.add(t),t}function i(e){return n.has(e)}function o(e,t,a,n,i,o){return o.breadcrumbs&&o.parentPath?{value:e,key:t,breadcrumbs:n,parentPath:i,IGNORE:a,FINISH:r}:o.breadcrumbs?{value:e,key:t,breadcrumbs:n,IGNORE:a,FINISH:r}:o.parentPath?{value:e,key:t,parentPath:i,IGNORE:a,FINISH:r}:{value:e,key:t,IGNORE:a,FINISH:r}}const c=new WeakSet;function s(e){const t=Object.freeze({value:e,hasValue:arguments.length>0});return c.add(t),t}function u(e){return c.has(e)}const l=Symbol("ignore___");function f(){return l}function p(e){return e===l}async function d(e,t,a,n,...o){let c,l,p=`${n} at '${t.breadcrumbs??t.key}'`;function d(){c||(c=new Promise(e=>{l=e}),c.catch(()=>{}))}Object.defineProperties(t,{resolve:{enumerable:!0,get:()=>(d(),l)},reject:{enumerable:!0,get:()=>(d(),()=>l(f()))}}),a.add(p);try{const a=await e(t,...o),l=c?await c:a;return"objectCallback"===n&&u(l)&&l.hasValue?s(await l.value):i(l)&&l.hasValue?r(await l.value):l}finally{a.delete(p)}}async function y(n,r,c,l,y,v,k,w,j,P,g,S,...I){let[O,C]=l,F=Object.keys(n),V=v.detectCycles?{data:n,location:r,parent:P,depth:P?P.depth+1:0}:void 0,N=!1;g&&g.set(n,V);for(let l=0;l<F.length&&!N;l++){const P=F[l];let A=n[P],$=e(A),_=v.copy&&t(P,r),E=v.breadcrumbs&&("simple"!==$||!j&&O)?`${y}/${P}`:void 0,z=!1;if("simple"!==$&&C&&!k.isFinished){const t=o(A,P,f,E,w,v);t.PASS=s;const a=await d(C,t,S,"objectCallback",...I);if(i(a)){if(!a.hasValue||!O)return b(a,r,P,_,v,k),V;c.length=0,k.restart=!0,k.isFinished=!0,N=!0,A=a.value}else{if(p(a))continue;u(a)?(z=!0,a.hasValue&&(A=a.value)):A=a}$=e(A)}if("simple"===$){if(z||j&&!k.isFinished||!O){if(!v.copy)continue;_?r.push(A):a(r,P,A);continue}const t=o(A,P,f,E,w,v);t.isFinished=k.isFinished;let n=await d(O,t,S,"keyCallback",...I);if(i(n))return b(n,r,P,_,v,k),V;if(p(n))continue;const c=e(n);if("simple"===c){if(!v.copy)continue;_?r.push(n):a(r,P,n);continue}A=n,$=c}const R=v.detectCycles?m(A,V,g):void 0;if(R){if(!v.copy)continue;_?r.push(R.location):a(r,P,R.location)}else{if("object"===$){const e=v.copy?{}:void 0;v.copy&&(_?r.push(e):a(r,P,e)),c.push(h(A,e,E,w,P,z,V))}if("array"===$){const e=v.copy?[]:void 0;v.copy&&(_?r.push(e):a(r,P,e)),c.push(h(A,e,E,w,P,z,V))}}}return V}function b(e,t,n,r,i,o){o.finished=!0,i.copy&&e.hasValue&&(r?t.push(e.value):a(t,n,e.value))}function h(e,t,a,n,r,i,o){return{data:e,location:t,breadcrumbs:a,parentPath:n,key:r,pass:i,parent:o}}function m(e,t,a){if(a)return a.get(e);for(let a=t;a;a=a.parent)if(a.data===e)return a}function v(e,t,a){if(!a){if(t.depth<32)return;a=new WeakMap;for(let e=t;e;e=e.parent)a.set(e.data,e);return a}const n=[];for(;e!==t;)e.depth>=t.depth?(a.delete(e.data),e=e.parent):(n.push(t),t=t.parent);for(let e=n.length-1;e>=0;e--){const t=n[e];a.set(t.data,t)}return a}module.exports=async function(t,...a){const n=new Set,r=async function(t,a,...n){let r,c,l,{data:b,keyCallback:h,objectCallback:m,settings:k}=t,w=e(b),j=[],P=!(!h&&!m),g={copy:!1!==k?.copy,breadcrumbs:P&&!1!==k?.breadcrumbs,parentPath:P&&!1!==k?.parentPath,detectCycles:!1!==k?.detectCycles},S=g.breadcrumbs?"root":void 0,I=g.parentPath&&"simple"!==w?Object.freeze(["root"]):void 0,O=!1,C={finished:!1,isFinished:!1,restart:!1},F=[h,m];if("simple"!==w&&m){const t=o(b,"root",f,S,g.parentPath?Object.freeze([]):void 0,g);t.PASS=s;const r=await d(m,t,a,"objectCallback",...n);if(i(r)){if(!r.hasValue){if(!g.copy)return;return"array"===w?[]:{}}if(!h)return g.copy?r.value:void 0;C.isFinished=!0,b=r.value}else{if(p(r)){if(!g.copy)return;return"array"===w?[]:{}}u(r)?(O=!0,r.hasValue&&(b=r.value)):b=r}w=e(b)}switch(w){case"array":r=g.copy?[]:void 0,l=await y(b,r,j,F,S,g,C,I,O,void 0,c,a,...n);break;case"object":r=g.copy?{}:void 0,l=await y(b,r,j,F,S,g,C,I,O,void 0,c,a,...n);break;case"simple":return g.copy?b:void 0}for(let e=0;e<j.length&&!C.finished;){const t=j[e];j[e]=void 0,C.restart=!1,g.detectCycles&&(c=v(l,t.parent,c));const r=g.parentPath?Object.freeze([...t.parentPath,t.key]):void 0;l=await y(t.data,t.location,j,F,t.breadcrumbs,g,C,r,t.pass,t.parent,c,a,...n),e=C.restart?0:e+1}return r}(t,n,...a);if(null==t.timeout)return r;let c;const l=new Promise((e,a)=>{c=setTimeout(()=>{const e=[...n].map(e=>`\n - ${e}`).join("");a(new Error(`walk-async: timed out after ${t.timeout}ms; callbacks still pending:${e}`))},t.timeout)});try{return await Promise.race([r,l])}finally{clearTimeout(c)}};
|
package/dist/walk-async.esm.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
function e(e){return null==e||e.nodeType?"simple":Array.isArray(e)?"array":"object"==typeof e?e instanceof Date||e instanceof RegExp||e instanceof Map||e instanceof Set||e instanceof WeakMap||e instanceof WeakSet||e instanceof ArrayBuffer||e instanceof DataView||ArrayBuffer.isView(e)?"simple":"object":"simple"}function t(e,t){if(!Array.isArray(t))return!1;const a=Number(e);return Number.isInteger(a)&&a>=0&&a<4294967295&&String(a)===e}function a(e,t,a){"__proto__"===t?Object.defineProperty(e,t,{value:a,enumerable:!0,writable:!0,configurable:!0}):e[t]=a}const n=new WeakSet;function r(e){const t=Object.freeze({value:e,hasValue:arguments.length>0});return n.add(t),t}function i(e){return n.has(e)}function o(e,t,a,n,i,o){return o.breadcrumbs&&o.parentPath?{value:e,key:t,breadcrumbs:n,parentPath:i,IGNORE:a,FINISH:r}:o.breadcrumbs?{value:e,key:t,breadcrumbs:n,IGNORE:a,FINISH:r}:o.parentPath?{value:e,key:t,parentPath:i,IGNORE:a,FINISH:r}:{value:e,key:t,IGNORE:a,FINISH:r}}const c=new WeakSet;function s(e){const t=Object.freeze({value:e,hasValue:arguments.length>0});return c.add(t),t}function u(e){return c.has(e)}const l=Symbol("ignore___");function f(){return l}function p(e){return e===l}async function d(e,t,a,n,...o){let c,l,p=`${n} at '${t.breadcrumbs??t.key}'`;function d(){c||(c=new Promise(e=>{l=e}),c.catch(()=>{}))}Object.defineProperties(t,{resolve:{enumerable:!0,get:()=>(d(),l)},reject:{enumerable:!0,get:()=>(d(),()=>l(f()))}}),a.add(p);try{const a=await e(t,...o),l=c?await c:a;return"objectCallback"===n&&u(l)&&l.hasValue?s(await l.value):i(l)&&l.hasValue?r(await l.value):l}finally{a.delete(p)}}async function y(n,r,c,l,y,v,k,w,j,P,g,S,...I){let[O,C]=l,F=Object.keys(n),V=v.detectCycles?{data:n,location:r,parent:P,depth:P?P.depth+1:0}:void 0,N=!1;g&&g.set(n,V);for(let l=0;l<F.length&&!N;l++){const P=F[l];let A=n[P],$=e(A),_=v.copy&&t(P,r),E=v.breadcrumbs&&("simple"!==$||!j&&O)?`${y}/${P}`:void 0,z=!1;if("simple"!==$&&C&&!k.isFinished){const t=o(A,P,f,E,w,v);t.PASS=s;const a=await d(C,t,S,"objectCallback",...I);if(i(a)){if(!a.hasValue||!O)return b(a,r,P,_,v,k),V;c.length=0,k.restart=!0,k.isFinished=!0,N=!0,A=a.value}else{if(p(a))continue;u(a)?(z=!0,a.hasValue&&(A=a.value)):A=a}$=e(A)}if("simple"===$){if(z||j&&!k.isFinished||!O){if(!v.copy)continue;_?r.push(A):a(r,P,A);continue}const t=o(A,P,f,E,w,v);t.isFinished=k.isFinished;let n=await d(O,t,S,"keyCallback",...I);if(i(n))return b(n,r,P,_,v,k),V;if(p(n))continue;const c=e(n);if("simple"===c){if(!v.copy)continue;_?r.push(n):a(r,P,n);continue}A=n,$=c}const R=v.detectCycles?m(A,V,g):void 0;if(R){if(!v.copy)continue;_?r.push(R.location):a(r,P,R.location)}else{if("object"===$){const e=v.copy?{}:void 0;v.copy&&(_?r.push(e):a(r,P,e)),c.push(h(A,e,E,w,P,z,V))}if("array"===$){const e=v.copy?[]:void 0;v.copy&&(_?r.push(e):a(r,P,e)),c.push(h(A,e,E,w,P,z,V))}}}return V}function b(e,t,n,r,i,o){o.finished=!0,i.copy&&e.hasValue&&(r?t.push(e.value):a(t,n,e.value))}function h(e,t,a,n,r,i,o){return{data:e,location:t,breadcrumbs:a,parentPath:n,key:r,pass:i,parent:o}}function m(e,t,a){if(a)return a.get(e);for(let a=t;a;a=a.parent)if(a.data===e)return a}async function v(t,...a){const n=new Set,r=async function(t,a,...n){let r,c,l,{data:b,keyCallback:h,objectCallback:m,settings:v}=t,w=e(b),j=[],P=!(!h&&!m),g={copy:!1!==v?.copy,breadcrumbs:P&&!1!==v?.breadcrumbs,parentPath:P&&!1!==v?.parentPath,detectCycles:!1!==v?.detectCycles},S=g.breadcrumbs?"root":void 0,I=g.parentPath&&"simple"!==w?Object.freeze(["root"]):void 0,O=!1,C={finished:!1,isFinished:!1,restart:!1},F=[h,m];if("simple"!==w&&m){const t=o(b,"root",f,S,g.parentPath?Object.freeze([]):void 0,g);t.PASS=s;const r=await d(m,t,a,"objectCallback",...n);if(i(r)){if(!r.hasValue){if(!g.copy)return;return"array"===w?[]:{}}if(!h)return g.copy?r.value:void 0;C.isFinished=!0,b=r.value}else{if(p(r)){if(!g.copy)return;return"array"===w?[]:{}}u(r)?(O=!0,r.hasValue&&(b=r.value)):b=r}w=e(b)}switch(w){case"array":r=g.copy?[]:void 0,l=await y(b,r,j,F,S,g,C,I,O,void 0,c,a,...n);break;case"object":r=g.copy?{}:void 0,l=await y(b,r,j,F,S,g,C,I,O,void 0,c,a,...n);break;case"simple":return g.copy?b:void 0}for(let e=0;e<j.length&&!C.finished;){const t=j[e];j[e]=void 0,C.restart=!1,g.detectCycles&&(c=k(l,t.parent,c));const r=g.parentPath?Object.freeze([...t.parentPath,t.key]):void 0;l=await y(t.data,t.location,j,F,t.breadcrumbs,g,C,r,t.pass,t.parent,c,a,...n),e=C.restart?0:e+1}return r}(t,n,...a);if(null==t.timeout)return r;let c;const l=new Promise((e,a)=>{c=setTimeout(()=>{const e=[...n].map(e=>`\n - ${e}`).join("");a(new Error(`walk-async: timed out after ${t.timeout}ms; callbacks still pending:${e}`))},t.timeout)});try{return await Promise.race([r,l])}finally{clearTimeout(c)}}function k(e,t,a){if(!a){if(t.depth<32)return;a=new WeakMap;for(let e=t;e;e=e.parent)a.set(e.data,e);return a}const n=[];for(;e!==t;)e.depth>=t.depth?(a.delete(e.data),e=e.parent):(n.push(t),t=t.parent);for(let e=n.length-1;e>=0;e--){const t=n[e];a.set(t.data,t)}return a}export{v as default};
|
package/dist/walk-async.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).walkAsync=t()}(this,function(){"use strict";function e(e){return null==e||e.nodeType?"simple":Array.isArray(e)?"array":"object"==typeof e?e instanceof Date||e instanceof RegExp||e instanceof Map||e instanceof Set||e instanceof WeakMap||e instanceof WeakSet||e instanceof ArrayBuffer||e instanceof DataView||ArrayBuffer.isView(e)?"simple":"object":"simple"}function t(e,t){if(!Array.isArray(t))return!1;const n=Number(e);return Number.isInteger(n)&&n>=0&&n<4294967295&&String(n)===e}function n(e,t,n){"__proto__"===t?Object.defineProperty(e,t,{value:n,enumerable:!0,writable:!0,configurable:!0}):e[t]=n}const a=new WeakSet;function r(e){const t=Object.freeze({value:e,hasValue:arguments.length>0});return a.add(t),t}function i(e){return a.has(e)}function o(e,t,n,a,i,o){return o.breadcrumbs&&o.parentPath?{value:e,key:t,breadcrumbs:a,parentPath:i,IGNORE:n,FINISH:r}:o.breadcrumbs?{value:e,key:t,breadcrumbs:a,IGNORE:n,FINISH:r}:o.parentPath?{value:e,key:t,parentPath:i,IGNORE:n,FINISH:r}:{value:e,key:t,IGNORE:n,FINISH:r}}const c=new WeakSet;function s(e){const t=Object.freeze({value:e,hasValue:arguments.length>0});return c.add(t),t}function u(e){return c.has(e)}const l=Symbol("ignore___");function f(){return l}function p(e){return e===l}async function d(e,t,n,a,...o){let c,l,p=`${a} at '${t.breadcrumbs??t.key}'`;function d(){c||(c=new Promise(e=>{l=e}),c.catch(()=>{}))}Object.defineProperties(t,{resolve:{enumerable:!0,get:()=>(d(),l)},reject:{enumerable:!0,get:()=>(d(),()=>l(f()))}}),n.add(p);try{const n=await e(t,...o),l=c?await c:n;return"objectCallback"===a&&u(l)&&l.hasValue?s(await l.value):i(l)&&l.hasValue?r(await l.value):l}finally{n.delete(p)}}async function y(a,r,c,l,y,v,k,w,j,g,P,S,...I){let[O,C]=l,F=Object.keys(a),V=v.detectCycles?{data:a,location:r,parent:g,depth:g?g.depth+1:0}:void 0,N=!1;P&&P.set(a,V);for(let l=0;l<F.length&&!N;l++){const g=F[l];let A=a[g],$=e(A),_=v.copy&&t(g,r),E=v.breadcrumbs&&("simple"!==$||!j&&O)?`${y}/${g}`:void 0,z=!1;if("simple"!==$&&C&&!k.isFinished){const t=o(A,g,f,E,w,v);t.PASS=s;const n=await d(C,t,S,"objectCallback",...I);if(i(n)){if(!n.hasValue||!O)return b(n,r,g,_,v,k),V;c.length=0,k.restart=!0,k.isFinished=!0,N=!0,A=n.value}else{if(p(n))continue;u(n)?(z=!0,n.hasValue&&(A=n.value)):A=n}$=e(A)}if("simple"===$){if(z||j&&!k.isFinished||!O){if(!v.copy)continue;_?r.push(A):n(r,g,A);continue}const t=o(A,g,f,E,w,v);t.isFinished=k.isFinished;let a=await d(O,t,S,"keyCallback",...I);if(i(a))return b(a,r,g,_,v,k),V;if(p(a))continue;const c=e(a);if("simple"===c){if(!v.copy)continue;_?r.push(a):n(r,g,a);continue}A=a,$=c}const R=v.detectCycles?m(A,V,P):void 0;if(R){if(!v.copy)continue;_?r.push(R.location):n(r,g,R.location)}else{if("object"===$){const e=v.copy?{}:void 0;v.copy&&(_?r.push(e):n(r,g,e)),c.push(h(A,e,E,w,g,z,V))}if("array"===$){const e=v.copy?[]:void 0;v.copy&&(_?r.push(e):n(r,g,e)),c.push(h(A,e,E,w,g,z,V))}}}return V}function b(e,t,a,r,i,o){o.finished=!0,i.copy&&e.hasValue&&(r?t.push(e.value):n(t,a,e.value))}function h(e,t,n,a,r,i,o){return{data:e,location:t,breadcrumbs:n,parentPath:a,key:r,pass:i,parent:o}}function m(e,t,n){if(n)return n.get(e);for(let n=t;n;n=n.parent)if(n.data===e)return n}function v(e,t,n){if(!n){if(t.depth<32)return;n=new WeakMap;for(let e=t;e;e=e.parent)n.set(e.data,e);return n}const a=[];for(;e!==t;)e.depth>=t.depth?(n.delete(e.data),e=e.parent):(a.push(t),t=t.parent);for(let e=a.length-1;e>=0;e--){const t=a[e];n.set(t.data,t)}return n}return async function(t,...n){const a=new Set,r=async function(t,n,...a){let r,c,l,{data:b,keyCallback:h,objectCallback:m,settings:k}=t,w=e(b),j=[],g=!(!h&&!m),P={copy:!1!==k?.copy,breadcrumbs:g&&!1!==k?.breadcrumbs,parentPath:g&&!1!==k?.parentPath,detectCycles:!1!==k?.detectCycles},S=P.breadcrumbs?"root":void 0,I=P.parentPath&&"simple"!==w?Object.freeze(["root"]):void 0,O=!1,C={finished:!1,isFinished:!1,restart:!1},F=[h,m];if("simple"!==w&&m){const t=o(b,"root",f,S,P.parentPath?Object.freeze([]):void 0,P);t.PASS=s;const r=await d(m,t,n,"objectCallback",...a);if(i(r)){if(!r.hasValue){if(!P.copy)return;return"array"===w?[]:{}}if(!h)return P.copy?r.value:void 0;C.isFinished=!0,b=r.value}else{if(p(r)){if(!P.copy)return;return"array"===w?[]:{}}u(r)?(O=!0,r.hasValue&&(b=r.value)):b=r}w=e(b)}switch(w){case"array":r=P.copy?[]:void 0,l=await y(b,r,j,F,S,P,C,I,O,void 0,c,n,...a);break;case"object":r=P.copy?{}:void 0,l=await y(b,r,j,F,S,P,C,I,O,void 0,c,n,...a);break;case"simple":return P.copy?b:void 0}for(let e=0;e<j.length&&!C.finished;){const t=j[e];j[e]=void 0,C.restart=!1,P.detectCycles&&(c=v(l,t.parent,c));const r=P.parentPath?Object.freeze([...t.parentPath,t.key]):void 0;l=await y(t.data,t.location,j,F,t.breadcrumbs,P,C,r,t.pass,t.parent,c,n,...a),e=C.restart?0:e+1}return r}(t,a,...n);if(null==t.timeout)return r;let c;const l=new Promise((e,n)=>{c=setTimeout(()=>{const e=[...a].map(e=>`\n - ${e}`).join("");n(new Error(`walk-async: timed out after ${t.timeout}ms; callbacks still pending:${e}`))},t.timeout)});try{return await Promise.race([r,l])}finally{clearTimeout(c)}}});
|
package/package.json
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter.naydenov/walk-async",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "Deep copy with fine control during the process. Can be used also as a deep forEach function",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "./dist/walk-async.umd.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "./types/main.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"types": "./types/main.d.ts",
|
|
11
|
+
"import": "./dist/walk-async.esm.mjs",
|
|
12
|
+
"require": "./dist/walk-async.cjs",
|
|
12
13
|
"default": "./dist/walk-async.umd.js"
|
|
13
14
|
},
|
|
14
15
|
"./package.json": "./package.json",
|
|
15
16
|
"./dist/*": "./dist/*",
|
|
16
17
|
"./src/*": "./src/*"
|
|
17
18
|
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src",
|
|
22
|
+
"types",
|
|
23
|
+
"README.md",
|
|
24
|
+
"Changelog.md",
|
|
25
|
+
"Migration.guide.md",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
".agents/skills"
|
|
28
|
+
],
|
|
18
29
|
"scripts": {
|
|
19
30
|
"test": "vitest run",
|
|
20
31
|
"test:watch": "vitest",
|
|
@@ -35,11 +46,11 @@
|
|
|
35
46
|
"@rollup/plugin-commonjs": "^29.0.3",
|
|
36
47
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
37
48
|
"@rollup/plugin-terser": "^1.0.0",
|
|
38
|
-
"@vitest/coverage-v8": "
|
|
39
|
-
"rollup": "^4.
|
|
49
|
+
"@vitest/coverage-v8": "5.0.2",
|
|
50
|
+
"rollup": "^4.63.5",
|
|
40
51
|
"typescript": "^7.0.2",
|
|
41
|
-
"vite": "^8.
|
|
42
|
-
"vitest": "
|
|
52
|
+
"vite": "^8.3.1",
|
|
53
|
+
"vitest": "5.0.2"
|
|
43
54
|
},
|
|
44
55
|
"vitest": {
|
|
45
56
|
"coverage": {
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
import findType from "./findType.js";
|
|
4
|
+
import validateForInsertion from "./validateForInsertion.js";
|
|
5
|
+
import setKey from "./setKey.js";
|
|
6
|
+
import createCallbackArgs from "./createCallbackArgs.js";
|
|
7
|
+
import PASS, { isPass } from "./pass.js";
|
|
8
|
+
import IGNORE, { isIgnore } from "./ignore.js";
|
|
9
|
+
import { isFinish } from "./finish.js";
|
|
10
|
+
import runCallback from "./runCallback.js";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async function copyObject ( resource, result, extend, cb, breadcrumbs, settings, control, parentPath, pass, parent, ancestors, pending, ...args ) {
|
|
15
|
+
let
|
|
16
|
+
[ keyCallback, objectCallback ] = cb
|
|
17
|
+
, keys = Object.keys ( resource )
|
|
18
|
+
, parents = settings.detectCycles ? { data:resource, location:result, parent, depth:parent ? parent.depth + 1 : 0 } : undefined
|
|
19
|
+
, last = false
|
|
20
|
+
;
|
|
21
|
+
|
|
22
|
+
if ( ancestors ) ancestors.set ( resource, parents )
|
|
23
|
+
|
|
24
|
+
for ( let index = 0; index < keys.length && !last; index++ ) {
|
|
25
|
+
const k = keys[index];
|
|
26
|
+
let
|
|
27
|
+
item = resource[k]
|
|
28
|
+
, type = findType ( item )
|
|
29
|
+
, canInsert = settings.copy && validateForInsertion ( k, result )
|
|
30
|
+
, br = settings.breadcrumbs && ( type !== 'simple' || ( !pass && keyCallback ) ) ? `${breadcrumbs}/${k}` : undefined
|
|
31
|
+
, passChild = false
|
|
32
|
+
;
|
|
33
|
+
|
|
34
|
+
if ( type !== 'simple' && objectCallback && !control.isFinished ) {
|
|
35
|
+
const callbackArgs = createCallbackArgs ( item, k, IGNORE, br, parentPath, settings )
|
|
36
|
+
callbackArgs.PASS = PASS
|
|
37
|
+
const replacement = await runCallback ( objectCallback, callbackArgs, pending, 'objectCallback', ...args )
|
|
38
|
+
if ( isFinish ( replacement ) ) {
|
|
39
|
+
if ( !replacement.hasValue || !keyCallback ) {
|
|
40
|
+
finish ( replacement, result, k, canInsert, settings, control )
|
|
41
|
+
return parents
|
|
42
|
+
}
|
|
43
|
+
// Discard unrelated pending work. Only the selected
|
|
44
|
+
// final branch may schedule further containers.
|
|
45
|
+
extend.length = 0
|
|
46
|
+
control.restart = true
|
|
47
|
+
control.isFinished = true
|
|
48
|
+
last = true
|
|
49
|
+
item = replacement.value
|
|
50
|
+
}
|
|
51
|
+
else if ( isIgnore ( replacement ) ) continue
|
|
52
|
+
else if ( isPass ( replacement ) ) {
|
|
53
|
+
passChild = true
|
|
54
|
+
if ( replacement.hasValue ) item = replacement.value
|
|
55
|
+
}
|
|
56
|
+
else item = replacement
|
|
57
|
+
type = findType ( item )
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if ( type === 'simple' ) {
|
|
61
|
+
if ( passChild || ( pass && !control.isFinished ) || !keyCallback ) {
|
|
62
|
+
if ( !settings.copy ) continue
|
|
63
|
+
if ( canInsert ) result.push ( item ) // It's an array
|
|
64
|
+
else setKey ( result, k, item ) // It's an object
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
const callbackArgs = createCallbackArgs ( item, k, IGNORE, br, parentPath, settings );
|
|
68
|
+
callbackArgs.isFinished = control.isFinished
|
|
69
|
+
let keyRes = await runCallback ( keyCallback, callbackArgs, pending, 'keyCallback', ...args );
|
|
70
|
+
if ( isFinish ( keyRes ) ) {
|
|
71
|
+
finish ( keyRes, result, k, canInsert, settings, control )
|
|
72
|
+
return parents
|
|
73
|
+
}
|
|
74
|
+
if ( isIgnore ( keyRes ) ) continue
|
|
75
|
+
// Re-type the returned value. A plain object/array returned from
|
|
76
|
+
// keyCallback is walked into via the same work queue used for
|
|
77
|
+
// original nested values; built-in types (Date, Map, Set, etc.) are
|
|
78
|
+
// still 'simple' and stored by reference.
|
|
79
|
+
const newType = findType ( keyRes )
|
|
80
|
+
if ( newType === 'simple' ) {
|
|
81
|
+
if ( !settings.copy ) continue
|
|
82
|
+
if ( canInsert ) result.push ( keyRes ) // It's an array
|
|
83
|
+
else setKey ( result, k, keyRes ) // It's an object
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
item = keyRes
|
|
87
|
+
type = newType
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const circular = settings.detectCycles ? findParent ( item, parents, ancestors ) : undefined
|
|
91
|
+
if ( circular ) {
|
|
92
|
+
if ( !settings.copy ) continue
|
|
93
|
+
if ( canInsert ) result.push ( circular.location )
|
|
94
|
+
else setKey ( result, k, circular.location )
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if ( type === 'object' ) {
|
|
99
|
+
const newObject = settings.copy ? {} : undefined;
|
|
100
|
+
if ( settings.copy ) {
|
|
101
|
+
if ( canInsert ) result.push ( newObject )
|
|
102
|
+
else setKey ( result, k, newObject )
|
|
103
|
+
}
|
|
104
|
+
extend.push ( createJob ( item, newObject, br, parentPath, k, passChild, parents ) )
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if ( type === 'array' ) {
|
|
108
|
+
const newArray = settings.copy ? [] : undefined;
|
|
109
|
+
if ( settings.copy ) {
|
|
110
|
+
if ( canInsert ) result.push ( newArray )
|
|
111
|
+
else setKey ( result, k, newArray )
|
|
112
|
+
}
|
|
113
|
+
extend.push ( createJob ( item, newArray, br, parentPath, k, passChild, parents ) )
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return parents
|
|
117
|
+
} // copyObject func.
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
function finish ( instruction, result, k, canInsert, settings, control ) {
|
|
122
|
+
control.finished = true
|
|
123
|
+
if ( !settings.copy || !instruction.hasValue ) return
|
|
124
|
+
if ( canInsert ) result.push ( instruction.value )
|
|
125
|
+
else setKey ( result, k, instruction.value )
|
|
126
|
+
} // finish func.
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
function createJob ( data, location, breadcrumbs, parentPath, key, pass, parent ) {
|
|
131
|
+
return { data, location, breadcrumbs, parentPath, key, pass, parent }
|
|
132
|
+
} // createJob func.
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
function findParent ( data, parent, ancestors ) {
|
|
137
|
+
if ( ancestors ) return ancestors.get ( data )
|
|
138
|
+
for ( let current = parent; current; current = current.parent ) {
|
|
139
|
+
if ( current.data === data ) return current
|
|
140
|
+
}
|
|
141
|
+
} // findParent func.
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
export default copyObject
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import FINISH from "./finish.js";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
function createCallbackArgs ( value, key, IGNORE, breadcrumbs, parentPath, settings ) {
|
|
6
|
+
if ( settings.breadcrumbs && settings.parentPath ) return { value, key, breadcrumbs, parentPath, IGNORE, FINISH }
|
|
7
|
+
if ( settings.breadcrumbs ) return { value, key, breadcrumbs, IGNORE, FINISH }
|
|
8
|
+
if ( settings.parentPath ) return { value, key, parentPath, IGNORE, FINISH }
|
|
9
|
+
return { value, key, IGNORE, FINISH }
|
|
10
|
+
} // createCallbackArgs func.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export default createCallbackArgs
|
|
15
|
+
|
package/src/findType.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function findType ( x ) {
|
|
2
|
+
if ( x == null ) return 'simple' // null and undefined
|
|
3
|
+
if ( x.nodeType ) return 'simple' // DOM node
|
|
4
|
+
if ( Array.isArray ( x ) ) return 'array'
|
|
5
|
+
if ( typeof x === 'object' ) {
|
|
6
|
+
// Built-in object types whose data lives outside the own-enumerable-string-key
|
|
7
|
+
// model that walk uses. Treated as 'simple' so the value is preserved by
|
|
8
|
+
// reference (same contract as functions and DOM nodes).
|
|
9
|
+
if ( x instanceof Date ) return 'simple'
|
|
10
|
+
if ( x instanceof RegExp ) return 'simple'
|
|
11
|
+
if ( x instanceof Map ) return 'simple'
|
|
12
|
+
if ( x instanceof Set ) return 'simple'
|
|
13
|
+
if ( x instanceof WeakMap ) return 'simple'
|
|
14
|
+
if ( x instanceof WeakSet ) return 'simple'
|
|
15
|
+
if ( x instanceof ArrayBuffer ) return 'simple'
|
|
16
|
+
if ( x instanceof DataView ) return 'simple'
|
|
17
|
+
if ( ArrayBuffer.isView ( x ) ) return 'simple' // Typed arrays (Uint8Array, Float32Array, ...)
|
|
18
|
+
return 'object'
|
|
19
|
+
}
|
|
20
|
+
return 'simple' // number, bigint, string, boolean, symbol, function
|
|
21
|
+
} // findType func.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export default findType
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
package/src/finish.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
const instructions = new WeakSet ();
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Private instruction created by the FINISH helper.
|
|
8
|
+
*
|
|
9
|
+
* @typedef {object} FinishInstruction
|
|
10
|
+
* @property {*} value
|
|
11
|
+
* @property {boolean} hasValue
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Stop the entire walk. Omit the current value when no argument is
|
|
16
|
+
* supplied. From objectCallback, a supplied value selects the final
|
|
17
|
+
* branch for key callbacks with isFinished true. From keyCallback,
|
|
18
|
+
* include the supplied value directly and stop immediately.
|
|
19
|
+
*
|
|
20
|
+
* @param {*} [value] - Final value or branch.
|
|
21
|
+
* @returns {FinishInstruction}
|
|
22
|
+
*/
|
|
23
|
+
function FINISH ( value ) {
|
|
24
|
+
const instruction = Object.freeze ({ value, hasValue:arguments.length > 0 });
|
|
25
|
+
instructions.add ( instruction )
|
|
26
|
+
return instruction
|
|
27
|
+
} // FINISH func.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
function isFinish ( value ) {
|
|
32
|
+
return instructions.has ( value )
|
|
33
|
+
} // isFinish func.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
export { isFinish }
|
|
38
|
+
export default FINISH
|
package/src/ignore.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
const instruction = Symbol ( 'ignore___' );
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Remove the current key or the entire current object/array branch.
|
|
8
|
+
*
|
|
9
|
+
* @returns {symbol}
|
|
10
|
+
*/
|
|
11
|
+
function IGNORE () {
|
|
12
|
+
return instruction
|
|
13
|
+
} // IGNORE func.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
function isIgnore ( value ) {
|
|
18
|
+
return value === instruction
|
|
19
|
+
} // isIgnore func.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export { isIgnore }
|
|
24
|
+
export default IGNORE
|