rubico
Version:
[a]synchronous functional programming
9 lines (7 loc) • 1.91 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.trace=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=n=>function(){return n},r=(n,t,r)=>n?t():r(),e=Symbol.for("placeholder"),o=function(n,t,r,o){return t==e?((n,t,r)=>function(e){return n(e,t,r)})(n,r,o):r==e?((n,t,r)=>function(e){return n(t,e,r)})(n,t,o):((n,t,r)=>function(e){return n(t,r,e)})(n,t,r)},u=Array.isArray,c=function(t){if(u(t)){const r=t.length;let e=-1;for(;++e<r;){const r=t[e];if(n(r))return!0}return!1}for(const r in t){const e=t[r];if(n(e))return!0}return!1},i=Promise.all.bind(Promise),f=function(r,e){const o=r[0],u=e(...r);return n(u)?u.then(t(o)):o},s=function(...n){const t=n.pop();return 0==n.length?(r=f,u=t,(o=e)==e?((n,t)=>function(...r){return n(r,t)})(r,u):((n,t)=>function(...r){return n(t,r)})(r,o)):c(n)?i(n).then(function(n,t,r){return t==e?((n,t)=>function(r){return n(r,t)})(n,r):((n,t)=>function(r){return n(t,r)})(n,t)}(f,e,t)):f(n,t);var r,o,u},l=function(u,c,i){const f=u(...i);if(n(f))return f.then(o(r,e,((n,t)=>function(){return n(...t)})(s(c),i),t(i[0])));if(f){const r=c(...i);if(n(r))return r.then(t(i[0]))}return i[0]};s.if=function(...n){if(2==n.length)return t=l,r=n[0],u=n[1],f=e,r==e?((n,t,r)=>function(...e){return n(e,t,r)})(t,u,f):u==e?((n,t,r)=>function(...e){return n(t,e,r)})(t,r,f):((n,t,r)=>function(...e){return n(t,r,e)})(t,r,u);var t,r,u,f;const s=n.length,h=n[s-1],a=n[s-2],p=n.slice(0,-2);return c(p)?i(p).then(o(l,a,h,e)):l(a,h,n)};const h=console.log;return function(...t){const r=t[0];return"function"==typeof r?s((e=r,o=h,function(...t){const r=e(...t);return n(r)?r.then(o):o(r)})):s(h)(...t);var e,o}}());