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']

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.0
    509
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.2.0
    509
  • 1.1.0
    5
  • 1.0.1
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i insert-if

Weekly Downloads

514

Version

1.2.0

License

MIT

Unpacked Size

5.94 kB

Total Files

8

Last publish

Collaborators

  • sweetslush