UNPKG

@voiceflow/common

Version:

Junk drawer of utility functions

62 lines (61 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withEffect = exports.chainVoidAsync = exports.chainAsync = exports.chainVoid = exports.chain = exports.stringify = exports.identity = exports.noop = exports.compose = exports.isFunction = void 0; const isFunction = (value) => typeof value === 'function'; exports.isFunction = isFunction; const compose = (...transforms) => (value) => { if (transforms.length === 1) { return transforms[0](value); } if (transforms.length === 2) { return transforms[0](transforms[1](value)); } return transforms.reduceRight((acc, transform) => transform(acc), value); }; exports.compose = compose; const noop = () => undefined; exports.noop = noop; const identity = (value) => value; exports.identity = identity; const stringify = (value) => (typeof value === 'string' ? value : String(value)); exports.stringify = stringify; const chain = (...fns) => (...args) => { // perf optimization, most of the time we have one or two functions if (fns.length === 1) { fns[0]?.(...args); } else if (fns.length === 2) { fns[0]?.(...args); fns[1]?.(...args); } else { fns.forEach((fn) => fn?.(...args)); } }; exports.chain = chain; const chainVoid = (...fns) => () => (0, exports.chain)(...fns)(); exports.chainVoid = chainVoid; const chainAsync = (...fns) => async (...args) => { // perf optimization, most of the time we have one or two functions if (fns.length === 1) { await fns[0]?.(...args); } else if (fns.length === 2) { await fns[0]?.(...args); await fns[1]?.(...args); } else { for (const fn of fns) { // eslint-disable-next-line no-await-in-loop await fn?.(...args); } } }; exports.chainAsync = chainAsync; const chainVoidAsync = (...fns) => () => (0, exports.chainAsync)(...fns)(); exports.chainVoidAsync = chainVoidAsync; const withEffect = (callback) => (value) => { callback(value); return value; }; exports.withEffect = withEffect;