UNPKG

iterates

Version:

Iterator and AsyncIterator helper functions with typings

73 lines (68 loc) 1.66 kB
/** * @internal */ const isIterableOrIterator = iterator => { return iterator != null && (typeof iterator[Symbol.iterator] === 'function' || typeof iterator[Symbol.asyncIterator] === 'function' || typeof iterator.next === 'function'); }; /** * @internal */ /** * @internal */ export const curry2 = func => (a, b) => { if (b === undefined) { return b => func(a, b); } else { return func(a, b); } }; /** * @internal */ export function autoCurry(fn) { const args = Array.from(arguments); if (fn.length <= 1) return fn; if (args.length - 1 >= fn.length) return fn.apply(this, args.slice(1)); return function () { return autoCurry.apply(this, args.concat(Array.from(arguments))); }; } /** * @internal */ /** * @internal */ export const curry2WithOptions = func => (a, b, options) => { if (options === undefined) { if (b === undefined) { return (b, options) => func(a, b, options); } else { if (isIterableOrIterator(b)) { return func(a, b); } else { options = b; return b => func(a, b, options); } } } else { return func(a, b, options); } }; export function pipeValue(value, ...functions) { return functions.reduce((value, func) => func(value), value); } /** * An identity function that is typed so that Typescript understands it passes through a tuple. * * ## Example * ```typescript * const isArray = ['hello', 42] // isArray has type Array<string|number> * const isTuple = tuple(['hello', 42]) // isTuple has type [string, number] * ``` */ export function tuple(items) { return items; } export const collectEntry = entry => entry;