evaluate-value
v3.0.0
Published
Return a value or an evaluated function (with arguments).
Readme
evaluate-value

Return a value or an evaluated function (with arguments).
- When the first input argument is a function, it is executed with the remaining arguments, and the result is returned.
- When the first input argument is not a function, it is simply returned.
Install
npm install evaluate-valueUsage
import evaluateValue from 'evaluate-value';
evaluateValue(true); //-> true
evaluateValue(() => true); //-> true
evaluateValue((arg1, arg2) => arg1 + arg2, 2, 2); //-> 4An EvaluateValueArgs type is exposed for convenient wrapping:
import { type EvaluateValueArgs } from 'evaluate-value';
interface Options {
prop: (arg: string) => boolean;
}
const DEFAULT_OPTIONS: Options = {
prop: arg => arg === 'something',
};
/**
* Fallback to the default value for a specific option if there's no custom value.
*/
const defaultFallback = <K extends keyof typeof DEFAULT_OPTIONS>(
options: Options | undefined,
name: K,
...args: EvaluateValueArgs<Options[K]>
) => evaluateValue(options?.[name], ...args) ?? evaluateValue(DEFAULT_OPTIONS[name], ...args);