Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 6x 20x 20x 10x 10x | import { Provider, isFunction } from "@benneq/function";
export type ValueOrProvider<T, TArgs extends unknown[] = []> =
| T
| Provider<T, TArgs>;
/**
* Resolves a value that was provided via {@link ValueOrProvider}
*
* Can be called with optional `args` which will be used as arguments when calling the Provider Function.
*
* @example
* Create a Function that accepts a `value` that can be either lazy and eager inizialized
* ```ts
* function fn(value: ValueOrProvider<string>) {
* // ... some code that may return early from the Function ...
*
* // resolve the value when it is actually needed
* const actualValue = valueOrProviderResult(value);
* console.log(typeof actualValue); // 'string'
* }
* ```
*
* Call the Function with an eager initialized value
* ```ts
* fn(1);
* ```
*
* Call the Function with a lazy initialized value
* ```ts
* function someExpensiveOperation() { /* ... *\/ }
*
* fn(someExpensiveOperation);
* ```
*
* @example
* Create a Function that accepts a `value` that can be either static or dynamic
* ```ts
* function add(numbers: number[], value: ValueOrProvider<number, [number]>) {
* return numbers.map(n => n + valueOrProviderResult(value, e));
* }
* ```
*
* Call the Function with a static value
* ```ts
* const a = add([1,2,3], 1);
* console.log(a); // [2,3,4]
* ```
*
* Call the Function with a dynamic value
* ```ts
* const a = add([1,2,3], n => 2+n);
* console.log(a); // [4,6,8]
* ```
*
* @returns the result of the Function if `value` is a Function, otherwise returns `value` itself
*/
export const valueOrProviderResult = <T, TArgs extends unknown[] = []>(
value: ValueOrProvider<T, TArgs>,
...args: TArgs
): T => {
if (isFunction(value)) {
return value(...args);
} else {
return value;
}
};
|