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

Added default value field to toNumber function #5852

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kapilkumar9395
Copy link

@kapilkumar9395 kapilkumar9395 commented Apr 22, 2024

Modified toNumber method to have two parameters

  1. value
  2. defaultValue - when given value can't be converted into number it will return the default value

@Trott
Copy link

Trott commented Apr 22, 2024

Needs tests and docs.

Doesn't seem like a great API function signature. If I look at code and see _.toNumber(something), that's clear. If I see _.toNumber(something, somethingElse), I'm not sure what is going on. I'm not sure about the performance implications, but it is probably better to use an options object in this case: _.toNumber(something, {defaultValue: 0})

1) Added options as second param for toNumber
2) defaultValue will be returned in case if NaN occurs
3) Updated example and docs for toNumber
@kapilkumar9395
Copy link
Author

@Trott thanks for the input. I have made the necessary changes

@Trott
Copy link

Trott commented Apr 23, 2024

@Trott thanks for the input. I have made the necessary changes

I'm not a lodash maintainer or anything, so bear that in mind, but I would imagine that the tests should be added to test/number-coercion-methods.spec.js rather than creating a separate test file just for additional toNumber() tests. That also raises the question of whether symmetric changes should be made to the API of toFinite(), toInteger(), and toSafeInteger(). But those never return NaN so I'm not sure it makes sense.

That in turn raises the issue of whether this API adjustment makes sense. It seems like it would typically be used to return 0 instead of NaN, but that's already what toFinite() does, and toFinite() has the added bonus of returning usable values for Infinity and -Infinity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants