1.2.0 • Published 4 years ago

insert-if v1.2.0

Weekly downloads
66
License
MIT
Repository
github
Last release
4 years ago

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