rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 4.63 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.transform=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,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)},e=Array.isArray,o=ArrayBuffer.isView,u=(n,t,r)=>n[t](r),f=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)},c=function(){},i=(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}),a=Symbol.iterator,s=Symbol.asyncIterator,l=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=Object.keys,h=async function(t,r,e,o,u){const f=o.length;for(;++u<f;){const f=o[u];e=r(e,t[f],f,t),n(e)&&(e=await e)}return e},p=async function(t,r,e,o){for(const[u,f]of o)e=r(e,f,u,t),n(e)&&(e=await e);return e},v=function(o,u,c){return e(o)?function(r,e,o){const u=r.length;let f=-1;for(void 0===o&&(o=r[++f]);++f<u;)if(o=e(o,r[f],f,r),n(o))return o.then(l(y,r,e,t,f));return o}(o,u,c):null==o?void 0===c?f(u,o,t):u(c,o):o.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[f,c]of u)if(o=e(o,c,f,r),n(o))return o.then(l(p,r,e,t,u));return o}(o,u,c):"function"==typeof o[a]?function(e,o,u){let f=e.next();if(f.done)return u;for(void 0===u&&(u=f.value,f=e.next());!f.done;){if(u=o(u,f.value),n(u))return u.then(r(i,e,o,t));f=e.next()}return u}(o[a](),u,c):"function"==typeof o[s]?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}(o[s](),u,c):"function"==typeof o.reduce?o.reduce(u,c):"function"==typeof o.chain?o.chain(f(u,c,t)):"function"==typeof o.flatMap?o.flatMap(f(u,c,t)):o.constructor==Object?function(r,e,o){const u=d(r),f=u.length;let c=-1;for(void 0===o&&(o=r[u[++c]]);++c<f;){const f=u[c];if(o=e(o,r[f],f,r),n(o))return o.then((i=h,s=e,l=t,y=u,p=c,(a=r)==t?((n,t,r,e,o)=>function(u){return n(u,t,r,e,o)})(i,s,l,y,p):s==t?((n,t,r,e,o)=>function(u){return n(t,u,r,e,o)})(i,a,l,y,p):l==t?((n,t,r,e,o)=>function(u){return n(t,r,u,e,o)})(i,a,s,y,p):y==t?((n,t,r,e,o)=>function(u){return n(t,r,e,u,o)})(i,a,s,l,p):((n,t,r,e,o)=>function(u){return n(t,r,e,o,u)})(i,a,s,l,y)))}var i,a,s,l,y,p;return o}(o,u,c):void 0===c?f(u,o,t):u(c,o)},g=Object.assign,w=function(n,t){return e(t)||o(t)?function(n,t){const r=n.length,e=t.length;let o=-1;for(;++o<e;)n[r+o]=t[o];return n}(n,t):(n.push(t),n)},b="function"==typeof Buffer,j=b?Buffer.alloc:c,x=function(n,t){const r=n.length,e=b&&n.constructor==Buffer?j(r+t.length):new n.constructor(r+t.length);return e.set(n),e.set(t,r),e},m=function(n,t){return e(t)||o(t)?x(n,t):x(n,[t])},S=function(n,t){return null!=(r=t)&&"function"==typeof r.pipe?((n,t)=>new Promise(((n,t)=>function(r,e){var o,u;t.on("data",(n=>function(t,r,e){return n.write(t,r,e),n})(n)),t.on("end",(o=r,u=n,function(){return o(u)})),t.on("error",e)})(n,t)))(n,t):(n.write(t),n);var r},O=function(n,t){if(null!=t&&t.constructor==Set){for(const r of t)n.add(r);return n}return n.add(t)},B=function(n,t){return n.concat(t)},A=function(t,r,e){const o=v(t,r(c),null);return n(o)?o.then((u=e,function(){return u})):e;var u},M=function(c,i,a){if(e(a))return v(c,i(w),a);if(o(a)){const r=v(c,i(w),[]);return n(r)?r.then(f(m,a,t)):m(a,r)}if(null==a)return A(c,i,a);const s=a.constructor;if("string"==typeof a||s==String){const e=v(c,i(w),[a]);return n(e)?e.then(r(u,t,"join","")):e.join("")}return"function"==typeof a.concat?v(c,i(B),a):"function"==typeof a.write?v(c,i(S),a):s==Set?v(c,i(O),a):s==Object?v(c,i(g),a):A(c,i,a)},T=function(e,o,u){if("function"==typeof u){const f=u(e);return n(f)?f.then(r(M,e,o,t)):M(e,o,f)}return n(u)?u.then(r(M,e,o,t)):M(e,o,u)};return function(...e){return"function"==typeof e[0]?r(T,t,e[0],e[1]):n(e[0])?e[0].then(r(T,t,e[1],e[2])):T(e[0],e[1],e[2])}}());