iif-ts
Version:
> 🧠A tiny utility for conditional insertion into arrays and objects using the spread operator. Supports lazy evaluation.
41 lines • 1.33 kB
TypeScript
/**
* Conditionally includes values in an array using the spread operator. Supports both eager and lazy
* evaluation.
*
* @example
* const items = [
* 'a',
* ...iifArray(true, 'b', 'c'), // ['a', 'b', 'c']
* ]
*
* @example
* const safe = [...iifArray(user?.enabled, () => getExpensiveValue())]
*
* @template T
* @param condition - If true, the values are included in the result.
* @param values - One or more values or lazy functions that return values.
* @returns An array of evaluated values or an empty array.
*/
export declare function iifArray<T>(condition: boolean, ...values: (T | (() => T))[]): T[];
/**
* Conditionally includes properties in an object using the spread operator. Supports both eager and
* lazy evaluation.
*
* @example
* const obj = {
* id: 1,
* ...iifObject(true, { role: 'admin' }), // { role: 'admin' }
* }
*
* @example
* const safe = {
* ...iifObject(config?.enabled, () => ({ debug: true })),
* }
*
* @template T
* @param condition - If true, the object is returned.
* @param value - An object or a lazy function that returns an object.
* @returns A shallow object or an empty object.
*/
export declare function iifObject<T extends object>(condition: boolean, value: T | (() => T)): Partial<T>;
//# sourceMappingURL=main.d.ts.map