UNPKG

rubico

Version:

[a]synchronous functional programming

10 lines (8 loc) 3.87 kB
/** * Rubico v2.8.2 * https://rubico.land/ * * © Richard Yufei Tong, King of Software * Rubico may be freely distributed under the CFOSS license. */ !function(n,t){"object"==typeof module?module.exports=t:"function"==typeof define?define(()=>t):n.Transducer=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=Symbol.for("placeholder"),r=function(n,r,e){return r==t?((n,t)=>function(r){return n(r,t)})(n,e):((n,t)=>function(r){return n(t,r)})(n,r)},e=(e,o)=>function(u,c){const f=o(c);return n(f)?f.then(r(e,u,t)):e(u,f)},o=function(n,r,e,o){return r==t?((n,t,r)=>function(e){return n(e,t,r)})(n,e,o):e==t?((n,t,r)=>function(e){return n(t,e,r)})(n,r,o):((n,t,r)=>function(e){return n(t,r,e)})(n,r,e)},u=(n,t,r)=>function(){return n(t,r)},c=(n,t,r)=>n?t():r(),f=(r,e)=>function(f,i){const a=e(i);return n(a)?a.then(o(c,t,u(r,f,i),(s=f,function(){return s}))):a?r(f,i):f;var s},i=Array.isArray,a=(Object.values,Object.prototype.toString,async function(t,r,e){let o=t.next();if(o.done)return e;for(;!o.done;)e=r(e,o.value),n(e)&&(e=await e),o=t.next();return e}),s=Symbol.iterator,l=Symbol.asyncIterator,h=function(n,r,e,o,u){return r==t?((n,t,r,e)=>function(o){return n(o,t,r,e)})(n,e,o,u):e==t?((n,t,r,e)=>function(o){return n(t,o,r,e)})(n,r,o,u):o==t?((n,t,r,e)=>function(o){return n(t,r,o,e)})(n,r,e,u):((n,t,r,e)=>function(o){return n(t,r,e,o)})(n,r,e,o)},y=async function(t,r,e,o){const u=t.length;for(;++o<u;)e=r(e,t[o],o,t),n(e)&&(e=await e);return e},d=function(n,r,e,o,u,c){return r==t?((n,t,r,e,o)=>function(u){return n(u,t,r,e,o)})(n,e,o,u,c):e==t?((n,t,r,e,o)=>function(u){return n(t,u,r,e,o)})(n,r,o,u,c):o==t?((n,t,r,e,o)=>function(u){return n(t,r,u,e,o)})(n,r,e,u,c):u==t?((n,t,r,e,o)=>function(u){return n(t,r,e,u,o)})(n,r,e,o,c):((n,t,r,e,o)=>function(u){return n(t,r,e,o,u)})(n,r,e,o,u)},p=Object.keys,v=async function(t,r,e,o,u){const c=o.length;for(;++u<c;){const c=o[u];e=r(e,t[c],c,t),n(e)&&(e=await e)}return e},b=async function(t,r,e,o){for(const[u,c]of o)e=r(e,c,u,t),n(e)&&(e=await e);return e},x=function(e,u,c){return i(e)?function(r,e,o){const u=r.length;let c=-1;for(void 0===o&&(o=r[++c]);++c<u;)if(o=e(o,r[c],c,r),n(o))return o.then(h(y,r,e,t,c));return o}(e,u,c):null==e?void 0===c?r(u,e,t):u(c,e):e.constructor==Map?function(r,e,o){const u=r.entries();if(void 0===o){const n=u.next();if(n.done)return o;o=n.value[1]}for(const[c,f]of u)if(o=e(o,f,c,r),n(o))return o.then(h(b,r,e,t,u));return o}(e,u,c):"function"==typeof e[s]?function(r,e,u){let c=r.next();if(c.done)return u;for(void 0===u&&(u=c.value,c=r.next());!c.done;){if(u=e(u,c.value),n(u))return u.then(o(a,r,e,t));c=r.next()}return u}(e[s](),u,c):"function"==typeof e[l]?async function(n,t,r){let e=await n.next();if(e.done)return r;for(void 0===r&&(r=e.value,e=await n.next());!e.done;)r=await t(r,e.value),e=await n.next();return r}(e[l](),u,c):"function"==typeof e.reduce?e.reduce(u,c):"function"==typeof e.chain?e.chain(r(u,c,t)):"function"==typeof e.flatMap?e.flatMap(r(u,c,t)):e.constructor==Object?function(r,e,o){const u=p(r),c=u.length;let f=-1;for(void 0===o&&(o=r[u[++f]]);++f<c;){const c=u[f];if(o=e(o,r[c],c,r),n(o))return o.then(d(v,r,e,t,u,f))}return o}(e,u,c):void 0===c?r(u,e,t):u(c,e)},g=(r,e)=>function(u,c){const f=e(c);return n(f)?f.then(o(x,t,r,u)):x(f,r,u)},w=(t,r)=>function(e,o){const c=r(o);return n(c)?c.then(u(t,e,o)):t(e,o)},j=function(e,o,u,c,f){const i=e(u,f);return n(i)?i.then(r(o,c,t)):o(c,i)},m=function(r,e,o){const u=e(r);return function(e,c){try{const f=u(e,c);return n(f)?f.catch(d(j,o,r,t,e,c)):f}catch(n){return j(o,r,n,e,c)}}},M={map:function(n){return r(e,t,n)},filter:function(n){return r(f,t,n)},flatMap:function(n){return r(g,t,n)},forEach:function(n){return r(w,t,n)},passthrough:function(n){return n},tryCatch:function(n,r){return o(m,t,n,r)}};return M}());