Skip to content

Commit 1727cf1

Browse files
Fix show unit_separator in #humanize_bytes with empty prefix (#15683)
1 parent a6e1e9b commit 1727cf1

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

spec/std/humanize_spec.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ describe Int do
301301
it { assert_prints 1099511627776.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0TB" }
302302
it { assert_prints 1125899906842624.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0PB" }
303303
it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0EB" }
304+
it { assert_prints 1.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: '\u2009'), "1\u2009B" }
304305
it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: '\u2009'), "1.0\u2009EB" }
305306

306307
it { assert_prints 1024.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0kiB" }

src/humanize.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,17 @@ struct Int
326326
#
327327
# See `Number#humanize` for more details on the behaviour and arguments.
328328
def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator = nil, format : BinaryPrefixFormat = :IEC) : Nil
329-
humanize(io, precision, separator, nil, base: 1024, significant: significant) do |magnitude|
329+
humanize(io, precision, separator, nil, base: 1024, significant: significant, unit_separator: unit_separator) do |magnitude|
330330
magnitude = Number.prefix_index(magnitude)
331331

332332
prefix = Number.si_prefix(magnitude)
333333
if prefix.nil?
334334
unit = "B"
335335
else
336336
if format.iec?
337-
unit = "#{unit_separator}#{prefix}iB"
337+
unit = "#{prefix}iB"
338338
else
339-
unit = "#{unit_separator}#{prefix.upcase}B"
339+
unit = "#{prefix.upcase}B"
340340
end
341341
end
342342
{magnitude, unit, magnitude > 0}

0 commit comments

Comments
 (0)