-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 unit_separator
to #humanize
and #humanize_bytes
#15176
base: master
Are you sure you want to change the base?
Conversation
separate_unit
for humanize
and humanize_bytes
separate_unit
for humanize
and humanize_bytes
Maybe a configurable parameter could be better than a boolean flag? |
Does it make sense to give the user full control over the separator, or should we define an enum that limits options? |
Also, the default IMO should use a regular space for compatibility. |
What would be the reason for limiting? I don't see why any arbitrary restriction would be useful. If someone want's a particular string as separator, let them have it. |
Just pushed the change to allow for custom unit separators. Thought about a Char but that felt overly restrictive as well. 👍 |
separate_unit
for humanize
and humanize_bytes
unit_separator
to #humanize
and #humanize_bytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a tiny nitpick: specs could use a Char
whenever possible to demonstrate the encouraged use, use a String when there are more than one char, and just leave one check that we can pass a 1 char string (or nil, number or whatever).
Good call. I cleaned up the tests and left the ones that I think are meaningful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your patience while we ironed out this feature 🙇
Happy to! Honored to contribute. 🔮✨ |
Should this parameter be added to the method documentation? |
@@ -152,17 +152,17 @@ struct Number | |||
# delimiter (see `#format`). | |||
# | |||
# See `Int#humanize_bytes` to format a file size. | |||
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes : Indexable = SI_PREFIXES) : Nil | |||
humanize(io, precision, separator, delimiter, base: base, significant: significant) do |magnitude, _| | |||
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, prefixes : Indexable = SI_PREFIXES) : Nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, documentation is needed.
We also have an opportunity to mention:
"Consider using '\u00A0'
(non -breaking space) as the value for unit_separator
, it can prevent the output from being split across lines".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure - I'm not immediately seeing where I can add to the docs, can someone point me in the right direction? 😅
Adds a
separate_unit
parameter to thehumanize
andhumanize_bytes
methods. If selected adds a non-breaking space ('\u00A0'
) between the value and the unit.Also includes relevant specs.
Fixes #14349