UNPKG

rubico

Version:

[a]synchronous functional programming

10 lines (8 loc) 8.95 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.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=function(t){if(l(t)){const e=t.length;let r=-1;for(;++r<e;){const e=t[r];if(n(e))return!0}return!1}for(const e in t){const r=t[e];if(n(r))return!0}return!1},C=(n,t,e)=>n.apply(t,e),D=(n,t,e,r)=>function(...o){const u=r.length,c=o.length,s=[];let l=-1,h=-1,d=0;for(;++l<u;){const u=r[l];if(u==i&&(h+=1)<c){const n=o[h];n==i&&(d+=1),s.push(n)}else s.push(u);if(s.length==n)return B(s)?0==d?a(s).then(f(C,t,e,i)):a(s).then(y(F,n,t,e,i)):0==d?t.apply(e,s):F(n,t,e,s)}for(;++h<c;){const r=o[h];if(r==i&&(d+=1),s.push(r),s.length==n)return B(s)?0==d?a(s).then(f(C,t,e,i)):a(s).then(y(F,n,t,e,i)):0==d?t.apply(e,s):F(n,t,e,s)}return B(s)?a(s).then(y(F,n,t,e,i)):F(n,t,e,s)},F=function(n,t,e,r){const o=r.length;if(o<n)return D(n,t,e,r);let u=-1;for(;++u<o;){if(r[u]==i)return D(n,t,e,r)}return t.apply(e,r)},G=n=>function([t,e]){return n(t,e)},H=(t,e,r)=>function(o,u){const c=t([u,o]);n(c)?r.push(c.then(G(F(3,T,this,[e])))):e.set(c[0],c[1])},J=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)},K=function(t,e){return null==e?s(J,i,t):n(t)?t.then(s(J,i,e)):J(t,e)},L=(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(G(F(3,v,this,[r])))):r[c[0]]=c[1]}return 0==o.length?r:a(o).then(p(r))}(t,e);if(t.constructor==Map)return function(n,t){const e=new Map,r=[];return n.forEach(H(t,e,r)),0==r.length?e:a(r).then(p(e))}(t,e);throw new TypeError("value is not an Object or Map")};K.entries=function(t,e){return null==e?s(L,i,t):n(t)?t.then(s(L,i,e)):L(t,e)};const N=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}`)};K.series=function(t,e){return null==e?s(N,i,t):n(t)?t.then(s(N,i,e)):N(t,e)};const Q=function(t,r,o){if(l(t))return $(t,r,o);if(null==t)throw new TypeError(`invalid functor ${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 functor ${t}`)};K.pool=function(t,e,r){return null==r?f(Q,i,t,e):n(t)?t.then(f(Q,i,e,r)):Q(t,e,r)};const R=/[.|[|\]]+/,U=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(R):e?n.slice(1).split(R):r?n.slice(0,t).split(R):n.split(R)},500),V=function(n,t){const e=(n=>l(n)?n:"string"==typeof n?U(n):[n])(t),r=e.length;let o=-1,u=n;for(;++o<r;)if(u=u[e[o]],null==u)return;return u},W=function(n,t,e){const r=null==n?void 0:V(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(W,i,o,u):n(o)?o.then(f(W,i,u,c)):W(o,u,c);var o,u,c;return 0==t.length?K(r):K(t[0],r)}}());