UNPKG

rubico

Version:

[a]synchronous functional programming

9 lines (7 loc) 3.4 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. */ !function(n,t){"object"==typeof module?module.exports=t:"function"==typeof define?define((()=>t)):n.filterOut=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=Symbol.for("placeholder"),t=function(t,r,e){return r==n?((n,t)=>function(r){return n(r,t)})(t,e):((n,t)=>function(r){return n(t,r)})(t,r)},r=Symbol.iterator,e=(n,t)=>({[r](){return this},next(){let r=n.next();for(;!r.done;){const{value:e}=r;if(t(e))return{value:e,done:!1};r=n.next()}return r}}),o=n=>null!=n&&"function"==typeof n.then,u=Symbol.asyncIterator,c=(n,t)=>({isAsyncIteratorDone:!1,[u](){return this},async next(){for(;!this.isAsyncIteratorDone;){const{value:r,done:e}=await n.next();if(e)this.isAsyncIteratorDone=!0;else{const n=t(r);if(o(n)?await n:n)return{value:r,done:!1}}}return{value:void 0,done:!0}}}),i=Array.isArray,f=function(t,r,e,o,u){return r==n?((n,t,r,e)=>function(o){return n(o,t,r,e)})(t,e,o,u):e==n?((n,t,r,e)=>function(o){return n(t,o,r,e)})(t,r,o,u):o==n?((n,t,r,e)=>function(o){return n(t,r,o,e)})(t,r,e,u):((n,t,r,e)=>function(o){return n(t,r,e,o)})(t,r,e,o)},s=Promise.all.bind(Promise),l=function(n,t,r,e){const o=t.length;let u=n.length-1;for(;++e<o;)n[++u]=r(t[e],e,n);return n},h=function(n,t,r,e){const o=n.length;let u=-1;for(;++r<o;)e[++u]&&t.push(n[r]);return t},a=function(t,r){const e=t.length,u=[];let c=-1,i=-1;for(;++c<e;){const e=t[c],a=r(e,c,t);if(o(a))return s(l([a],t,r,c)).then(f(h,t,u,c-1,n));a&&(u[++i]=e)}return u},p=function(t,r,e,o){return r==n?((n,t,r)=>function(e){return n(e,t,r)})(t,e,o):e==n?((n,t,r)=>function(e){return n(t,e,r)})(t,r,o):((n,t,r)=>function(e){return n(t,r,e)})(t,r,e)},y=(n,t,r)=>n[t](r),d=n=>function(){return n},g=(n,t,r)=>n?t():r(),b=(n,t)=>function(){return n(t)},v=function(){},m=(n,t,r,e,o)=>function(){return n(t,r,e,o)},x=(n,t,r,e)=>n[t](r,e),S=function(n,t,r,e){e&&(n[t]=r)},j=function(t,l){return i(t)?a(t,l):null==t?t:"string"==typeof t||t.constructor==String?function(t,r){const e=a(t,r);return o(e)?e.then(p(y,n,"join","")):e.join("")}(t,l):t.constructor==Set?function(t,r){const e=new Set,u=e.add.bind(e),c=[];for(const i of t){const f=r(i,i,t);o(f)?c.push(f.then(p(g,n,b(u,i),v))):f&&e.add(i)}return 0==c.length?e:s(c).then(d(e))}(t,l):t.constructor==Map?function(t,r){const e=new Map,u=[];for(const[c,i]of t){const f=r(i,c,t);o(f)?u.push(f.then(p(g,n,m(x,e,"set",c,i),v))):f&&e.set(c,i)}return 0==u.length?e:s(u).then(d(e))}(t,l):"function"==typeof t.filter?t.filter(l):"function"==typeof t[r]?e(t[r](),l):"function"==typeof t[u]?c(t[u](),l):t.constructor==Object?function(t,r){const e={},u=[];for(const c in t){const i=t[c],s=r(i,c,t);o(s)?u.push(s.then(f(S,e,c,t[c],n))):s&&(e[c]=i)}return 0==u.length?e:s(u).then(d(e))}(t,l):t},A=n=>!n,w=function(n,t){const r=t(...n);return o(r)?r.then(A):!r},I=function(...r){const e=r.pop();return"function"==typeof e?0==r.length?(u=w,f=e,(c=n)==n?((n,t)=>function(...r){return n(r,t)})(u,f):((n,t)=>function(...r){return n(t,r)})(u,c)):function(n){if(i(n)){const t=n.length;let r=-1;for(;++r<t;){const t=n[r];if(o(t))return!0}return!1}for(const t in n){const r=n[t];if(o(r))return!0}return!1}(r)?s(r).then(t(w,n,e)):w(r,e):!e;var u,c,f};return r=>{return"function"==typeof(e=I(r))?t(j,n,e):j(e,o);var e,o}}());