rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 3.54 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.groupBy=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=()=>new Map,t=n=>null!=n&&"function"==typeof n.then,e=Symbol.for("placeholder"),r=function(n,t,r,o){return t==e?((n,t,e)=>function(r){return n(r,t,e)})(n,r,o):r==e?((n,t,e)=>function(r){return n(t,r,e)})(n,t,o):((n,t,e)=>function(r){return n(t,e,r)})(n,t,r)},o=Array.isArray,u=(Object.values,Object.prototype.toString,async function(n,e,r){let o=n.next();if(o.done)return r;for(;!o.done;)r=e(r,o.value),t(r)&&(r=await r),o=n.next();return r}),f=Symbol.iterator,i=Symbol.asyncIterator,c=function(n,t,r){return t==e?((n,t)=>function(e){return n(e,t)})(n,r):((n,t)=>function(e){return n(t,e)})(n,t)},a=function(n,t,r,o,u){return t==e?((n,t,e,r)=>function(o){return n(o,t,e,r)})(n,r,o,u):r==e?((n,t,e,r)=>function(o){return n(t,o,e,r)})(n,t,o,u):o==e?((n,t,e,r)=>function(o){return n(t,e,o,r)})(n,t,r,u):((n,t,e,r)=>function(o){return n(t,e,r,o)})(n,t,r,o)},l=async function(n,e,r,o){const u=n.length;for(;++o<u;)r=e(r,n[o],o,n),t(r)&&(r=await r);return r},s=Object.keys,y=async function(n,e,r,o,u){const f=o.length;for(;++u<f;){const f=o[u];r=e(r,n[f],f,n),t(r)&&(r=await r)}return r},p=async function(n,e,r,o){for(const[u,f]of o)r=e(r,f,u,n),t(r)&&(r=await r);return r},d=function(n,d,h){return o(n)?function(n,r,o){const u=n.length;let f=-1;for(void 0===o&&(o=n[++f]);++f<u;)if(o=r(o,n[f],f,n),t(o))return o.then(a(l,n,r,e,f));return o}(n,d,h):null==n?void 0===h?c(d,n,e):d(h,n):n.constructor==Map?function(n,r,o){const u=n.entries();if(void 0===o){const n=u.next();if(n.done)return o;o=n.value[1]}for(const[f,i]of u)if(o=r(o,i,f,n),t(o))return o.then(a(p,n,r,e,u));return o}(n,d,h):"function"==typeof n[f]?function(n,o,f){let i=n.next();if(i.done)return f;for(void 0===f&&(f=i.value,i=n.next());!i.done;){if(f=o(f,i.value),t(f))return f.then(r(u,n,o,e));i=n.next()}return f}(n[f](),d,h):"function"==typeof n[i]?async function(n,t,e){let r=await n.next();if(r.done)return e;for(void 0===e&&(e=r.value,r=await n.next());!r.done;)e=await t(e,r.value),r=await n.next();return e}(n[i](),d,h):"function"==typeof n.reduce?n.reduce(d,h):"function"==typeof n.chain?n.chain(c(d,h,e)):"function"==typeof n.flatMap?n.flatMap(c(d,h,e)):n.constructor==Object?function(n,r,o){const u=s(n),f=u.length;let i=-1;for(void 0===o&&(o=n[u[++i]]);++i<f;){const f=u[i];if(o=r(o,n[f],f,n),t(o))return o.then((c=y,l=r,p=e,d=u,h=i,(a=n)==e?((n,t,e,r,o)=>function(u){return n(u,t,e,r,o)})(c,l,p,d,h):l==e?((n,t,e,r,o)=>function(u){return n(t,u,e,r,o)})(c,a,p,d,h):p==e?((n,t,e,r,o)=>function(u){return n(t,e,u,r,o)})(c,a,l,d,h):d==e?((n,t,e,r,o)=>function(u){return n(t,e,r,u,o)})(c,a,l,p,h):((n,t,e,r,o)=>function(u){return n(t,e,r,o,u)})(c,a,l,p,d)))}var c,a,l,p,d,h;return o}(n,d,h):void 0===h?c(d,n,e):d(h,n)},h=function(n,o,u){if("function"==typeof u){const f=u(n);return t(f)?f.then(r(d,n,o,e)):d(n,o,f)}return t(u)?u.then(r(d,n,o,e)):d(n,o,u)},v=function(...n){return"function"==typeof n[0]?r(h,e,n[0],n[1]):t(n[0])?n[0].then(r(h,e,n[1],n[2])):h(n[0],n[1],n[2])},b=function(n,t,e){const r=n.get(t);return null==r?n.set(t,[e]):r.push(e),n};return o=>{return v("function"==typeof o?(f=o,function(n,o){const u=f(o);return t(u)?u.then(r(b,n,e,o)):b(n,u,o)}):(u=o,function(n,t){return b(n,t[u],t)}),n);var u,f}}());