rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 2.7 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.all=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=Array.isArray,r=function(r){if(t(r)){const t=r.length;let o=-1;for(;++o<t;){const t=r[o];if(n(t))return!0}return!1}for(const t in r){const o=r[t];if(n(o))return!0}return!1},o=Promise.all.bind(Promise),e=t=>new Promise((t=>function(r){const o={};let e=0;for(const u in t){const c=t[u];n(c)?(e+=1,c.then((n=>function(t){o[n]=t,e-=1,0==e&&r(o)})(u))):o[u]=c}0==e&&r(o)})(t)),u=Symbol.for("placeholder"),c=function(n,t,r){return t==u?((n,t)=>function(r){return n(r,t)})(n,r):((n,t)=>function(r){return n(t,r)})(n,t)},f=function(n,t,r){return t==u?((n,t)=>function(...r){return n(r,t)})(n,r):((n,t)=>function(...r){return n(t,r)})(n,t)},i=function(t,r){const e=t.length,u=Array(e);let c=-1,f=!1;for(;++c<e;){const o=t[c],e="function"==typeof o?o(...r):o;n(e)&&(f=!0),u[c]=e}return f?o(u):u},s=(t,r)=>function(...o){const e=t(...o);return n(e)?e.then(r):r(e)},l=function(n,t,r,o){return t==u?((n,t,r)=>function(o){return n(o,t,r)})(n,r,o):r==u?((n,t,r)=>function(o){return n(t,o,r)})(n,t,o):((n,t,r)=>function(o){return n(t,r,o)})(n,t,r)},h=function(n,t,r){return n[t]=r,n},a=async function(t,r,o,e){const u=t.length;for(;++e<u;){const u=t[e](...r);o[e]=n(u)?await u:u}return o},p=function(t,r){const o=t.length,e=[];let c=-1;for(;++c<o;){const o=t[c](...r);if(n(o))return o.then(s(l(h,e,c,u),(f=a,p=r,y=u,g=c,(i=t)==u?((n,t,r,o)=>function(e){return n(e,t,r,o)})(f,p,y,g):p==u?((n,t,r,o)=>function(e){return n(t,e,r,o)})(f,i,y,g):y==u?((n,t,r,o)=>function(e){return n(t,r,e,o)})(f,i,p,g):((n,t,r,o)=>function(e){return n(t,r,o,e)})(f,i,p,y))));e[c]=o}var f,i,p,y,g;return e},y=function(t,r){const e={},c=[];for(const o in t){const f=t[o],i="function"==typeof f?f(...r):f;n(i)?c.push(i.then(l(h,e,o,u))):e[o]=i}return 0==c.length?e:o(c).then((f=e,function(){return f}));var f},g=function(n){return t(n)?r(n)?o(n):n:r(n)?e(n):n},b=function(...e){if(1==e.length){const r=e[0];return n(r)?r.then(g):function(n){if(t(n)){const t=n.length;let r=-1;for(;++r<t;)if("function"==typeof n[r])return!1;return!0}for(const t in n)if("function"==typeof n[t])return!1;return!0}(r)?g(r):t(r)?f(i,r,u):f(y,r,u)}const s=e[e.length-1],l=e.slice(0,-1);return r(l)?t(s)?o(l).then(c(i,s,u)):o(l).then(c(y,s,u)):t(s)?i(s,l):y(s,l)};return b.series=function(...n){const t=n.pop();return 0==n.length?f(p,t,u):r(n)?o(n).then(c(p,t,u)):p(t,n)},b}());