import { isArray } from './is' import { Func } from './types' /** * Identity function, always return input itself * * @export * @template T * @param {T} self * @returns {T} */ export function identity(self: T): T { return self } /** * A function that does nothing * * @export */ export function noop() { /* noop */ } /** * Given an initmial argument and a list of functions, each function will be executed sequentially, and * the returned value is passed to the next function as argument. * * @deprecated {{in favor of compose}}{{}} * @export * @template T * @param {(any[] | any)} [args] * @param {...Func[]} funcs * @returns {T} */ export function flow(args?: any[] | any, ...funcs: Func[]): T { if (funcs.length === 0) { return args } if (!isArray(args)) { args = [args] } return funcs.reduce((result, nextFunc) => { return result === args ? nextFunc(...result) : nextFunc(result) }, args) } export function compose(...funcs: Func[]): (...args: any[]) => T { if (funcs.length === 0) { return identity } return (...args: any[]) => funcs.reduce((result, nextFunc) => { return result === args ? nextFunc(...result) : nextFunc(result) }, args) } /** * Simple try catch wrapper that returns a default value if exception was raised. * If silent = false, there will be a 'console.error' call. * * @export * @param {Func} func * @param {*} [defaultValue] * @param {boolean} [silent=true] * @returns {*} */ export function attempt(func: Func, defaultValue?: T, silent: boolean = true): T | undefined { try { return func() } catch (e) { !silent && console.error(e) return defaultValue } }