insert-if
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

insert-if

Conditionally add elements to array using spread operators

import iif from 'insert-if';
 
const arr = [
    'a',
    'b',
    // eager evaluation (only use for literal values never for computed deep properties)
    ...iif(isCForCat(), 'c'),
    // on-demand evaluation (safer for deep object properties as it prevents reference errors)
    ...iif(isThemeEnabled(), () => settings.theme.color),
    'd'
];
 
console.log(arr);
// > Array ['a', 'b', 'c', 'blue', 'd']

Package Sidebar

Install

npm i insert-if

Weekly Downloads

740

Version

1.2.0

License

MIT

Unpacked Size

5.94 kB

Total Files

8

Last publish

Collaborators

  • sweetslush