import type {Bud} from '@roots/bud-framework' import isFunction from '@roots/bud-support/isFunction' type Parameters = Array export interface Callable { (...value: Parameters): I } export type MaybeCallable = | ((...params: P) => T) | Exclude interface maybeCall { ( value: ((bud: T) => T) | Exclude, ): T ( value: ((...params: P) => T) | Exclude, ...params: P ): T } /** * Calls a given value if it is a function. The function will be bound to * Budbefore it is called. * * If it is not a function, returns the value without doing anything to it. * * @typeParam I - Type of the value expected to be returned */ const maybeCall: maybeCall = function < T extends unknown, P extends Parameters, >(value: T, ...params: P) { if (!params.length) params.push(this) return isFunction(value) ? typeof value.bind !== `undefined` ? value.bind(this)(...params) : value(...params) : value } export {maybeCall}