Omit Empty

omitEmpty removes empty properties from an object.


Build objects without conditional spreads

Objects can be simpler even when some properties are conditional.

No need to use conditional spreads or add them in later.

main.js
// Before
const author = {
name: 'Zell Liew',
...(bio && { bio }),
}
main.js
// After
const author = omitEmpty({
name: 'Zell Liew',
bio,
})

Cleans up nested values too

omitEmpty is recursive by default, so it removes empty values inside nested objects and arrays.

main.js
omitEmpty({
a: {
one: {},
two: [],
three: 1,
},
})
// {
// a: {
// three: 1,
// },
// }