rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 8.61 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.pluck=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=(t,e)=>function(...r){const o=t(...r);return n(o)?o.then(e):e(o)},e=Symbol.iterator,r=(n,t)=>({toString:()=>"[object MappingIterator]",[e](){return this},next(){const e=n.next();return e.done?e:{value:t(e.value),done:!1}}}),o=n=>({value:n,done:!1}),u=Symbol.asyncIterator,c=(t,e)=>({[u](){return this},async next(){const r=await t.next();if(r.done)return r;const u=e(r.value);return n(u)?u.then(o):{value:u,done:!1}}}),i=Symbol.for("placeholder"),s=function(n,t,e){return t==i?((n,t)=>function(e){return n(e,t)})(n,e):((n,t)=>function(e){return n(t,e)})(n,t)},f=function(n,t,e,r){return t==i?((n,t,e)=>function(r){return n(r,t,e)})(n,e,r):e==i?((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)},l=Array.isArray,a=Promise.all.bind(Promise),h=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?a(o):o},d=(n,t,e)=>n[t](e),p=n=>function(){return n},y=function(n,t,e,r,o){return t==i?((n,t,e,r)=>function(o){return n(o,t,e,r)})(n,e,r,o):e==i?((n,t,e,r)=>function(o){return n(t,o,e,r)})(n,t,r,o):r==i?((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),g=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=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},b=function(e,r){const o=e.length,u=Array(o);let c=-1;for(;++c<o;){const o=r(e[c],c);if(n(o))return o.then(t(f(v,u,c,i),y(x,e,r,i,c)));u[c]=o}return u},S=(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},z=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},T=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 a(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 a(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 a(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 a(c),o},B=(n,t,e)=>function(...r){const o=e.length,u=r.length,c=[];let s=-1,f=-1,l=0;for(;++s<o;){const o=e[s];if(o==i&&(f+=1)<u){const n=r[f];n==i&&(l+=1),c.push(n)}else c.push(o);if(c.length==n)return 0==l?t(...c):C(n,t,c)}for(;++f<u;){const e=r[f];if(e==i&&(l+=1),c.push(e),c.length==n)return 0==l?t(...c):C(n,t,c)}return C(n,t,c)},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]==i)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,T,[e])))):e.set(c[0],c[1])})(e,r,o)),0==o.length?r:a(o).then(p(r))},G=function(t,o){return l(t)?h(t,o):null==t?t:"function"==typeof t.then?t.then(o):"function"==typeof t.map?t.map(o):"string"==typeof t||t.constructor==String?function(t,e){const r=h(t,e);return n(r)?r.then(f(d,i,"join","")):r.join("")}(t,o):t.constructor==Set?function(t,e){const r=new Set,o=[];for(const u of t){const c=e(u,u,t);n(c)?o.push(c.then(f(d,r,"add",i))):r.add(c)}return 0==o.length?r:a(o).then(p(r))}(t,o):t.constructor==Map?function(t,e){const r=new Map,o=[];for(const[u,c]of t){const s=e(c,u,t);n(s)?o.push(s.then(y(w,r,"set",u,i))):r.set(u,s)}return 0==o.length?r:a(o).then(p(r))}(t,o):"function"==typeof t[e]?r(t[e](),o):"function"==typeof t[u]?c(t[u](),o):t.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?g(r):r}(t,o):o(t)},H=function(t,e){return null==e?s(G,i,t):n(t)?t.then(s(G,i,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,v,[r])))):r[c[0]]=c[1]}return 0==o.length?r:a(o).then(p(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?s(J,i,t):n(t)?t.then(s(J,i,e)):J(t,e)};const K=function(r,o){if(l(r))return b(r,o);if(null==r)throw new TypeError(`invalid collection ${r}`);if("string"==typeof r||r.constructor==String)return function(t,e){const r=b(t,e);return n(r)?r.then(f(d,i,"join","")):r.join("")}(r,o);if(r.constructor==Set)return function(r,o){const u=new Set,c=r[e]();let f=c.next();for(;!f.done;){const e=o(f.value);if(n(e))return e.then(t(s(M,u,i),m(z,c,o,u)));u.add(e),f=c.next()}return u}(r,o);if(r.constructor==Map)return function(r,o){const u=new Map,c=r[e]();let s=c.next();for(;!s.done;){const e=s.value[0],r=o(s.value[1]);if(n(r))return r.then(t(f(T,u,e,i),m(E,c,o,u)));u.set(e,r),s=c.next()}return u}(r,o);if(r.constructor==Object)return function(e,r){const o={},u={};for(const c in e){u[c]=!0;const s=r(e[c],c,e);if(n(s))return s.then(t(f(v,o,c,i),S(j,e,r,o,u)));o[c]=s}return o}(r,o);throw new TypeError(`invalid collection ${r}`)};H.series=function(t,e){return null==e?s(K,i,t):n(t)?t.then(s(K,i,e)):K(t,e)};const L=function(t,r,o){if(l(t))return $(t,r,o);if(null==t)throw new TypeError(`invalid collection ${t}`);if("string"==typeof t||t.constructor==String)return function(t,e,r){const o=$(t,e,r);return n(o)?o.then(f(d,i,"join","")):o.join("")}(t,r,o);if(t.constructor==Set)return function(t,r,o){const u=new Set,c=t[e]();let i=c.next();for(;!i.done;){const e=o(i.value,i.value,t);if(n(e)){const n=new Set,i=e.then((t=>{n.delete(i),u.add(t)}));return n.add(i),I(t,c,r,o,u,n)}u.add(e),i=c.next()}return u}(t,r,o);if(t.constructor==Map)return function(t,r,o){const u=new Map,c=t[e]();let i=c.next();for(;!i.done;){const e=i.value[0],s=o(i.value[1],e,t);if(n(s)){const n=new Set;u.set(e,s);const i=s.then((t=>{n.delete(i),u.set(e,t)}));return n.add(i),k(t,c,r,o,u,n)}u.set(e,s),i=c.next()}return u}(t,r,o);if(t.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}(t,r,o);throw new TypeError(`invalid collection ${t}`)};H.pool=function(t,e,r){return null==r?f(L,i,t,e):n(t)?t.then(f(L,i,e,r)):L(t,e,r)};const N=/[.|[|\]]+/,Q=function(n,t){const e=new Map,r=function(r){if(e.has(r))return e.get(r);const o=n(r);return e.set(r,o),e.size>t&&e.clear(),o};return r.cache=e,r}((function(n){const t=n.length-1,e="["==n[0],r="]"==n[t];return e&&r?n.slice(1,t).split(N):e?n.slice(1).split(N):r?n.slice(0,t).split(N):n.split(N)}),500),R=function(n,t){const e=(n=>l(n)?n:"string"==typeof n?Q(n):[n])(t),r=e.length;let o=-1,u=n;for(;++o<r;)if(u=u[e[o]],null==u)return;return u},U=function(n,t,e){const r=null==n?void 0:R(n,t);return void 0===r?"function"==typeof e?e(n):e:r};return function(...t){const e=t.pop(),r="string"==typeof(o=e)||"number"==typeof o||l(o)?f(U,i,o,u):n(o)?o.then(f(U,i,u,c)):U(o,u,c);var o,u,c;return 0==t.length?H(r):H(t[0],r)}}());