UNPKG

rubico

Version:

[a]synchronous functional programming

10 lines (8 loc) 7.42 kB
/** * Rubico v2.8.2 * https://rubico.land/ * * © Richard Yufei Tong, King of Software * Rubico may be freely distributed under the CFOSS 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=function(n,t,o,r,u,f){return t==e?((n,t,e,o,r)=>function(u){return n(u,t,e,o,r)})(n,o,r,u,f):o==e?((n,t,e,o,r)=>function(u){return n(t,u,e,o,r)})(n,t,r,u,f):r==e?((n,t,e,o,r)=>function(u){return n(t,e,u,o,r)})(n,t,o,u,f):u==e?((n,t,e,o,r)=>function(u){return n(t,e,o,u,r)})(n,t,o,r,f):((n,t,e,o,r)=>function(u){return n(t,e,o,r,u)})(n,t,o,r,u)},a=Object.keys,h=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},p=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},d=function(d,y,g){return t(d)?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}(d,y,g):null==d?void 0===g?c(y,d,e):y(g,d):d.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(p,t,o,e,u));return r}(d,y,g):"function"==typeof d[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}(d[u](),y,g):"function"==typeof d[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}(d[f](),y,g):"function"==typeof d.reduce?d.reduce(y,g):"function"==typeof d.chain?d.chain(c(y,g,e)):"function"==typeof d.flatMap?d.flatMap(c(y,g,e)):d.constructor==Object?function(t,o,r){const u=a(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(l(h,t,o,e,u,c))}return r}(d,y,g):void 0===g?c(y,d,e):y(g,d)},y=function(n,t){return n.push(t),n},g=Promise.race.bind(Promise),v=n=>new Promise(t=>{setTimeout(t,n)}),b=n=>function(){return n},w=(n,t)=>t,x=n=>n,M=Promise.all.bind(Promise),j=(n,t)=>function(...e){return t(n(...e))},m=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:M(o).then(b(t))},S=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(y,s,e)));else if("function"==typeof r[u])for(const n of r)s.push(n);else if("function"==typeof r[f])i.push(m(r[f](),c(y,s,e)));else if("function"==typeof r.chain){const t=r.chain(x);n(t)?i.push(t.then(c(y,s,e))):s.push(t)}else if("function"==typeof r.flatMap){const t=r.flatMap(x);n(t)?i.push(t.then(c(y,s,e))):s.push(t)}else if("function"==typeof r.reduce){const t=r.reduce(j(w,c(y,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:M(i).then(b(s))},A=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?M(r):r},O=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)),I=Object.assign,P=function(t){const o=[],r={},i=c(I,r,e),s=j(w,i);for(const e in t){const c=t[e];if(null!=c)if("function"==typeof c[u])for(const n of c)I(r,n);else if("function"==typeof c[f])o.push(m(c[f](),i));else if("function"==typeof c.chain){const t=c.chain(x);n(t)?o.push(t.then(i)):I(r,t)}else if("function"==typeof c.flatMap){const t=c.flatMap(x);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 I(r,c)}return 0==o.length?r:M(o).then(()=>r)},D=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?O(o):o}(t,e);return n(o)?o.then(P):P(o)},z=(n,t,e)=>n[t](e),T=function(r){r.size;const c=[],i=new Set,s=(n,t)=>i.add(t),l=o(z,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(m(e[f](),l));else if("function"==typeof e.chain){const t=e.chain(x);n(t)?c.push(t.then(l)):i.add(t)}else if("function"==typeof e.flatMap){const t=e.flatMap(x);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:M(c).then(()=>i)},k=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(z,u,"add",e))):u.add(i)}return 0==f.length?u:M(f).then(b(u))}(t,r);return n(u)?u.then(T):T(u)},F=(q=S,B=c((n,t)=>n.join(t),e,""),function(...t){const e=q(...t);return n(e)?e.then(B):B(e)});var q,B;const C=function(r,c){if(t(r))return function(t,e){const o=A(t,e);return n(o)?o.then(S):S(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=d(t(r.value),y,[]);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(d,e,y,u)).then(()=>c.delete(n));c.add(n)}else{const e=d(t,y,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 g([v(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?D(r,c):i==Set?k(r,c):"string"==typeof r||i==String?function(t,e){const o=A(t,e);return n(o)?o.then(F):F(o)}(r,c):c(r)};return(t,o)=>"function"==typeof t?c(C,e,t):n(t)?t.then(c(C,e,o)):C(t,o)}());