spreadif
v1.3.0
Published
Assign properties to object with spread operator and this utility wrapper function.
Downloads
12
Readme
Usage
const spreadIf = require('spreadif');
const isCitizen = () => true;
const isResident = () => false;
const isEarthman = () => true;
const isAlien = () => false;
No more this:
const info = {};
if(isCitizen()) {
info.city = 'spacertown';
}
if(isResident()) {
info.resiNumber = 'X000X';
}
if(isAlien()) {
info.alienType = 'spacer';
}
if(isEarthman()) {
info.earthmanType = 'medievalist';
}
Better!
const info = {
...spreadIf(isCitizen(), {city: 'spacertown'}),
...spreadIf(isResident(), {resiNumber: 'X000X'}),
...spreadIf(isAlien(), {alienType: 'spacer'}),
...spreadIf(isEarthman(), {earthmanType: 'medievalists'})
}
console.log(info) // {city: 'spacertown', earthmanType: 'medievalists' }