UNPKG

iif-ts

Version:

> ๐Ÿง  A tiny utility for conditional insertion into arrays and objects using the spread operator. Supports lazy evaluation.

91 lines (62 loc) โ€ข 1.72 kB
# iif-ts > ๐Ÿง  A tiny utility for conditional insertion into arrays and objects using the spread operator. Supports lazy evaluation. --- ## โœจ Features - โœ… Drop-in use with `...spread` - โœ… Supports arrays (`iifArray`) and objects (`iifObject`) - โœ… Lazy evaluation with functions - โœ… Zero runtime dependencies - โœ… Fully typed with TypeScript --- ## ๐Ÿ’พ Installation ```bash pnpm add iif-ts # or npm install iif-ts # or yarn add iif-ts ``` ## ๐Ÿ”ง Usage ### โžค Arrays: iifArray ```ts import { iifArray } from 'iif-ts' const arr = [ 'a', ...iifArray(true, 'b', 'c'), // ['b', 'c'] ] const lazy = [...iifArray(condition, () => computeHeavyValue())] ``` ### โžค Objects: iifObject ```ts import { iifObject } from 'iif-ts' const obj = { name: 'Islam', ...iifObject(true, { role: 'admin' }), // adds "role" if true } const safe = { ...iifObject(user?.active, () => ({ token: user.token })), } ``` ## ๐Ÿ“š API ```ts iifArray<T>(condition: boolean, ...values: (T | (() => T))[]): T[] ``` - condition โ€“ If true, the values are included. - values โ€“ One or more values or lazy functions returning values. - โœ… Returns an array or empty array. ```ts iifObject<T>(condition: boolean, value: T | (() => T)): Partial<T> ``` - condition โ€“ If true, the object is returned. - value โ€“ An object or a lazy function returning one. - โœ… Returns an object or {}. ## ๐Ÿ›  Example with JSX (React) ```ts {...iifObject(isDev, { 'data-dev': true })} {...iifArray(showTooltip, 'aria-label')} ``` ## ๐Ÿงช Testing This package is fully covered by TypeScript type checks. For full runtime tests, add your own test framework like Vitest or Jest. ## ๐Ÿ“„ License Apache-2.0 ยฉ Islam Yamor ---