UNPKG

rubico

Version:

[a]synchronous functional programming

9 lines (7 loc) 2.58 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.forEach=t}("object"==typeof globalThis?globalThis:this,function(){"use strict";const n=n=>null!=n&&"function"==typeof n.then,t=Symbol.for("placeholder"),o=function(n,o,r){return o==t?((n,t)=>function(o){return n(o,t)})(n,r):((n,t)=>function(o){return n(t,o)})(n,o)},r=Array.isArray,e=(Object.prototype.toString,Promise.all.bind(Promise)),c=n=>function(){return n},u=Symbol.iterator,f=Symbol.asyncIterator,i=(n,t,o,r)=>function(){return n(t,o,r)},s=async function(t,o,r){const e=t.length;for(;++r<e;){const e=o(t[r]);n(e)&&await e}return t},l=async function(t,o,r){for(const e in t){if(e in r)continue;const c=o(t[e]);n(c)&&await c}return t},a=(n,t,o)=>function(){return n(t,o)},h=async function(t,o){let r=t.next();for(;!r.done;){const e=o(r.value);n(e)&&await e,r=t.next()}return t},y=function(t,o){return r(t)?function(t,o){const r=t.length,u=[];let f=-1;for(;++f<r;){const r=o(t[f]);n(r)&&u.push(r)}return 0==u.length?t:e(u).then(c(t))}(t,o):null==t?t:"function"==typeof t.forEach?(t.forEach(o),t):"function"==typeof t[u]?function(t,o){const r=[];for(const e of t){const t=o(e);n(t)&&r.push(t)}return 0==r.length?t:e(r).then(c(t))}(t[u](),o):"function"==typeof t[f]?async function(t,o){const r=[];for await(const e of t){const t=o(e);n(t)&&r.push(t)}return 0==r.length?t:e(r).then(c(t))}(t[f](),o):t.constructor==Object?function(t,o){const r=[];for(const e in t){const c=o(t[e]);n(c)&&r.push(c)}return 0==r.length?t:e(r).then(c(t))}(t,o):t},p=function(r,e){return"function"==typeof r?o(y,t,r):n(r)?r.then(o(y,t,e)):y(r,e)},b=function(t,o){if(r(t))return function(t,o){const r=t.length;let e=-1;for(;++e<r;){const r=o(t[e]);if(n(r))return r.then(i(s,t,o,e))}return t}(t,o);if(null==t)throw new TypeError(`invalid collection ${t}`);if("function"==typeof t[u])return function(t,o){let r=t.next();for(;!t.done;){const e=o(r.value);if(n(e))return e.then(a(h,t,o));r=t.next()}return t}(t[u](),o);if("function"==typeof t[f])return async function(t,o){for await(const r of t){const t=o(r);n(t)&&await t}return t}(t[f](),o);if(t.constructor==Object)return function(t,o){const r={};for(const e in t){r[e]=!0;const c=o(t[e]);if(n(c))return c.then(i(l,t,o,r))}return t}(t,o);throw new TypeError(`invalid collection ${t}`)};return p.series=function(r,e){return"function"==typeof r?o(b,t,r):n(r)?r.then(o(b,t,e)):b(r,e)},p}());