UNPKG

rubico

Version:

[a]synchronous functional programming

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