rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 2.45 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.unionWith=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=Array.isArray,t=n=>null!=n&&"function"==typeof n.then,e=Promise.all.bind(Promise),o=Symbol.for("placeholder"),u=function(n,t,e,u){return t==o?((n,t,e)=>function(o){return n(o,t,e)})(n,e,u):e==o?((n,t,e)=>function(o){return n(t,o,e)})(n,t,u):((n,t,e)=>function(o){return n(t,e,o)})(n,t,e)},r=(n,t,e)=>function(){return n(t,e)},f=(n,t,e,o,u)=>function(){return n(t,e,o,u)},c=(n,t)=>function(...e){return t(n(...e))},i=(n,t,e)=>n[t](e),s=(n,t,e)=>n?t():e(),l=function(n,t){return n.push(t),n},h=n=>function(){return n},p=function(n,t,e){return t==o?((n,t)=>function(e){return n(e,t)})(n,e):((n,t)=>function(e){return n(t,e)})(n,t)},a=(n,t)=>t,y=n=>n,g=async function(n,o){const u=[];for await(const e of n){const n=o(e);t(n)&&u.push(n)}return 0==u.length?n:e(u).then(h(n))},b=Symbol.iterator,d=Symbol.asyncIterator,m=function(){},w=function(n,r,f){const c=n.length,s=[];let l=-1;for(;++l<c;){const e=f(r,n[l]);if(t(e))s.push(e);else if(e)return!0}return 0!=s.length&&e(s).then(u(i,o,"some",Boolean))},j=async function(n,e,o,u){const r=n.length;for(;++u<r;){const r=n[u],f=w(o,r,e);(t(f)?await f:f)||o.push(r)}return o};return i=>function(A){if(n(A))return function(n,e){const i=n.length,h=[];let p=-1;for(;++p<i;){const i=n[p],a=w(h,i,e);if(t(a))return a.then(c(u(s,o,m,r(l,h,i)),f(j,n,e,h,p)));a||h.push(i)}return h}(function(u){const r=u.length,f=[],i=[];let s=-1;for(;++s<r;){const e=u[s];if(n(e)){const n=e.length;let t=-1;for(;++t<n;)i.push(e[t])}else if(null==e)i.push(e);else if("function"==typeof e.then)f.push(e.then(p(l,i,o)));else if("function"==typeof e[b])for(const n of e)i.push(n);else if("function"==typeof e[d])f.push(g(e[d](),p(l,i,o)));else if("function"==typeof e.chain){const n=e.chain(y);t(n)?f.push(n.then(p(l,i,o))):i.push(n)}else if("function"==typeof e.flatMap){const n=e.flatMap(y);t(n)?f.push(n.then(p(l,i,o))):i.push(n)}else if("function"==typeof e.reduce){const n=e.reduce(c(a,p(l,i,o)),null);t(n)&&f.push(n)}else if(e.constructor==Object)for(const n in e)i.push(e[n]);else i.push(e)}return 0==f.length?i:e(f).then(h(i))}(A),i);throw new TypeError(`${A} is not an Array`)}}());