rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 3.28 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.reduce=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=Symbol.for("placeholder"),e=function(n,e,r,o){return e==t?((n,t,e)=>function(r){return n(r,t,e)})(n,r,o):r==t?((n,t,e)=>function(r){return n(t,r,e)})(n,e,o):((n,t,e)=>function(r){return n(t,e,r)})(n,e,r)},r=Array.isArray,o=(Object.values,Object.prototype.toString,async function(t,e,r){let o=t.next();if(o.done)return r;for(;!o.done;)r=e(r,o.value),n(r)&&(r=await r),o=t.next();return r}),u=Symbol.iterator,f=Symbol.asyncIterator,i=function(n,e,r){return e==t?((n,t)=>function(e){return n(e,t)})(n,r):((n,t)=>function(e){return n(t,e)})(n,e)},c=function(n,e,r,o,u){return e==t?((n,t,e,r)=>function(o){return n(o,t,e,r)})(n,r,o,u):r==t?((n,t,e,r)=>function(o){return n(t,o,e,r)})(n,e,o,u):o==t?((n,t,e,r)=>function(o){return n(t,e,o,r)})(n,e,r,u):((n,t,e,r)=>function(o){return n(t,e,r,o)})(n,e,r,o)},a=async function(t,e,r,o){const u=t.length;for(;++o<u;)r=e(r,t[o],o,t),n(r)&&(r=await r);return r},l=Object.keys,s=async function(t,e,r,o,u){const f=o.length;for(;++u<f;){const f=o[u];r=e(r,t[f],f,t),n(r)&&(r=await r)}return r},y=async function(t,e,r,o){for(const[u,f]of o)r=e(r,f,u,t),n(r)&&(r=await r);return r},d=function(d,h,p){return r(d)?function(e,r,o){const u=e.length;let f=-1;for(void 0===o&&(o=e[++f]);++f<u;)if(o=r(o,e[f],f,e),n(o))return o.then(c(a,e,r,t,f));return o}(d,h,p):null==d?void 0===p?i(h,d,t):h(p,d):d.constructor==Map?function(e,r,o){const u=e.entries();if(void 0===o){const n=u.next();if(n.done)return o;o=n.value[1]}for(const[f,i]of u)if(o=r(o,i,f,e),n(o))return o.then(c(y,e,r,t,u));return o}(d,h,p):"function"==typeof d[u]?function(r,u,f){let i=r.next();if(i.done)return f;for(void 0===f&&(f=i.value,i=r.next());!i.done;){if(f=u(f,i.value),n(f))return f.then(e(o,r,u,t));i=r.next()}return f}(d[u](),h,p):"function"==typeof d[f]?async function(n,t,e){let r=await n.next();if(r.done)return e;for(void 0===e&&(e=r.value,r=await n.next());!r.done;)e=await t(e,r.value),r=await n.next();return e}(d[f](),h,p):"function"==typeof d.reduce?d.reduce(h,p):"function"==typeof d.chain?d.chain(i(h,p,t)):"function"==typeof d.flatMap?d.flatMap(i(h,p,t)):d.constructor==Object?function(e,r,o){const u=l(e),f=u.length;let i=-1;for(void 0===o&&(o=e[u[++i]]);++i<f;){const f=u[i];if(o=r(o,e[f],f,e),n(o))return o.then((c=s,y=r,d=t,h=u,p=i,(a=e)==t?((n,t,e,r,o)=>function(u){return n(u,t,e,r,o)})(c,y,d,h,p):y==t?((n,t,e,r,o)=>function(u){return n(t,u,e,r,o)})(c,a,d,h,p):d==t?((n,t,e,r,o)=>function(u){return n(t,e,u,r,o)})(c,a,y,h,p):h==t?((n,t,e,r,o)=>function(u){return n(t,e,r,u,o)})(c,a,y,d,p):((n,t,e,r,o)=>function(u){return n(t,e,r,o,u)})(c,a,y,d,h)))}var c,a,y,d,h,p;return o}(d,h,p):void 0===p?i(h,d,t):h(p,d)},h=function(r,o,u){if("function"==typeof u){const f=u(r);return n(f)?f.then(e(d,r,o,t)):d(r,o,f)}return n(u)?u.then(e(d,r,o,t)):d(r,o,u)};return function(...r){return"function"==typeof r[0]?e(h,t,r[0],r[1]):n(r[0])?r[0].then(e(h,t,r[1],r[2])):h(r[0],r[1],r[2])}}());