Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to remove falsy values from objects #248

Open
raingerber opened this issue Mar 13, 2018 · 0 comments
Open

Add method to remove falsy values from objects #248

raingerber opened this issue Mar 13, 2018 · 0 comments
Labels

Comments

@raingerber
Copy link
Contributor

Problem description:

I've noticed a pattern of wanting to remove falsy values from objects, but there's no elegant way of doing that:

const reducer = [
  input => {
    // get the "a" and "b" properties, but only if they're truthy
    // this works, but it's awkward and boilerplate-ish
    const result = {}
    if (obj.a) result.a = a
    if (obj.b) result.b = b
    return obj
  }
]

Suggested solution:

I don't know, but these are a few ideas:

Proposal # 1

const reducer = {
  '?a': '$a', // only include this if "$a" is truthy
  b: '$b'
}

This works, but we removed the special treatment for keys beginning with '$', so this feels like going backward.

Proposal # 2

// not the best name...
const { removeFalsy } = DataPoint.helpers

removeFalsy({
  a: '$a',
  b: '$b'
})

This would remove every falsy value, but we might want to keep some of them.

Proposal # 3

// another not-so-great name...
const { truthy } = DataPoint.helpers

{
  a: truthy('$a'),
  b: '$b'
}

It could work? Feels weird though...

Summary

It would be nice if we don't add a new reducer helper, especially because it would only be useful for object reducers. Not sure if there's a good solution though!

@acatl acatl added the 📦data-point DataPoint Package label May 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants