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