Skip to content

Commit

Permalink
drop type requirement on unit_separator
Browse files Browse the repository at this point in the history
  • Loading branch information
CTC97 committed Nov 12, 2024
1 parent 80c50b9 commit 4b41c29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions spec/std/humanize_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,13 @@ describe Number do
it { assert_prints 1.0e+9.humanize(unit_separator: "\t"), "1.0\tG" }
it { assert_prints 1.0e+35.humanize(unit_separator: "-"), "100,000-Q" }
it { assert_prints 0.000_001.humanize(unit_separator: "\u2009"), "1.0\u2009µ" }
it { assert_prints 0.000_001.humanize(unit_separator: '\u2009'), "1.0\u2009µ" }
it { assert_prints 0.000_000_001.humanize(unit_separator: "."), "1.0.n" }
it { assert_prints 1_000_000_000_000.humanize(unit_separator: "__"), "1.0__T" }
it { assert_prints 123_456_789_012.humanize(unit_separator: ","), "123,G" }
it { assert_prints 123_456_789_012.humanize(unit_separator: '-'), "123-G" }
it { assert_prints 123_456_789_012.humanize(unit_separator: 0), "1230G" }
it { assert_prints 123_456_789_012.humanize(unit_separator: nil), "123G" }

it { assert_prints Float32::INFINITY.humanize, "Infinity" }
it { assert_prints (-Float32::INFINITY).humanize, "-Infinity" }
Expand Down
18 changes: 9 additions & 9 deletions src/humanize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ 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, unit_separator : String = "", prefixes : Indexable = SI_PREFIXES) : Nil
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, prefixes : Indexable = SI_PREFIXES) : Nil
humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator) do |magnitude, _|
magnitude = Number.prefix_index(magnitude, prefixes: prefixes)
{magnitude, Number.si_prefix(magnitude, prefixes)}
end
end

# :ditto:
def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", prefixes = SI_PREFIXES) : String
def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, prefixes = SI_PREFIXES) : String
String.build do |io|
humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator, prefixes: prefixes)
end
Expand Down Expand Up @@ -215,7 +215,7 @@ struct Number
# ```
#
# See `Int#humanize_bytes` to format a file size.
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil
if zero? || (responds_to?(:infinite?) && self.infinite?) || (responds_to?(:nan?) && self.nan?)
digits = 0
else
Expand Down Expand Up @@ -259,12 +259,12 @@ struct Number

number.format(io, separator, delimiter, decimal_places: decimal_places, only_significant: significant)

io << unit_separator if unit
io << unit_separator.to_s if unit
io << unit
end

# :ditto:
def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", &) : String
def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, &) : String
String.build do |io|
humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator) do |magnitude, number|
yield magnitude, number
Expand All @@ -273,14 +273,14 @@ struct Number
end

# :ditto:
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", prefixes : Proc) : Nil
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, prefixes : Proc) : Nil
humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator) do |magnitude, number|
prefixes.call(magnitude, number)
end
end

# :ditto:
def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", prefixes : Proc) : String
def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator = nil, prefixes : Proc) : String
String.build do |io|
humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator, prefixes: prefixes)
end
Expand Down Expand Up @@ -322,7 +322,7 @@ struct Int
# ```
#
# See `Number#humanize` for more details on the behaviour and arguments.
def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator : String = "", format : BinaryPrefixFormat = :IEC) : Nil
def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator = nil, format : BinaryPrefixFormat = :IEC) : Nil
humanize(io, precision, separator, nil, base: 1024, significant: significant) do |magnitude|
magnitude = Number.prefix_index(magnitude)

Expand All @@ -341,7 +341,7 @@ struct Int
end

# :ditto:
def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator : String = "", format : BinaryPrefixFormat = :IEC) : String
def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator = nil, format : BinaryPrefixFormat = :IEC) : String
String.build do |io|
humanize_bytes(io, precision, separator, significant: significant, unit_separator: unit_separator, format: format)
end
Expand Down

0 comments on commit 4b41c29

Please sign in to comment.