UNPKG

rubico

Version:

[a]synchronous functional programming

8 lines (7 loc) 2.47 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 promiseAll=Promise.all.bind(Promise),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},__=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)},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)},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)},thunkConditional=(r,n,e)=>r?n():e(),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},thunkify2=(r,n,e)=>function(){return r(n,e)},thunkify3=(r,n,e,t)=>function(){return r(n,e,t)},always=r=>function(){return r},areAllValuesTruthy=function(r,n){const e=r.length;for(;++n<e;){const e=r[n];if(isPromise(e))return e.then(curry3(thunkConditional,__,thunkify2(areAllValuesTruthy,r,n),always(!1)));if(!e)return!1}return!0},asyncArePredicatesAllTruthy=async function(r,n,e){const t=n.length;for(;++e<t;){let t=n[e];if("function"==typeof t&&(t=t(...r)),isPromise(t)&&(t=await t),!t)return!1}return!0},areAllPredicatesTruthy=function(r,n){const e=n.length;let t=-1;for(;++t<e;){let e=n[t];if("function"==typeof e&&(e=e(...r)),isPromise(e))return e.then(curry3(thunkConditional,__,thunkify3(asyncArePredicatesAllTruthy,r,n,t),always(!1)));if(!e)return!1}return!0},and=function(...r){const n=r.pop();return areAllValuesNonfunctions(n)?areAllValuesTruthy(n,-1):0==r.length?curryArgs2(areAllPredicatesTruthy,__,n):areAnyValuesPromises(r)?promiseAll(r).then(curry2(areAllPredicatesTruthy,__,n)):areAllPredicatesTruthy(r,n)};export default and;