UNPKG

rubico

Version:

[a]synchronous functional programming

8 lines (7 loc) 3.78 kB
/** * rubico v2.6.2 * https://github.com/a-synchronous/rubico * (c) 2019-2024 Richard Tong * rubico may be freely distributed under the MIT license. */ const isPromise=r=>null!=r&&"function"==typeof r.then,isArray=Array.isArray,areAnyValuesPromises=function(r){if(isArray(r)){const n=r.length;let e=-1;for(;++e<n;){const n=r[e];if(isPromise(n))return!0}return!1}for(const n in r){const e=r[n];if(isPromise(e))return!0}return!1},areAllValuesNonfunctions=function(r){if(isArray(r)){const n=r.length;let e=-1;for(;++e<n;)if("function"==typeof r[e])return!1;return!0}for(const n in r)if("function"==typeof r[n])return!1;return!0},promiseAll=Promise.all.bind(Promise),promiseObjectAllExecutor=r=>function(n){const e={};let t=0;for(const o in r){const u=r[o];isPromise(u)?(t+=1,u.then((r=>function(o){e[r]=o,t-=1,0==t&&n(e)})(o))):e[o]=u}0==t&&n(e)},promiseObjectAll=r=>new Promise(promiseObjectAllExecutor(r)),__=Symbol.for("placeholder"),curry2ResolveArg0=(r,n)=>function(e){return r(e,n)},curry2ResolveArg1=(r,n)=>function(e){return r(n,e)},curry2=function(r,n,e){return n==__?curry2ResolveArg0(r,e):curry2ResolveArg1(r,n)},curryArgs2ResolveArgs0=(r,n,e)=>function(...e){return r(e,n)},curryArgs2ResolveArgs1=(r,n,e)=>function(...e){return r(n,e)},curryArgs2=function(r,n,e){return n==__?curryArgs2ResolveArgs0(r,e):curryArgs2ResolveArgs1(r,n)},functionArrayAll=function(r,n){const e=r.length,t=Array(e);let o=-1,u=!1;for(;++o<e;){const e=r[o],s="function"==typeof e?e(...n):e;isPromise(s)&&(u=!0),t[o]=s}return u?promiseAll(t):t},funcConcat=(r,n)=>function(...e){const t=r(...e);return isPromise(t)?t.then(n):n(t)},curry3ResolveArg0=(r,n,e)=>function(t){return r(t,n,e)},curry3ResolveArg1=(r,n,e)=>function(t){return r(n,t,e)},curry3ResolveArg2=(r,n,e)=>function(t){return r(n,e,t)},curry3=function(r,n,e,t){return n==__?curry3ResolveArg0(r,e,t):e==__?curry3ResolveArg1(r,n,t):curry3ResolveArg2(r,n,e)},curry4ResolveArg0=(r,n,e,t)=>function(o){return r(o,n,e,t)},curry4ResolveArg1=(r,n,e,t)=>function(o){return r(n,o,e,t)},curry4ResolveArg2=(r,n,e,t)=>function(o){return r(n,e,o,t)},curry4ResolveArg3=(r,n,e,t)=>function(o){return r(n,e,t,o)},curry4=function(r,n,e,t,o){return n==__?curry4ResolveArg0(r,e,t,o):e==__?curry4ResolveArg1(r,n,t,o):t==__?curry4ResolveArg2(r,n,e,o):curry4ResolveArg3(r,n,e,t)},objectSet=function(r,n,e){return r[n]=e,r},asyncFunctionArrayAllSeries=async function(r,n,e,t){const o=r.length;for(;++t<o;){const o=r[t](...n);e[t]=isPromise(o)?await o:o}return e},functionArrayAllSeries=function(r,n){const e=r.length,t=[];let o=-1;for(;++o<e;){const e=r[o](...n);if(isPromise(e))return e.then(funcConcat(curry3(objectSet,t,o,__),curry4(asyncFunctionArrayAllSeries,r,n,__,o)));t[o]=e}return t},always=r=>function(){return r},functionObjectAll=function(r,n){const e={},t=[];for(const o in r){const u=r[o],s="function"==typeof u?u(...n):u;isPromise(s)?t.push(s.then(curry3(objectSet,e,o,__))):e[o]=s}return 0==t.length?e:promiseAll(t).then(always(e))},_allValues=function(r){return isArray(r)?areAnyValuesPromises(r)?promiseAll(r):r:areAnyValuesPromises(r)?promiseObjectAll(r):r},all=function(...r){if(1==r.length){const n=r[0];return isPromise(n)?n.then(_allValues):areAllValuesNonfunctions(n)?_allValues(n):isArray(n)?curryArgs2(functionArrayAll,n,__):curryArgs2(functionObjectAll,n,__)}const n=r[r.length-1],e=r.slice(0,-1);return areAnyValuesPromises(e)?isArray(n)?promiseAll(e).then(curry2(functionArrayAll,n,__)):promiseAll(e).then(curry2(functionObjectAll,n,__)):isArray(n)?functionArrayAll(n,e):functionObjectAll(n,e)};all.series=function(...r){const n=r.pop();return 0==r.length?curryArgs2(functionArrayAllSeries,n,__):areAnyValuesPromises(r)?promiseAll(r).then(curry2(functionArrayAllSeries,n,__)):functionArrayAllSeries(n,r)};export default all;