rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 7.88 kB
JavaScript
/**
* 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.map=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=Symbol.iterator,e=(n,e)=>({toString:()=>"[object MappingIterator]",[t](){return this},next(){const t=n.next();return t.done?t:{value:e(t.value),done:!1}}}),r=n=>({value:n,done:!1}),o=Symbol.asyncIterator,u=(t,e)=>({[o](){return this},async next(){const o=await t.next();if(o.done)return o;const u=e(o.value);return n(u)?u.then(r):{value:u,done:!1}}}),c=Symbol.for("placeholder"),i=function(n,t,e){return t==c?((n,t)=>function(e){return n(e,t)})(n,e):((n,t)=>function(e){return n(t,e)})(n,t)},s=function(n,t,e,r){return t==c?((n,t,e)=>function(r){return n(r,t,e)})(n,e,r):e==c?((n,t,e)=>function(r){return n(t,r,e)})(n,t,r):((n,t,e)=>function(r){return n(t,e,r)})(n,t,e)},f=Array.isArray,l=Promise.all.bind(Promise),a=function(t,e){const r=t.length,o=Array(r);let u=-1,c=!1;for(;++u<r;){const r=e(t[u],u,t);n(r)&&(c=!0),o[u]=r}return c?l(o):o},h=(n,t,e)=>n[t](e),d=n=>function(){return n},p=function(n,t,e,r,o){return t==c?((n,t,e,r)=>function(o){return n(o,t,e,r)})(n,e,r,o):e==c?((n,t,e,r)=>function(o){return n(t,o,e,r)})(n,t,r,o):r==c?((n,t,e,r)=>function(o){return n(t,e,o,r)})(n,t,e,o):((n,t,e,r)=>function(o){return n(t,e,r,o)})(n,t,e,r)},w=(n,t,e,r)=>n[t](e,r),y=t=>new Promise((t=>function(e){const r={};let o=0;for(const u in t){const c=t[u];n(c)?(o+=1,c.then((n=>function(t){r[n]=t,o-=1,0==o&&e(r)})(u))):r[u]=c}0==o&&e(r)})(t)),v=(t,e)=>function(...r){const o=t(...r);return n(o)?o.then(e):e(o)},g=function(n,t,e){return n[t]=e,n},x=async function(t,e,r,o){const u=t.length;for(;++o<u;){const u=e(t[o],o);r[o]=n(u)?await u:u}return r},S=function(t,e){const r=t.length,o=Array(r);let u=-1;for(;++u<r;){const r=e(t[u],u);if(n(r))return r.then(v(s(g,o,u,c),p(x,t,e,c,u)));o[u]=r}return o},b=(n,t,e,r,o)=>function(){return n(t,e,r,o)},j=async function(t,e,r,o){for(const u in t){if(u in o)continue;let c=e(t[u]);n(c)&&(c=await c),r[u]=c}return r},m=(n,t,e,r)=>function(){return n(t,e,r)},M=function(n,t){return n.add(t),n},T=async function(t,e,r){let o=t.next();for(;!o.done;){let u=e(o.value);n(u)&&(u=await u),r.add(u),o=t.next()}return r},z=function(n,t,e){return n.set(t,e)},E=async function(t,e,r){let o=t.next();for(;!o.done;){let u=e(o.value[1]);n(u)&&(u=await u),r.set(o.value[0],u),o=t.next()}return r},O=n=>function(...t){return n(...t),t[0]},A=Promise.race.bind(Promise),P=async function(t,e,r,o,u,c){const i=t.length;for(;++u<i;){c.size>=r&&await A(c);const i=e(t[u]);if(n(i)){const n=i.then(O((()=>c.delete(n))));c.add(n),o[u]=n}else o[u]=i}return l(o)},$=function(t,e,r){const o=t.length,u=Array(o);let c=-1;for(;++c<o;){const o=r(t[c]);if(n(o)){const n=new Set,i=o.then(O((()=>n.delete(i))));return n.add(i),u[c]=i,P(t,r,e,u,c,n)}u[c]=o}return u},I=async function(t,e,r,o,u,c){let i=e.next();for(;!i.done;){c.size>=r&&await A(c);const s=o(i.value,i.value,t);if(n(s)){const n=s.then((t=>{c.delete(n),u.add(t)}));c.add(n)}else u.add(s);i=e.next()}return c.size>0&&await l(c),u},k=async function(t,e,r,o,u,c){let i=e.next();for(;!i.done;){c.size>=r&&await A(c);const s=i.value[0],f=o(i.value[1],s,t);if(n(f)){u.set(s,f);const n=f.then((t=>{c.delete(n),u.set(s,t)}));c.add(n)}else u.set(s,f);i=e.next()}return c.size>0&&await l(c),u},q=async function(t,e,r,o,u,c){for(const i in t){if(i in u)continue;c.size>=e&&await A(c);const s=r(t[i],i,t);if(n(s)){o[i]=s;const n=s.then((t=>{c.delete(n),o[i]=t}));c.add(n)}else o[i]=s}return c.size>0&&await l(c),o},B=(n,t,e)=>function(...r){const o=e.length,u=r.length,i=[];let s=-1,f=-1,l=0;for(;++s<o;){const o=e[s];if(o==c&&(f+=1)<u){const n=r[f];n==c&&(l+=1),i.push(n)}else i.push(o);if(i.length==n)return 0==l?t(...i):C(n,t,i)}for(;++f<u;){const e=r[f];if(e==c&&(l+=1),i.push(e),i.length==n)return 0==l?t(...i):C(n,t,i)}return C(n,t,i)},C=function(n,t,e){const r=e.length;if(r<n)return B(n,t,e);let o=-1;for(;++o<r;){if(e[o]==c)return B(n,t,e)}return t(...e)},D=n=>function([t,e]){return n(t,e)},F=function(t,e){const r=new Map,o=[];return t.forEach(((t,e,r)=>function(o,u){const c=t([u,o]);n(c)?r.push(c.then(D(C(3,z,[e])))):e.set(c[0],c[1])})(e,r,o)),0==o.length?r:l(o).then(d(r))},G=function(r,i){return f(r)?a(r,i):null==r?r:"function"==typeof r.then?r.then(i):"function"==typeof r.map?r.map(i):"string"==typeof r||r.constructor==String?function(t,e){const r=a(t,e);return n(r)?r.then(s(h,c,"join","")):r.join("")}(r,i):r.constructor==Set?function(t,e){const r=new Set,o=[];for(const u of t){const i=e(u,u,t);n(i)?o.push(i.then(s(h,r,"add",c))):r.add(i)}return 0==o.length?r:l(o).then(d(r))}(r,i):r.constructor==Map?function(t,e){const r=new Map,o=[];for(const[u,i]of t){const s=e(i,u,t);n(s)?o.push(s.then(p(w,r,"set",u,c))):r.set(u,s)}return 0==o.length?r:l(o).then(d(r))}(r,i):"function"==typeof r[t]?e(r[t](),i):"function"==typeof r[o]?u(r[o](),i):r.constructor==Object?function(t,e){const r={};let o=!1;for(const u in t){const c=e(t[u],u,t);n(c)&&(o=!0),r[u]=c}return o?y(r):r}(r,i):i(r)},H=function(t,e){return null==e?i(G,c,t):n(t)?t.then(i(G,c,e)):G(t,e)},J=(t,e)=>{if(null==t)throw new TypeError("value is not an Object or Map");if(t.constructor==Object)return function(t,e){const r={},o=[];for(const u in t){const c=e([u,t[u]]);n(c)?o.push(c.then(D(C(3,g,[r])))):r[c[0]]=c[1]}return 0==o.length?r:l(o).then(d(r))}(t,e);if(t.constructor==Map)return F(t,e);throw new TypeError("value is not an Object or Map")};H.entries=function(t,e){return null==e?i(J,c,t):n(t)?t.then(i(J,c,e)):J(t,e)};const K=function(e,r){if(f(e))return S(e,r);if(null==e)throw new TypeError(`invalid collection ${e}`);if("string"==typeof e||e.constructor==String)return function(t,e){const r=S(t,e);return n(r)?r.then(s(h,c,"join","")):r.join("")}(e,r);if(e.constructor==Set)return function(e,r){const o=new Set,u=e[t]();let s=u.next();for(;!s.done;){const t=r(s.value);if(n(t))return t.then(v(i(M,o,c),m(T,u,r,o)));o.add(t),s=u.next()}return o}(e,r);if(e.constructor==Map)return function(e,r){const o=new Map,u=e[t]();let i=u.next();for(;!i.done;){const t=i.value[0],e=r(i.value[1]);if(n(e))return e.then(v(s(z,o,t,c),m(E,u,r,o)));o.set(t,e),i=u.next()}return o}(e,r);if(e.constructor==Object)return function(t,e){const r={},o={};for(const u in t){o[u]=!0;const i=e(t[u],u,t);if(n(i))return i.then(v(s(g,r,u,c),b(j,t,e,r,o)));r[u]=i}return r}(e,r);throw new TypeError(`invalid collection ${e}`)};H.series=function(t,e){return null==e?i(K,c,t):n(t)?t.then(i(K,c,e)):K(t,e)};const L=function(e,r,o){if(f(e))return $(e,r,o);if(null==e)throw new TypeError(`invalid collection ${e}`);if("string"==typeof e||e.constructor==String)return function(t,e,r){const o=$(t,e,r);return n(o)?o.then(s(h,c,"join","")):o.join("")}(e,r,o);if(e.constructor==Set)return function(e,r,o){const u=new Set,c=e[t]();let i=c.next();for(;!i.done;){const t=o(i.value,i.value,e);if(n(t)){const n=new Set,i=t.then((t=>{n.delete(i),u.add(t)}));return n.add(i),I(e,c,r,o,u,n)}u.add(t),i=c.next()}return u}(e,r,o);if(e.constructor==Map)return function(e,r,o){const u=new Map,c=e[t]();let i=c.next();for(;!i.done;){const t=i.value[0],s=o(i.value[1],t,e);if(n(s)){const n=new Set;u.set(t,s);const i=s.then((e=>{n.delete(i),u.set(t,e)}));return n.add(i),k(e,c,r,o,u,n)}u.set(t,s),i=c.next()}return u}(e,r,o);if(e.constructor==Object)return function(t,e,r){const o={},u={};for(const c in t){u[c]=!0;const i=r(t[c],c,t);if(n(i)){const n=new Set;o[c]=i;const s=i.then((t=>{n.delete(s),o[c]=t}));return n.add(s),q(t,e,r,o,u,n)}o[c]=i}return o}(e,r,o);throw new TypeError(`invalid collection ${e}`)};return H.pool=function(t,e,r){return null==r?s(L,c,t,e):n(t)?t.then(s(L,c,e,r)):L(t,e,r)},H}());