UNPKG

rubico

Version:

[a]synchronous functional programming

8 lines (7 loc) 6 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,funcConcat=(r,e)=>function(...n){const t=r(...n);return isPromise(t)?t.then(e):e(t)},__=Symbol.for("placeholder"),curry2ResolveArg0=(r,e)=>function(n){return r(n,e)},curry2ResolveArg1=(r,e)=>function(n){return r(e,n)},curry2=function(r,e,n){return e==__?curry2ResolveArg0(r,n):curry2ResolveArg1(r,e)},reducerMap=(r,e)=>function(n,t){const c=e(t);return isPromise(c)?c.then(curry2(r,n,__)):r(n,c)},curry3ResolveArg0=(r,e,n)=>function(t){return r(t,e,n)},curry3ResolveArg1=(r,e,n)=>function(t){return r(e,t,n)},curry3ResolveArg2=(r,e,n)=>function(t){return r(e,n,t)},curry3=function(r,e,n,t){return e==__?curry3ResolveArg0(r,n,t):n==__?curry3ResolveArg1(r,e,t):curry3ResolveArg2(r,e,n)},thunkify2=(r,e,n)=>function(){return r(e,n)},thunkConditional=(r,e,n)=>r?e():n(),always=r=>function(){return r},reducerFilter=(r,e)=>function(n,t){const c=e(t);return isPromise(c)?c.then(curry3(thunkConditional,__,thunkify2(r,n,t),always(n))):c?r(n,t):n},isArray=Array.isArray,objectValues=Object.values,objectProto=Object.prototype,nativeObjectToString=objectProto.toString,objectToString=r=>nativeObjectToString.call(r),generatorFunctionTag="[object GeneratorFunction]",isGeneratorFunction=r=>objectToString(r)==generatorFunctionTag,asyncGeneratorFunctionTag="[object AsyncGeneratorFunction]",isAsyncGeneratorFunction=r=>objectToString(r)==asyncGeneratorFunctionTag,iteratorReduceAsync=async function(r,e,n){let t=r.next();if(t.done)return n;for(;!t.done;)n=e(n,t.value),isPromise(n)&&(n=await n),t=r.next();return n},iteratorReduce=function(r,e,n){let t=r.next();if(t.done)return n;for(void 0===n&&(n=t.value,t=r.next());!t.done;){if(n=e(n,t.value),isPromise(n))return n.then(curry3(iteratorReduceAsync,r,e,__));t=r.next()}return n},asyncIteratorReduce=async function(r,e,n){let t=await r.next();if(t.done)return n;for(void 0===n&&(n=t.value,t=await r.next());!t.done;)n=await e(n,t.value),t=await r.next();return n},symbolIterator=Symbol.iterator,symbolAsyncIterator=Symbol.asyncIterator,curryArgs3ResolveArgs0=(r,e,n)=>function(...t){return r(t,e,n)},curryArgs3ResolveArgs1=(r,e,n)=>function(...t){return r(e,t,n)},curryArgs3ResolveArgs2=(r,e,n)=>function(...t){return r(e,n,t)},curryArgs3=function(r,e,n,t){return e==__?curryArgs3ResolveArgs0(r,n,t):n==__?curryArgs3ResolveArgs1(r,e,t):curryArgs3ResolveArgs2(r,e,n)},curry4ResolveArg0=(r,e,n,t)=>function(c){return r(c,e,n,t)},curry4ResolveArg1=(r,e,n,t)=>function(c){return r(e,c,n,t)},curry4ResolveArg2=(r,e,n,t)=>function(c){return r(e,n,c,t)},curry4ResolveArg3=(r,e,n,t)=>function(c){return r(e,n,t,c)},curry4=function(r,e,n,t,c){return e==__?curry4ResolveArg0(r,n,t,c):n==__?curry4ResolveArg1(r,e,t,c):t==__?curry4ResolveArg2(r,e,n,c):curry4ResolveArg3(r,e,n,t)},arrayReduceAsync=async function(r,e,n,t){const c=r.length;for(;++t<c;)n=e(n,r[t],t,r),isPromise(n)&&(n=await n);return n},arrayReduce=function(r,e,n){const t=r.length;let c=-1;for(void 0===n&&(n=r[++c]);++c<t;)if(n=e(n,r[c],c,r),isPromise(n))return n.then(curry4(arrayReduceAsync,r,e,__,c));return n},curry5ResolveArg0=(r,e,n,t,c)=>function(o){return r(o,e,n,t,c)},curry5ResolveArg1=(r,e,n,t,c)=>function(o){return r(e,o,n,t,c)},curry5ResolveArg2=(r,e,n,t,c)=>function(o){return r(e,n,o,t,c)},curry5ResolveArg3=(r,e,n,t,c)=>function(o){return r(e,n,t,o,c)},curry5ResolveArg4=(r,e,n,t,c)=>function(o){return r(e,n,t,c,o)},curry5=function(r,e,n,t,c,o){return e==__?curry5ResolveArg0(r,n,t,c,o):n==__?curry5ResolveArg1(r,e,t,c,o):t==__?curry5ResolveArg2(r,e,n,c,o):c==__?curry5ResolveArg3(r,e,n,t,o):curry5ResolveArg4(r,e,n,t,c)},objectKeys=Object.keys,objectReduceAsync=async function(r,e,n,t,c){const o=t.length;for(;++c<o;){const o=t[c];n=e(n,r[o],o,r),isPromise(n)&&(n=await n)}return n},objectReduce=function(r,e,n){const t=objectKeys(r),c=t.length;let o=-1;for(void 0===n&&(n=r[t[++o]]);++o<c;){const c=t[o];if(n=e(n,r[c],c,r),isPromise(n))return n.then(curry5(objectReduceAsync,r,e,__,t,o))}return n},mapReduceAsync=async function(r,e,n,t){for(const[c,o]of t)n=e(n,o,c,r),isPromise(n)&&(n=await n);return n},mapReduce=function(r,e,n){const t=r.entries();if(void 0===n){const r=t.next();if(r.done)return n;n=r.value[1]}for(const[c,o]of t)if(n=e(n,o,c,r),isPromise(n))return n.then(curry4(mapReduceAsync,r,e,__,t));return n},reducerConcat=(r,e)=>function(n,t){const c=r(n,t);return isPromise(c)?c.then(curry2(e,__,t)):e(c,t)},genericReduce=function(r,e,n){return isArray(r)?arrayReduce(r,e,n):null==r?void 0===n?curry2(e,r,__):e(n,r):r.constructor==Map?mapReduce(r,e,n):"function"==typeof r[symbolIterator]?iteratorReduce(r[symbolIterator](),e,n):"function"==typeof r[symbolAsyncIterator]?asyncIteratorReduce(r[symbolAsyncIterator](),e,n):"function"==typeof r.reduce?r.reduce(e,n):"function"==typeof r.chain?r.chain(curry2(e,n,__)):"function"==typeof r.flatMap?r.flatMap(curry2(e,n,__)):r.constructor==Object?objectReduce(r,e,n):void 0===n?curry2(e,r,__):e(n,r)},reducerFlatMap=(r,e)=>function(n,t){const c=e(t);return isPromise(c)?c.then(curry3(genericReduce,__,r,n)):genericReduce(c,r,n)},reducerForEach=(r,e)=>function(n,t){const c=e(t);return isPromise(c)?c.then(thunkify2(r,n,t)):r(n,t)},_reducerTryCatchErrorHandler=function(r,e,n,t,c){const o=r(n,c);return isPromise(o)?o.then(curry2(e,t,__)):e(t,o)},reducerTryCatch=function(r,e,n){const t=e(r);return function(e,c){try{const o=t(e,c);return isPromise(o)?o.catch(curry5(_reducerTryCatchErrorHandler,n,r,__,e,c)):o}catch(t){return _reducerTryCatchErrorHandler(n,r,t,e,c)}}},Transducer={map:function(r){return curry2(reducerMap,__,r)},filter:function(r){return curry2(reducerFilter,__,r)},flatMap:function(r){return curry2(reducerFlatMap,__,r)},forEach:function(r){return curry2(reducerForEach,__,r)},passthrough:function(r){return r},tryCatch:function(r,e){return curry3(reducerTryCatch,__,r,e)}};export default Transducer;