UNPKG

rubico

Version:

[a]synchronous functional programming

112 lines (94 loc) 2.73 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 (root, not) { if (typeof module == 'object') (module.exports = not) // CommonJS else if (typeof define == 'function') define(() => not) // AMD else (root.not = not) // Browser }(typeof globalThis == 'object' ? globalThis : this, (function () { 'use strict' const isArray = Array.isArray const isPromise = value => value != null && typeof value.then == 'function' const areAnyValuesPromises = function (values) { if (isArray(values)) { const length = values.length let index = -1 while (++index < length) { const value = values[index] if (isPromise(value)) { return true } } return false } for (const key in values) { const value = values[key] if (isPromise(value)) { return true } } return false } const promiseAll = Promise.all.bind(Promise) const __ = Symbol.for('placeholder') // argument resolver for curry2 const curry2ResolveArg0 = ( baseFunc, arg1, ) => function arg0Resolver(arg0) { return baseFunc(arg0, arg1) } // argument resolver for curry2 const curry2ResolveArg1 = ( baseFunc, arg0, ) => function arg1Resolver(arg1) { return baseFunc(arg0, arg1) } const curry2 = function (baseFunc, arg0, arg1) { return arg0 == __ ? curry2ResolveArg0(baseFunc, arg1) : curry2ResolveArg1(baseFunc, arg0) } // argument resolver for curryArgs2 const curryArgs2ResolveArgs0 = ( baseFunc, arg1, arg2, ) => function args0Resolver(...args) { return baseFunc(args, arg1) } // argument resolver for curryArgs2 const curryArgs2ResolveArgs1 = ( baseFunc, arg0, arg2, ) => function arg1Resolver(...args) { return baseFunc(arg0, args) } const curryArgs2 = function (baseFunc, arg0, arg1) { if (arg0 == __) { return curryArgs2ResolveArgs0(baseFunc, arg1) } return curryArgs2ResolveArgs1(baseFunc, arg0) } // negate(value boolean) -> inverse boolean const negate = value => !value // _not(args Array, predicate function) const _not = function (args, predicate) { const boolean = predicate(...args) return isPromise(boolean) ? boolean.then(negate) : !boolean } const not = function (...args) { const predicateOrValue = args.pop() if (typeof predicateOrValue == 'function') { if (args.length == 0) { return curryArgs2(_not, __, predicateOrValue) } if (areAnyValuesPromises(args)) { return promiseAll(args).then(curry2(_not, __, predicateOrValue)) } return _not(args, predicateOrValue) } return isPromise(predicateOrValue) ? predicateOrValue.then(negate) : !predicateOrValue } return not }())))