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 missing nil support #62

Merged
merged 1 commit into from
Apr 5, 2024
Merged

Conversation

bkilshaw
Copy link
Contributor

@bkilshaw bkilshaw commented Apr 3, 2024

Added support so that the Human, Percentage, and SI modules accept nil as a value.

Copy link
Owner

@danielberkompas danielberkompas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! ⭐

@danielberkompas danielberkompas merged commit 21d7b99 into danielberkompas:master Apr 5, 2024
@zorn-allovue
Copy link

@danielberkompas It might be more helpful and explicit if the functions that are accepting of these nil argument values make sure nil is defined as a valid incoming argument value in the typesepcs as well.

Personal aside, I question if it is a good idea to accept nil for these functions. If the call sites do not have confidence about their variables I'm not sure the supporting libraries should have to be so accepting of nil. I'd rather the library be explicit about only offering conversion of known Number values. 🤷‍♂️

A kind of nitpicky take, and I only offer it as an additional perspective. Thanks for the work on this library.

@bkilshaw
Copy link
Contributor Author

bkilshaw commented Apr 8, 2024

@danielberkompas It might be more helpful and explicit if the functions that are accepting of these nil argument values make sure nil is defined as a valid incoming argument value in the typesepcs as well.

Personal aside, I question if it is a good idea to accept nil for these functions. If the call sites do not have confidence about their variables I'm not sure the supporting libraries should have to be so accepting of nil. I'd rather the library be explicit about only offering conversion of known Number values. 🤷‍♂️

A kind of nitpicky take, and I only offer it as an additional perspective. Thanks for the work on this library.

I honestly don't have a strong opinion either way; some of the other modules accept (and return) nil, so I was aligning the other modules so they all behaved the same. Removing the nil support in the other modules would be a breaking change though.

In our use case we're using Number directly in our HEEX templates, some fields nave a nil value, so something like this ended up being useful:

<table>
   <tr :for={quote <- quote_requests}>
      <td><%= quote.name %></td>
      <td><%= Number.Currency.number_to_currency(quote.price) %>
   </tr>
</table>

We needed the same for the Percentage module hence the PR. We can definitely check the value first, just sharing the use case for accepting nil.

@danielberkompas
Copy link
Owner

I chose to accept nil here because it matches the behavior of ActionView::NumberHelper. Since number advertises itself as having a very similar API, I think it makes sense to follow ActionView::NumberHelper, to reduce surprises that former Rails developers in particular will experience when using the library.

Thanks for the feedback both of you!

@zorn-allovue
Copy link

Thanks for the reply and the context. 👍

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.

3 participants