@peter.naydenov/walk-async 6.1.0 → 7.0.1

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 +1 @@
1
- "use strict";var e=require("ask-for-promise");async function o(e,o,n){for await(const o of e)await o.next();n.done(o)}function n(e){return null==e||e.nodeType?"simple":e instanceof Array?"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"}async function*t(e,o,n,t,a,i){yield await r(e,o,n,t,a,...i)}function a(e,o,n){"__proto__"===o?Object.defineProperty(e,o,{value:n,enumerable:!0,writable:!0,configurable:!0}):e[o]=n}function r(o,r,i,l,s,...c){let[d,u,p]=l,f=Object.keys(o),m=e(f),b=e();return f.forEach((f,b)=>{let y=n(o[f]),$=o[f],k="simple"!==y&&null!=u,j=e(),v=e(),_=e(),C="array"===n(r),w=!isNaN(f),h=Symbol("ignore___"),g=`${s}/${f}`,S=`objectCallback at '${g}'`,x=`keyCallback at '${g}'`;k?(p.add(S),u({resolve:j.done,reject:()=>j.done(h),value:$,key:f,breadcrumbs:g},...c)):j.done("$$cancel"),j.onComplete(e=>{if(p.delete(S),"$$cancel"!==e||d){if("$$cancel"!==e&&($=e,y=n($)),$==h)return m.promises[b].done("ignore object"),v.done("$$cancel"),void _.done();if("simple"===y){if(!d)return void v.done("$$noUpdates");p.add(x),d({resolve:v.done,reject:()=>v.done(h),value:$,key:f,breadcrumbs:g},...c)}else v.done("$$cancel")}else v.done("$$noUpdates")}),v.onComplete(e=>{if(p.delete(x),e==h)return m.promises[b].done("ignore key"),void _.done();if("$$cancel"!==e){if("$$noUpdates"!==e&&($=e,y=n($)),"simple"===y){const e=function(e,o){return o instanceof Array&&!isNaN(e)}(f,r);return e?r.push($):a(r,f,$),void m.promises[b].done("key")}_.done()}else _.done()}),_.onComplete(()=>{if("object"===y){const e={};C&&w?r.push(e):a(r,f,e),i.push(t($,e,i,l,g,c)),m.promises[b].done("object")}if("array"===y){const e=[];C&&w?r.push(e):a(r,f,e),i.push(t($,e,i,l,g,c)),m.promises[b].done("array")}})}),m.onComplete(e=>b.done()),b.promise}module.exports=function({data:t,objectCallback:a=null,keyCallback:i=null,timeout:l=null},...s){let c,d=n(t),u=[],p="root",f=new Set,m=[i,a,f],b=e(),y=e(),$=Symbol("ignore___");if("simple"!==d&&a?(f.add("objectCallback at 'root'"),a({resolve:y.done,reject:()=>y.done($),value:t,key:"root",breadcrumbs:p},...s)):y.done(t),y.onComplete(e=>{if(f.delete("objectCallback at 'root'"),e!==$)switch(n(e)){case"array":c=[],r(e,c,u,m,p,...s).then(()=>o(u,c,b));break;case"object":c={},r(e,c,u,m,p,...s).then(()=>{o(u,c,b)});break;case"simple":b.done(e)}else b.done("array"===d?[]:{})}),null==l)return b.promise;const k=Symbol("expired___"),j=e();return b.timeout(l,k),b.onComplete(e=>{if(e!==k)return void j.done(e);const o=[...f].map(e=>`\n - ${e}`).join("");j.cancel(new Error(`walk-async: timed out after ${l}ms; callbacks still pending:${o}`))}),j.promise};
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)}};
@@ -1 +1 @@
1
- import e from"ask-for-promise";function o({data:o,objectCallback:t=null,keyCallback:r=null,timeout:l=null},...c){let s,d=a(o),f=[],p="root",u=new Set,m=[r,t,u],b=e(),y=e(),$=Symbol("ignore___");if("simple"!==d&&t?(u.add("objectCallback at 'root'"),t({resolve:y.done,reject:()=>y.done($),value:o,key:"root",breadcrumbs:p},...c)):y.done(o),y.onComplete(e=>{if(u.delete("objectCallback at 'root'"),e!==$)switch(a(e)){case"array":s=[],i(e,s,f,m,p,...c).then(()=>n(f,s,b));break;case"object":s={},i(e,s,f,m,p,...c).then(()=>{n(f,s,b)});break;case"simple":b.done(e)}else b.done("array"===d?[]:{})}),null==l)return b.promise;const k=Symbol("expired___"),j=e();return b.timeout(l,k),b.onComplete(e=>{if(e!==k)return void j.done(e);const o=[...u].map(e=>`\n - ${e}`).join("");j.cancel(new Error(`walk-async: timed out after ${l}ms; callbacks still pending:${o}`))}),j.promise}async function n(e,o,n){for await(const o of e)await o.next();n.done(o)}function a(e){return null==e||e.nodeType?"simple":e instanceof Array?"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"}async function*t(e,o,n,a,t,r){yield await i(e,o,n,a,t,...r)}function r(e,o,n){"__proto__"===o?Object.defineProperty(e,o,{value:n,enumerable:!0,writable:!0,configurable:!0}):e[o]=n}function i(o,n,i,l,c,...s){let[d,f,p]=l,u=Object.keys(o),m=e(u),b=e();return u.forEach((u,b)=>{let y=a(o[u]),$=o[u],k="simple"!==y&&null!=f,j=e(),_=e(),v=e(),C="array"===a(n),w=!isNaN(u),h=Symbol("ignore___"),g=`${c}/${u}`,S=`objectCallback at '${g}'`,x=`keyCallback at '${g}'`;k?(p.add(S),f({resolve:j.done,reject:()=>j.done(h),value:$,key:u,breadcrumbs:g},...s)):j.done("$$cancel"),j.onComplete(e=>{if(p.delete(S),"$$cancel"!==e||d){if("$$cancel"!==e&&($=e,y=a($)),$==h)return m.promises[b].done("ignore object"),_.done("$$cancel"),void v.done();if("simple"===y){if(!d)return void _.done("$$noUpdates");p.add(x),d({resolve:_.done,reject:()=>_.done(h),value:$,key:u,breadcrumbs:g},...s)}else _.done("$$cancel")}else _.done("$$noUpdates")}),_.onComplete(e=>{if(p.delete(x),e==h)return m.promises[b].done("ignore key"),void v.done();if("$$cancel"!==e){if("$$noUpdates"!==e&&($=e,y=a($)),"simple"===y){const e=function(e,o){return o instanceof Array&&!isNaN(e)}(u,n);return e?n.push($):r(n,u,$),void m.promises[b].done("key")}v.done()}else v.done()}),v.onComplete(()=>{if("object"===y){const e={};C&&w?n.push(e):r(n,u,e),i.push(t($,e,i,l,g,s)),m.promises[b].done("object")}if("array"===y){const e=[];C&&w?n.push(e):r(n,u,e),i.push(t($,e,i,l,g,s)),m.promises[b].done("array")}})}),m.onComplete(e=>b.done()),b.promise}export{o as default};
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};
@@ -1 +1 @@
1
- !function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o(require("ask-for-promise")):"function"==typeof define&&define.amd?define(["ask-for-promise"],o):(e="undefined"!=typeof globalThis?globalThis:e||self).walkAsync=o(e.askForPromise)}(this,function(e){"use strict";async function o(e,o,n){for await(const o of e)await o.next();n.done(o)}function n(e){return null==e||e.nodeType?"simple":e instanceof Array?"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"}async function*t(e,o,n,t,a,i){yield await r(e,o,n,t,a,...i)}function a(e,o,n){"__proto__"===o?Object.defineProperty(e,o,{value:n,enumerable:!0,writable:!0,configurable:!0}):e[o]=n}function r(o,r,i,s,l,...c){let[d,f,u]=s,p=Object.keys(o),m=e(p),b=e();return p.forEach((p,b)=>{let y=n(o[p]),$=o[p],k="simple"!==y&&null!=f,j=e(),_=e(),h=e(),v="array"===n(r),C=!isNaN(p),w=Symbol("ignore___"),g=`${l}/${p}`,S=`objectCallback at '${g}'`,x=`keyCallback at '${g}'`;k?(u.add(S),f({resolve:j.done,reject:()=>j.done(w),value:$,key:p,breadcrumbs:g},...c)):j.done("$$cancel"),j.onComplete(e=>{if(u.delete(S),"$$cancel"!==e||d){if("$$cancel"!==e&&($=e,y=n($)),$==w)return m.promises[b].done("ignore object"),_.done("$$cancel"),void h.done();if("simple"===y){if(!d)return void _.done("$$noUpdates");u.add(x),d({resolve:_.done,reject:()=>_.done(w),value:$,key:p,breadcrumbs:g},...c)}else _.done("$$cancel")}else _.done("$$noUpdates")}),_.onComplete(e=>{if(u.delete(x),e==w)return m.promises[b].done("ignore key"),void h.done();if("$$cancel"!==e){if("$$noUpdates"!==e&&($=e,y=n($)),"simple"===y){const e=function(e,o){return o instanceof Array&&!isNaN(e)}(p,r);return e?r.push($):a(r,p,$),void m.promises[b].done("key")}h.done()}else h.done()}),h.onComplete(()=>{if("object"===y){const e={};v&&C?r.push(e):a(r,p,e),i.push(t($,e,i,s,g,c)),m.promises[b].done("object")}if("array"===y){const e=[];v&&C?r.push(e):a(r,p,e),i.push(t($,e,i,s,g,c)),m.promises[b].done("array")}})}),m.onComplete(e=>b.done()),b.promise}return function({data:t,objectCallback:a=null,keyCallback:i=null,timeout:s=null},...l){let c,d=n(t),f=[],u="root",p=new Set,m=[i,a,p],b=e(),y=e(),$=Symbol("ignore___");if("simple"!==d&&a?(p.add("objectCallback at 'root'"),a({resolve:y.done,reject:()=>y.done($),value:t,key:"root",breadcrumbs:u},...l)):y.done(t),y.onComplete(e=>{if(p.delete("objectCallback at 'root'"),e!==$)switch(n(e)){case"array":c=[],r(e,c,f,m,u,...l).then(()=>o(f,c,b));break;case"object":c={},r(e,c,f,m,u,...l).then(()=>{o(f,c,b)});break;case"simple":b.done(e)}else b.done("array"===d?[]:{})}),null==s)return b.promise;const k=Symbol("expired___"),j=e();return b.timeout(s,k),b.onComplete(e=>{if(e!==k)return void j.done(e);const o=[...p].map(e=>`\n - ${e}`).join("");j.cancel(new Error(`walk-async: timed out after ${s}ms; callbacks still pending:${o}`))}),j.promise}});
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,12 +1,13 @@
1
1
  {
2
2
  "name": "@peter.naydenov/walk-async",
3
- "version": "6.1.0",
3
+ "version": "7.0.1",
4
4
  "description": "Deep copy with fine control during the process. Can be used also as a deep forEach function",
5
5
  "main": "./dist/walk-async.umd.js",
6
6
  "type": "module",
7
7
  "types": "./types/main.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
+ "types": "./types/main.d.ts",
10
11
  "import": "./dist/walk-async.esm.mjs",
11
12
  "require": "./dist/walk-async.cjs",
12
13
  "default": "./dist/walk-async.umd.js"
@@ -39,17 +40,17 @@
39
40
  "author": "Peter Naydenov",
40
41
  "license": "MIT",
41
42
  "dependencies": {
42
- "ask-for-promise": "^3.2.0"
43
+ "ask-for-promise": "^3.2.1"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@rollup/plugin-commonjs": "^29.0.3",
46
47
  "@rollup/plugin-node-resolve": "^16.0.3",
47
48
  "@rollup/plugin-terser": "^1.0.0",
48
- "@vitest/coverage-v8": "4.1.11",
49
- "rollup": "^4.63.1",
49
+ "@vitest/coverage-v8": "5.0.5",
50
+ "rollup": "^4.64.0",
50
51
  "typescript": "^7.0.2",
51
- "vite": "^8.2.2",
52
- "vitest": "4.1.11"
52
+ "vite": "^8.3.4",
53
+ "vitest": "5.0.5"
53
54
  },
54
55
  "vitest": {
55
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
+
@@ -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