rubico
Version:
[a]synchronous functional programming
10 lines (8 loc) • 7.53 kB
JavaScript
/**
* 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.flatten=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=Array.isArray,t=Symbol.for("placeholder"),e=function(n,t){return n.push(t),n},o=n=>function(){return n},r=function(n,e,o){return e==t?((n,t)=>function(e){return n(e,t)})(n,o):((n,t)=>function(e){return n(t,e)})(n,e)},u=(n,t)=>t,c=n=>n,f=n=>null!=n&&"function"==typeof n.then,i=Promise.all.bind(Promise),s=(n,t)=>function(...e){return t(n(...e))},l=async function(n,t){const e=[];for await(const o of n){const n=t(o);f(n)&&e.push(n)}return 0==e.length?n:i(e).then(o(n))},a=Symbol.iterator,h=Symbol.asyncIterator,p=function(p){const d=p.length,y=[],g=[];let v=-1;for(;++v<d;){const o=p[v];if(n(o)){const n=o.length;let t=-1;for(;++t<n;)g.push(o[t])}else if(null==o)g.push(o);else if("function"==typeof o.then)y.push(o.then(r(e,g,t)));else if("function"==typeof o[a])for(const n of o)g.push(n);else if("function"==typeof o[h])y.push(l(o[h](),r(e,g,t)));else if("function"==typeof o.chain){const n=o.chain(c);f(n)?y.push(n.then(r(e,g,t))):g.push(n)}else if("function"==typeof o.flatMap){const n=o.flatMap(c);f(n)?y.push(n.then(r(e,g,t))):g.push(n)}else if("function"==typeof o.reduce){const n=o.reduce(s(u,r(e,g,t)),null);f(n)&&y.push(n)}else if(o.constructor==Object)for(const n in o)g.push(o[n]);else g.push(o)}return 0==y.length?g:i(y).then(o(g))},d=function(n,e,o,r){return e==t?((n,t,e)=>function(o){return n(o,t,e)})(n,o,r):o==t?((n,t,e)=>function(o){return n(t,o,e)})(n,e,r):((n,t,e)=>function(o){return n(t,e,o)})(n,e,o)},y=(n,t,e)=>n[t](e),g=function(e){e.size;const o=[],r=new Set,u=(n,t)=>r.add(t),s=d(y,r,"add",t);for(const t of e)if(n(t)){const n=t.length;let e=-1;for(;++e<n;)r.add(t[e])}else if(null==t)r.add(t);else if("function"==typeof t[a])for(const n of t)r.add(n);else if("function"==typeof t[h])o.push(l(t[h](),s));else if("function"==typeof t.chain){const n=t.chain(c);f(n)?o.push(n.then(s)):r.add(n)}else if("function"==typeof t.flatMap){const n=t.flatMap(c);f(n)?o.push(n.then(s)):r.add(n)}else if("function"==typeof t.reduce){const n=t.reduce(u,null);f(n)&&o.push(n)}else if(t.constructor==Object)for(const n in t)r.add(t[n]);else r.add(t);return 0==o.length?r:i(o).then(()=>r)},v=Object.assign,b=function(n){const e=[],o={},p=r(v,o,t),d=s(u,p);for(const t in n){const r=n[t];if(null!=r)if("function"==typeof r[a])for(const n of r)v(o,n);else if("function"==typeof r[h])e.push(l(r[h](),p));else if("function"==typeof r.chain){const n=r.chain(c);f(n)?e.push(n.then(p)):v(o,n)}else if("function"==typeof r.flatMap){const n=r.flatMap(c);f(n)?e.push(n.then(p)):p(n)}else if("function"==typeof r.reduce){const n=r.reduce(d,null);f(n)&&e.push(n)}else v(o,r)}return 0==e.length?o:i(e).then(()=>o)},w=(Object.values,Object.prototype.toString,async function(n,t,e){let o=n.next();if(o.done)return e;for(;!o.done;)e=t(e,o.value),f(e)&&(e=await e),o=n.next();return e}),x=function(n,e,o,r,u){return e==t?((n,t,e,o)=>function(r){return n(r,t,e,o)})(n,o,r,u):o==t?((n,t,e,o)=>function(r){return n(t,r,e,o)})(n,e,r,u):r==t?((n,t,e,o)=>function(r){return n(t,e,r,o)})(n,e,o,u):((n,t,e,o)=>function(r){return n(t,e,o,r)})(n,e,o,r)},j=async function(n,t,e,o){const r=n.length;for(;++o<r;)e=t(e,n[o],o,n),f(e)&&(e=await e);return e},m=function(n,e,o,r,u,c){return e==t?((n,t,e,o,r)=>function(u){return n(u,t,e,o,r)})(n,o,r,u,c):o==t?((n,t,e,o,r)=>function(u){return n(t,u,e,o,r)})(n,e,r,u,c):r==t?((n,t,e,o,r)=>function(u){return n(t,e,u,o,r)})(n,e,o,u,c):u==t?((n,t,e,o,r)=>function(u){return n(t,e,o,u,r)})(n,e,o,r,c):((n,t,e,o,r)=>function(u){return n(t,e,o,r,u)})(n,e,o,r,u)},M=Object.keys,S=async function(n,t,e,o,r){const u=o.length;for(;++r<u;){const u=o[r];e=t(e,n[u],u,n),f(e)&&(e=await e)}return e},O=async function(n,t,e,o){for(const[r,u]of o)e=t(e,u,r,n),f(e)&&(e=await e);return e},A=function(e,o,u){return n(e)?function(n,e,o){const r=n.length;let u=-1;for(void 0===o&&(o=n[++u]);++u<r;)if(o=e(o,n[u],u,n),f(o))return o.then(x(j,n,e,t,u));return o}(e,o,u):null==e?void 0===u?r(o,e,t):o(u,e):e.constructor==Map?function(n,e,o){const r=n.entries();if(void 0===o){const n=r.next();if(n.done)return o;o=n.value[1]}for(const[u,c]of r)if(o=e(o,c,u,n),f(o))return o.then(x(O,n,e,t,r));return o}(e,o,u):"function"==typeof e[a]?function(n,e,o){let r=n.next();if(r.done)return o;for(void 0===o&&(o=r.value,r=n.next());!r.done;){if(o=e(o,r.value),f(o))return o.then(d(w,n,e,t));r=n.next()}return o}(e[a](),o,u):"function"==typeof e[h]?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}(e[h](),o,u):"function"==typeof e.reduce?e.reduce(o,u):"function"==typeof e.chain?e.chain(r(o,u,t)):"function"==typeof e.flatMap?e.flatMap(r(o,u,t)):e.constructor==Object?function(n,e,o){const r=M(n),u=r.length;let c=-1;for(void 0===o&&(o=n[r[++c]]);++c<u;){const u=r[c];if(o=e(o,n[u],u,n),f(o))return o.then(m(S,n,e,t,r,c))}return o}(e,o,u):void 0===u?r(o,e,t):o(u,e)},I=Promise.race.bind(Promise),P=n=>new Promise(t=>{setTimeout(t,n)}),D=function(n,t){const e=n.length,o=Array(e);let r=-1,u=!1;for(;++r<e;){const e=t(n[r],r,n);f(e)&&(u=!0),o[r]=e}return u?i(o):o},z=n=>new Promise((n=>function(t){const e={};let o=0;for(const r in n){const u=n[r];f(u)?(o+=1,u.then((n=>function(r){e[n]=r,o-=1,0==o&&t(e)})(r))):e[r]=u}0==o&&t(e)})(n)),T=function(n,t){const e=function(n,t){const e={};let o=!1;for(const r in n){const u=t(n[r],r,n);f(u)&&(o=!0),e[r]=u}return o?z(e):e}(n,t);return f(e)?e.then(b):b(e)},k=function(n,e){const r=function(n,e){const r=new Set,u=[];for(const o of n){const c=e(o,o,n);f(c)?u.push(c.then(d(y,r,"add",t))):r.add(c)}return 0==u.length?r:i(u).then(o(r))}(n,e);return f(r)?r.then(g):g(r)},F=(q=p,B=r((n,t)=>n.join(t),t,""),function(...n){const t=q(...n);return f(t)?t.then(B):B(t)});var q,B;const C=function(o,r){if(n(o))return function(n,t){const e=D(n,t);return f(e)?e.then(p):p(e)}(o,r);if(null==o)return r(o);if("function"==typeof o.then)return o.then(r);if("function"==typeof o.next)return a in o?function(n,t){let o=[],r=0;return{[a](){return this},next(){if(r<o.length){const n=o[r];return r+=1,{value:n,done:!1}}const u=n.next();if(u.done)return u;const c=A(t(u.value),e,[]);return c.length>1&&(o=c,r=1),{value:c[0],done:!1}}}}(o,r):function(n,o){const r=[],u=new Set;return{isAsyncIteratorDone:!1,[h](){return this},toString:()=>"[object FlatMappingAsyncIterator]",async next(){for(;!this.isAsyncIteratorDone||r.length>0||u.size>0;){if(!this.isAsyncIteratorDone){const{value:c,done:i}=await n.next();if(i)this.isAsyncIteratorDone=i;else{const n=o(c);if(f(n)){const o=n.then(d(A,t,e,r)).then(()=>u.delete(o));u.add(o)}else{const t=A(n,e,r);if(f(t)){const n=t.then(()=>u.delete(n));u.add(n)}}}}if(r.length>0)return{value:r.shift(),done:!1};u.size>0&&await I([P(1e3),...u])}return{value:void 0,done:!0}}}}(o,r);if("function"==typeof o.chain)return o.chain(r);if("function"==typeof o.flatMap)return o.flatMap(r);const u=o.constructor;return u==Object?T(o,r):u==Set?k(o,r):"string"==typeof o||u==String?function(n,t){const e=D(n,t);return f(e)?e.then(F):F(e)}(o,r):r(o)};return function(e){return n(e)?p(e):null==e?e:e.constructor==Set?g(e):e.constructor==Object?b(e):(u=c,"function"==typeof(o=e)?r(C,t,o):f(o)?o.then(r(C,t,u)):C(o,u));var o,u}}());