UNPKG

rubico

Version:

[a]synchronous functional programming

9 lines (7 loc) 7.43 kB
/** * rubico v2.6.2 * https://github.com/a-synchronous/rubico * (c) 2019-2024 Richard Tong * rubico may be freely distributed under the MIT license. */ !function(n,t){"object"==typeof module?module.exports=t:"function"==typeof define?define((()=>t)):n.flatMap=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=Array.isArray,e=(Object.values,Object.prototype.toString,Symbol.for("placeholder")),o=function(n,t,o,r){return t==e?((n,t,e)=>function(o){return n(o,t,e)})(n,o,r):o==e?((n,t,e)=>function(o){return n(t,o,e)})(n,t,r):((n,t,e)=>function(o){return n(t,e,o)})(n,t,o)},r=async function(t,e,o){let r=t.next();if(r.done)return o;for(;!r.done;)o=e(o,r.value),n(o)&&(o=await o),r=t.next();return o},u=Symbol.iterator,f=Symbol.asyncIterator,c=function(n,t,o){return t==e?((n,t)=>function(e){return n(e,t)})(n,o):((n,t)=>function(e){return n(t,e)})(n,t)},i=function(n,t,o,r,u){return t==e?((n,t,e,o)=>function(r){return n(r,t,e,o)})(n,o,r,u):o==e?((n,t,e,o)=>function(r){return n(t,r,e,o)})(n,t,r,u):r==e?((n,t,e,o)=>function(r){return n(t,e,r,o)})(n,t,o,u):((n,t,e,o)=>function(r){return n(t,e,o,r)})(n,t,o,r)},s=async function(t,e,o,r){const u=t.length;for(;++r<u;)o=e(o,t[r],r,t),n(o)&&(o=await o);return o},l=Object.keys,a=async function(t,e,o,r,u){const f=r.length;for(;++u<f;){const f=r[u];o=e(o,t[f],f,t),n(o)&&(o=await o)}return o},h=async function(t,e,o,r){for(const[u,f]of r)o=e(o,f,u,t),n(o)&&(o=await o);return o},p=function(p,d,y){return t(p)?function(t,o,r){const u=t.length;let f=-1;for(void 0===r&&(r=t[++f]);++f<u;)if(r=o(r,t[f],f,t),n(r))return r.then(i(s,t,o,e,f));return r}(p,d,y):null==p?void 0===y?c(d,p,e):d(y,p):p.constructor==Map?function(t,o,r){const u=t.entries();if(void 0===r){const n=u.next();if(n.done)return r;r=n.value[1]}for(const[f,c]of u)if(r=o(r,c,f,t),n(r))return r.then(i(h,t,o,e,u));return r}(p,d,y):"function"==typeof p[u]?function(t,u,f){let c=t.next();if(c.done)return f;for(void 0===f&&(f=c.value,c=t.next());!c.done;){if(f=u(f,c.value),n(f))return f.then(o(r,t,u,e));c=t.next()}return f}(p[u](),d,y):"function"==typeof p[f]?async function(n,t,e){let o=await n.next();if(o.done)return e;for(void 0===e&&(e=o.value,o=await n.next());!o.done;)e=await t(e,o.value),o=await n.next();return e}(p[f](),d,y):"function"==typeof p.reduce?p.reduce(d,y):"function"==typeof p.chain?p.chain(c(d,y,e)):"function"==typeof p.flatMap?p.flatMap(c(d,y,e)):p.constructor==Object?function(t,o,r){const u=l(t),f=u.length;let c=-1;for(void 0===r&&(r=t[u[++c]]);++c<f;){const f=u[c];if(r=o(r,t[f],f,t),n(r))return r.then((i=a,h=o,p=e,d=u,y=c,(s=t)==e?((n,t,e,o,r)=>function(u){return n(u,t,e,o,r)})(i,h,p,d,y):h==e?((n,t,e,o,r)=>function(u){return n(t,u,e,o,r)})(i,s,p,d,y):p==e?((n,t,e,o,r)=>function(u){return n(t,e,u,o,r)})(i,s,h,d,y):d==e?((n,t,e,o,r)=>function(u){return n(t,e,o,u,r)})(i,s,h,p,y):((n,t,e,o,r)=>function(u){return n(t,e,o,r,u)})(i,s,h,p,d)))}var i,s,h,p,d,y;return r}(p,d,y):void 0===y?c(d,p,e):d(y,p)},d=function(n,t){return n.push(t),n},y=Promise.race.bind(Promise),g=n=>new Promise((t=>{setTimeout(t,n)})),v=n=>function(){return n},b=(n,t)=>t,w=n=>n,x=Promise.all.bind(Promise),M=(n,t)=>function(...e){return t(n(...e))},j=async function(t,e){const o=[];for await(const r of t){const t=e(r);n(t)&&o.push(t)}return 0==o.length?t:x(o).then(v(t))},m=function(o){const r=o.length,i=[],s=[];let l=-1;for(;++l<r;){const r=o[l];if(t(r)){const n=r.length;let t=-1;for(;++t<n;)s.push(r[t])}else if(null==r)s.push(r);else if("function"==typeof r.then)i.push(r.then(c(d,s,e)));else if("function"==typeof r[u])for(const n of r)s.push(n);else if("function"==typeof r[f])i.push(j(r[f](),c(d,s,e)));else if("function"==typeof r.chain){const t=r.chain(w);n(t)?i.push(t.then(c(d,s,e))):s.push(t)}else if("function"==typeof r.flatMap){const t=r.flatMap(w);n(t)?i.push(t.then(c(d,s,e))):s.push(t)}else if("function"==typeof r.reduce){const t=r.reduce(M(b,c(d,s,e)),null);n(t)&&i.push(t)}else if(r.constructor==Object)for(const n in r)s.push(r[n]);else s.push(r)}return 0==i.length?s:x(i).then(v(s))},S=function(t,e){const o=t.length,r=Array(o);let u=-1,f=!1;for(;++u<o;){const o=e(t[u],u,t);n(o)&&(f=!0),r[u]=o}return f?x(r):r},A=t=>new Promise((t=>function(e){const o={};let r=0;for(const u in t){const f=t[u];n(f)?(r+=1,f.then((n=>function(t){o[n]=t,r-=1,0==r&&e(o)})(u))):o[u]=f}0==r&&e(o)})(t)),O=Object.assign,I=function(t){const o=[],r={},i=c(O,r,e),s=M(b,i);for(const e in t){const c=t[e];if(null!=c)if("function"==typeof c[u])for(const n of c)O(r,n);else if("function"==typeof c[f])o.push(j(c[f](),i));else if("function"==typeof c.chain){const t=c.chain(w);n(t)?o.push(t.then(i)):O(r,t)}else if("function"==typeof c.flatMap){const t=c.flatMap(w);n(t)?o.push(t.then(i)):i(t)}else if("function"==typeof c.reduce){const t=c.reduce(s,null);n(t)&&o.push(t)}else O(r,c)}return 0==o.length?r:x(o).then((()=>r))},P=function(t,e){const o=function(t,e){const o={};let r=!1;for(const u in t){const f=e(t[u],u,t);n(f)&&(r=!0),o[u]=f}return r?A(o):o}(t,e);return n(o)?o.then(I):I(o)},D=(n,t,e)=>n[t](e),z=function(r){r.size;const c=[],i=new Set,s=(n,t)=>i.add(t),l=o(D,i,"add",e);for(const e of r)if(t(e)){const n=e.length;let t=-1;for(;++t<n;)i.add(e[t])}else if(null==e)i.add(e);else if("function"==typeof e[u])for(const n of e)i.add(n);else if("function"==typeof e[f])c.push(j(e[f](),l));else if("function"==typeof e.chain){const t=e.chain(w);n(t)?c.push(t.then(l)):i.add(t)}else if("function"==typeof e.flatMap){const t=e.flatMap(w);n(t)?c.push(t.then(l)):i.add(t)}else if("function"==typeof e.reduce){const t=e.reduce(s,null);n(t)&&c.push(t)}else if(e.constructor==Object)for(const n in e)i.add(e[n]);else i.add(e);return 0==c.length?i:x(c).then((()=>i))},T=function(t,r){const u=function(t,r){const u=new Set,f=[];for(const c of t){const i=r(c,c,t);n(i)?f.push(i.then(o(D,u,"add",e))):u.add(i)}return 0==f.length?u:x(f).then(v(u))}(t,r);return n(u)?u.then(z):z(u)},k=(F=m,q=c(((n,t)=>n.join(t)),e,""),function(...t){const e=F(...t);return n(e)?e.then(q):q(e)});var F,q;const B=function(r,c){if(t(r))return function(t,e){const o=S(t,e);return n(o)?o.then(m):m(o)}(r,c);if(null==r)return c(r);if("function"==typeof r.then)return r.then(c);if("function"==typeof r.next)return u in r?function(n,t){let e=[],o=0;return{[u](){return this},next(){if(o<e.length){const n=e[o];return o+=1,{value:n,done:!1}}const r=n.next();if(r.done)return r;const u=p(t(r.value),d,[]);return u.length>1&&(e=u,o=1),{value:u[0],done:!1}}}}(r,c):function(t,r){const u=[],c=new Set;return{isAsyncIteratorDone:!1,[f](){return this},toString:()=>"[object FlatMappingAsyncIterator]",async next(){for(;!this.isAsyncIteratorDone||u.length>0||c.size>0;){if(!this.isAsyncIteratorDone){const{value:f,done:i}=await t.next();if(i)this.isAsyncIteratorDone=i;else{const t=r(f);if(n(t)){const n=t.then(o(p,e,d,u)).then((()=>c.delete(n)));c.add(n)}else{const e=p(t,d,u);if(n(e)){const n=e.then((()=>c.delete(n)));c.add(n)}}}}if(u.length>0)return{value:u.shift(),done:!1};c.size>0&&await y([g(1e3),...c])}return{value:void 0,done:!0}}}}(r,c);if("function"==typeof r.chain)return r.chain(c);if("function"==typeof r.flatMap)return r.flatMap(c);const i=r.constructor;return i==Object?P(r,c):i==Set?T(r,c):"string"==typeof r||i==String?function(t,e){const o=S(t,e);return n(o)?o.then(k):k(o)}(r,c):c(r)};return(t,o)=>"function"==typeof t?c(B,e,t):n(t)?t.then(c(B,e,o)):B(t,o)}());