🧠 A tiny utility for conditional insertion into arrays and objects using the spread operator. Supports lazy evaluation.
- ✅ Drop-in use with
...spread
- ✅ Supports arrays (
iifArray
) and objects (iifObject
) - ✅ Lazy evaluation with functions
- ✅ Zero runtime dependencies
- ✅ Fully typed with TypeScript
pnpm add iif-ts
# or
npm install iif-ts
# or
yarn add iif-ts
import { iifArray } from 'iif-ts'
const arr = [
'a',
...iifArray(true, 'b', 'c') // ['b', 'c']
]
const lazy = [
...iifArray(condition, () => computeHeavyValue())
]
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 }))
}
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.
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 {}.
{...iifObject(isDev, { 'data-dev': true })}
{...iifArray(showTooltip, 'aria-label')}
This package is fully covered by TypeScript type checks. For full runtime tests, add your own test framework like Vitest or Jest.
MIT © Islam Yamor