Skip to content

adds option to ignore undefined values #14

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

Conversation

achingbrain
Copy link
Contributor

It's common to take options arguments and pass some of them on to other functions.

If the incoming option is not set, this can result in passing undefined on to other functions which can cause surprise if the other function sets up its own default values.

function someOtherFunc (options = {}) {
  options.key = options.key || 'default-value'

  console.info(options.key)
}

function someFunc (options = {}) {
  // do some work
  // ...

  return someOtherFunc(mergeOptions({
    key: 'overridden-default-value'
  }, {
    key: options.key
  }))
}

someFunc()
// prints 'default-value'

This PR adds a ignoreUndefined option to not set values to undefined:

function someFunc (options = {}) {
  // do some work
  // ...

  return someOtherFunc(mergeOptions.call({ ignoreUndefined: true }, {
    key: 'overridden-default-value'
  }, {
    key: options.key
  }))
}

someFunc()
// prints 'overridden-default-value'

Fixes #13

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling dc00af7 on achingbrain:add-ignore-undefined-config-option into 37dc96c on schnittstabil:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling dc00af7 on achingbrain:add-ignore-undefined-config-option into 37dc96c on schnittstabil:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling dc00af7 on achingbrain:add-ignore-undefined-config-option into 37dc96c on schnittstabil:master.

@coveralls
Copy link

coveralls commented Oct 19, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling e1e89f8 on achingbrain:add-ignore-undefined-config-option into 37dc96c on schnittstabil:master.

@achingbrain
Copy link
Contributor Author

achingbrain commented Oct 19, 2019

I would consider defaulting this option to true as it's by far the more common use case (for me) when passing options around (e.g. respect null, ignore undefined).

I'd argue for it to be false if this was a general-purpose object-merging module but given the name I don't think it is.

@schnittstabil
Copy link
Owner

Even though I understand your reasoning for defaulting to true, it is more important for me to be backward-compatible and compatible to the more recent (non-recursive) spread operator.

@achingbrain
Copy link
Contributor Author

I think the spread operator is trying to be closer to general-purpose object-merging than merging options specifically - there are plenty of modules out there already that do that recursively.

As for backwards compatibility, you can just release a major.

Up to you though.

@schnittstabil schnittstabil merged commit 2464655 into schnittstabil:master Oct 19, 2019
@schnittstabil
Copy link
Owner

I have to publish a major anyway, because of node>=8.

Regarding your example above, I would argue that the common use case looks like this, which already works at v1.0.0 as expected:

const mergeOptions = require('merge-options');

function someFunc (options = {}) {
  return someOtherFunc(mergeOptions({
    key: 'overridden-default-value'
  }, options))
}

Please note that you still may bind custom options, which may mitigate your concerns:

const mergeOptions = require('merge-options').bind({ignoreUndefined: true});

mergeOptions({foo: 'bar'}, {foo: undefined})
//=> {foo: 'bar'}

@schnittstabil
Copy link
Owner

[email protected] is out now.

@achingbrain achingbrain deleted the add-ignore-undefined-config-option branch November 22, 2019 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

undefined values are merged
3 participants