From c077a1b3f1f1b1b8de5720b684203f595748e44a Mon Sep 17 00:00:00 2001 From: CTC97 Date: Sat, 9 Nov 2024 15:20:23 -0500 Subject: [PATCH 1/6] adds separate_unit param to humanize and humanize_bytes + specs --- spec/std/humanize_spec.cr | 15 +++++++++++++++ src/humanize.cr | 34 ++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/spec/std/humanize_spec.cr b/spec/std/humanize_spec.cr index d24d2017cb28..f36af5d843c7 100644 --- a/spec/std/humanize_spec.cr +++ b/spec/std/humanize_spec.cr @@ -207,6 +207,13 @@ describe Number do it { assert_prints 1.0e+34.humanize, "10,000Q" } it { assert_prints 1.0e+35.humanize, "100,000Q" } + it { assert_prints 0.humanize(separate_unit: true), "0.0" } + it { assert_prints 12_345.humanize(separate_unit: true), "12.3\u00A0k" } + it { assert_prints 0.123_456_78.humanize(5, separate_unit: true), "123.46\u00A0m" } + it { assert_prints 1.0e-14.humanize(separate_unit: true), "10.0\u00A0f" } + it { assert_prints 1.0e+9.humanize(separate_unit: true), "1.0\u00A0G" } + it { assert_prints 1.0e+35.humanize(separate_unit: true), "100,000\u00A0Q" } + it { assert_prints Float32::INFINITY.humanize, "Infinity" } it { assert_prints (-Float32::INFINITY).humanize, "-Infinity" } it { assert_prints Float32::NAN.humanize, "NaN" } @@ -261,6 +268,7 @@ describe Number do it { assert_prints 1.0e+8.humanize(prefixes: CUSTOM_PREFIXES), "100d" } it { assert_prints 1.0e+9.humanize(prefixes: CUSTOM_PREFIXES), "1,000d" } it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES), "10,000d" } + it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES, separate_unit: true), "10,000\u00A0d" } end end end @@ -281,6 +289,7 @@ describe Int do it { assert_prints 1025.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0KB" } it { assert_prints 1026.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.01KB" } it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "2.0KB" } + it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "2.0\u00A0KB" } it { assert_prints 1536.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.5KB" } it { assert_prints 524288.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "512KB" } @@ -289,8 +298,14 @@ describe Int do it { assert_prints 1099511627776.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0TB" } it { assert_prints 1125899906842624.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0PB" } it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0EB" } + it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "1.0\u00A0EB" } it { assert_prints 1024.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0kiB" } it { assert_prints 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0GiB" } + + # it { assert_prints 0.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "0 B" } + # it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "2.0 KB" } + # it { assert_prints 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC, separate_unit: true), "1.0 GiB" } + # it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "1.0 EB" } end end diff --git a/src/humanize.cr b/src/humanize.cr index db9d84c64889..108d4ab27809 100644 --- a/src/humanize.cr +++ b/src/humanize.cr @@ -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, separate_unit = false, prefixes : Indexable = SI_PREFIXES) : Nil + humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit) 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, prefixes = SI_PREFIXES) : String + def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, prefixes = SI_PREFIXES) : String String.build do |io| - humanize(io, precision, separator, delimiter, base: base, significant: significant, prefixes: prefixes) + humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit, prefixes: prefixes) end end @@ -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, &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil + def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil if zero? || (responds_to?(:infinite?) && self.infinite?) || (responds_to?(:nan?) && self.nan?) digits = 0 else @@ -259,29 +259,30 @@ struct Number number.format(io, separator, delimiter, decimal_places: decimal_places, only_significant: significant) + io << '\u00A0' if unit && separate_unit io << unit end # :ditto: - def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, &) : String + def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, &) : String String.build do |io| - humanize(io, precision, separator, delimiter, base: base, significant: significant) do |magnitude, number| + humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit) do |magnitude, number| yield magnitude, number end end end # :ditto: - def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes : Proc) : Nil - humanize(io, precision, separator, delimiter, base: base, significant: significant) do |magnitude, number| + def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, prefixes : Proc) : Nil + humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit) do |magnitude, number| prefixes.call(magnitude, number) end end # :ditto: - def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, prefixes : Proc) : String + def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, prefixes : Proc) : String String.build do |io| - humanize(io, precision, separator, delimiter, base: base, significant: significant, prefixes: prefixes) + humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit, prefixes: prefixes) end end end @@ -321,18 +322,19 @@ 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, format : BinaryPrefixFormat = :IEC) : Nil + def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, separate_unit : Bool = false, format : BinaryPrefixFormat = :IEC) : Nil humanize(io, precision, separator, nil, base: 1024, significant: significant) do |magnitude| magnitude = Number.prefix_index(magnitude) + spacing = separate_unit ? '\u00A0' : "" prefix = Number.si_prefix(magnitude) if prefix.nil? unit = "B" else if format.iec? - unit = "#{prefix}iB" + unit = "#{spacing}#{prefix}iB" else - unit = "#{prefix.upcase}B" + unit = "#{spacing}#{prefix.upcase}B" end end {magnitude, unit, magnitude > 0} @@ -340,9 +342,9 @@ struct Int end # :ditto: - def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, format : BinaryPrefixFormat = :IEC) : String + def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, separate_unit : Bool = false, format : BinaryPrefixFormat = :IEC) : String String.build do |io| - humanize_bytes(io, precision, separator, significant: significant, format: format) + humanize_bytes(io, precision, separator, significant: significant, separate_unit: separate_unit, format: format) end end end From eeb30a05718bc416206fdbb53b3f88d53ce73b20 Mon Sep 17 00:00:00 2001 From: CTC97 Date: Sat, 9 Nov 2024 15:22:10 -0500 Subject: [PATCH 2/6] cleanup :broom: --- spec/std/humanize_spec.cr | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec/std/humanize_spec.cr b/spec/std/humanize_spec.cr index f36af5d843c7..80e31a5fd86e 100644 --- a/spec/std/humanize_spec.cr +++ b/spec/std/humanize_spec.cr @@ -302,10 +302,5 @@ describe Int do it { assert_prints 1024.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0kiB" } it { assert_prints 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0GiB" } - - # it { assert_prints 0.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "0 B" } - # it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "2.0 KB" } - # it { assert_prints 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC, separate_unit: true), "1.0 GiB" } - # it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "1.0 EB" } end end From 80c50b9546e35aad573bc89b4e1b955c5f04c225 Mon Sep 17 00:00:00 2001 From: CTC97 Date: Sun, 10 Nov 2024 15:46:54 -0500 Subject: [PATCH 3/6] rework to unit_separator : String --- spec/std/humanize_spec.cr | 21 ++++++++++++--------- src/humanize.cr | 35 +++++++++++++++++------------------ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/spec/std/humanize_spec.cr b/spec/std/humanize_spec.cr index 80e31a5fd86e..167cadfdcdc5 100644 --- a/spec/std/humanize_spec.cr +++ b/spec/std/humanize_spec.cr @@ -207,12 +207,15 @@ describe Number do it { assert_prints 1.0e+34.humanize, "10,000Q" } it { assert_prints 1.0e+35.humanize, "100,000Q" } - it { assert_prints 0.humanize(separate_unit: true), "0.0" } - it { assert_prints 12_345.humanize(separate_unit: true), "12.3\u00A0k" } - it { assert_prints 0.123_456_78.humanize(5, separate_unit: true), "123.46\u00A0m" } - it { assert_prints 1.0e-14.humanize(separate_unit: true), "10.0\u00A0f" } - it { assert_prints 1.0e+9.humanize(separate_unit: true), "1.0\u00A0G" } - it { assert_prints 1.0e+35.humanize(separate_unit: true), "100,000\u00A0Q" } + it { assert_prints 0.humanize(unit_separator: "_"), "0.0" } + it { assert_prints 0.123_456_78.humanize(5, unit_separator: "\u00A0"), "123.46\u00A0m" } + it { assert_prints 1.0e-14.humanize(unit_separator: " "), "10.0 f" } + 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_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 Float32::INFINITY.humanize, "Infinity" } it { assert_prints (-Float32::INFINITY).humanize, "-Infinity" } @@ -268,7 +271,7 @@ describe Number do it { assert_prints 1.0e+8.humanize(prefixes: CUSTOM_PREFIXES), "100d" } it { assert_prints 1.0e+9.humanize(prefixes: CUSTOM_PREFIXES), "1,000d" } it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES), "10,000d" } - it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES, separate_unit: true), "10,000\u00A0d" } + it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES, unit_separator: "\u00A0"), "10,000\u00A0d" } end end end @@ -289,7 +292,7 @@ describe Int do it { assert_prints 1025.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0KB" } it { assert_prints 1026.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.01KB" } it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "2.0KB" } - it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "2.0\u00A0KB" } + it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: "\u202F"), "2.0\u202FKB" } it { assert_prints 1536.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.5KB" } it { assert_prints 524288.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "512KB" } @@ -298,7 +301,7 @@ describe Int do it { assert_prints 1099511627776.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0TB" } it { assert_prints 1125899906842624.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0PB" } it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0EB" } - it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, separate_unit: true), "1.0\u00A0EB" } + it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: "\u2009"), "1.0\u2009EB" } it { assert_prints 1024.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0kiB" } it { assert_prints 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0GiB" } diff --git a/src/humanize.cr b/src/humanize.cr index 108d4ab27809..e5e0406ee806 100644 --- a/src/humanize.cr +++ b/src/humanize.cr @@ -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, separate_unit = false, prefixes : Indexable = SI_PREFIXES) : Nil - humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit) do |magnitude, _| + def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", 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, separate_unit = false, prefixes = SI_PREFIXES) : String + def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", prefixes = SI_PREFIXES) : String String.build do |io| - humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit, prefixes: prefixes) + humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator, prefixes: prefixes) end end @@ -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, separate_unit = false, &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil + def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil if zero? || (responds_to?(:infinite?) && self.infinite?) || (responds_to?(:nan?) && self.nan?) digits = 0 else @@ -259,30 +259,30 @@ struct Number number.format(io, separator, delimiter, decimal_places: decimal_places, only_significant: significant) - io << '\u00A0' if unit && separate_unit + io << unit_separator if unit io << unit end # :ditto: - def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, &) : String + def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", &) : String String.build do |io| - humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit) do |magnitude, number| + humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator) do |magnitude, number| yield magnitude, number end end end # :ditto: - def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, separate_unit = false, prefixes : Proc) : Nil - humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit) do |magnitude, number| + def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", 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, separate_unit = false, prefixes : Proc) : String + def humanize(precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, unit_separator : String = "", prefixes : Proc) : String String.build do |io| - humanize(io, precision, separator, delimiter, base: base, significant: significant, separate_unit: separate_unit, prefixes: prefixes) + humanize(io, precision, separator, delimiter, base: base, significant: significant, unit_separator: unit_separator, prefixes: prefixes) end end end @@ -322,19 +322,18 @@ 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, separate_unit : Bool = false, format : BinaryPrefixFormat = :IEC) : Nil + def humanize_bytes(io : IO, precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator : String = "", format : BinaryPrefixFormat = :IEC) : Nil humanize(io, precision, separator, nil, base: 1024, significant: significant) do |magnitude| magnitude = Number.prefix_index(magnitude) - spacing = separate_unit ? '\u00A0' : "" prefix = Number.si_prefix(magnitude) if prefix.nil? unit = "B" else if format.iec? - unit = "#{spacing}#{prefix}iB" + unit = "#{unit_separator}#{prefix}iB" else - unit = "#{spacing}#{prefix.upcase}B" + unit = "#{unit_separator}#{prefix.upcase}B" end end {magnitude, unit, magnitude > 0} @@ -342,9 +341,9 @@ struct Int end # :ditto: - def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, separate_unit : Bool = false, format : BinaryPrefixFormat = :IEC) : String + def humanize_bytes(precision : Int = 3, separator = '.', *, significant : Bool = true, unit_separator : String = "", format : BinaryPrefixFormat = :IEC) : String String.build do |io| - humanize_bytes(io, precision, separator, significant: significant, separate_unit: separate_unit, format: format) + humanize_bytes(io, precision, separator, significant: significant, unit_separator: unit_separator, format: format) end end end From 4b41c299d7db0efa4dda0615c0d3f5efabe19844 Mon Sep 17 00:00:00 2001 From: CTC97 Date: Mon, 11 Nov 2024 21:49:23 -0500 Subject: [PATCH 4/6] drop type requirement on unit_separator --- spec/std/humanize_spec.cr | 4 ++++ src/humanize.cr | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/spec/std/humanize_spec.cr b/spec/std/humanize_spec.cr index 167cadfdcdc5..68c8c7847b48 100644 --- a/spec/std/humanize_spec.cr +++ b/spec/std/humanize_spec.cr @@ -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" } diff --git a/src/humanize.cr b/src/humanize.cr index e5e0406ee806..18aa2744382f 100644 --- a/src/humanize.cr +++ b/src/humanize.cr @@ -152,7 +152,7 @@ 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)} @@ -160,7 +160,7 @@ struct Number 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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 From ef19e526bb05c9b265bfaf7437b8de910369fd38 Mon Sep 17 00:00:00 2001 From: CTC97 Date: Thu, 14 Nov 2024 21:16:07 -0500 Subject: [PATCH 5/6] test cleanup :broom: --- spec/std/humanize_spec.cr | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/spec/std/humanize_spec.cr b/spec/std/humanize_spec.cr index 68c8c7847b48..e4230540804d 100644 --- a/spec/std/humanize_spec.cr +++ b/spec/std/humanize_spec.cr @@ -207,17 +207,13 @@ describe Number do it { assert_prints 1.0e+34.humanize, "10,000Q" } it { assert_prints 1.0e+35.humanize, "100,000Q" } - it { assert_prints 0.humanize(unit_separator: "_"), "0.0" } - it { assert_prints 0.123_456_78.humanize(5, unit_separator: "\u00A0"), "123.46\u00A0m" } - it { assert_prints 1.0e-14.humanize(unit_separator: " "), "10.0 f" } - 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.humanize(unit_separator: '_'), "0.0" } + it { assert_prints 0.123_456_78.humanize(5, unit_separator: '\u00A0'), "123.46\u00A0m" } + it { assert_prints 1.0e-14.humanize(unit_separator: ' '), "10.0 f" } 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 0.000_000_001.humanize(unit_separator: "."), "1.0.n" } + it { assert_prints 1.0e+9.humanize(unit_separator: "\t"), "1.0\tG" } it { assert_prints 123_456_789_012.humanize(unit_separator: 0), "1230G" } it { assert_prints 123_456_789_012.humanize(unit_separator: nil), "123G" } @@ -275,7 +271,7 @@ describe Number do it { assert_prints 1.0e+8.humanize(prefixes: CUSTOM_PREFIXES), "100d" } it { assert_prints 1.0e+9.humanize(prefixes: CUSTOM_PREFIXES), "1,000d" } it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES), "10,000d" } - it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES, unit_separator: "\u00A0"), "10,000\u00A0d" } + it { assert_prints 1.0e+10.humanize(prefixes: CUSTOM_PREFIXES, unit_separator: '\u00A0'), "10,000\u00A0d" } end end end @@ -296,7 +292,7 @@ describe Int do it { assert_prints 1025.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0KB" } it { assert_prints 1026.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.01KB" } it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "2.0KB" } - it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: "\u202F"), "2.0\u202FKB" } + it { assert_prints 2048.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: '\u202F'), "2.0\u202FKB" } it { assert_prints 1536.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.5KB" } it { assert_prints 524288.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "512KB" } @@ -305,7 +301,7 @@ describe Int do it { assert_prints 1099511627776.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0TB" } it { assert_prints 1125899906842624.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0PB" } it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC), "1.0EB" } - it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: "\u2009"), "1.0\u2009EB" } + it { assert_prints 1152921504606846976.humanize_bytes(format: Int::BinaryPrefixFormat::JEDEC, unit_separator: '\u2009'), "1.0\u2009EB" } it { assert_prints 1024.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0kiB" } it { assert_prints 1073741824.humanize_bytes(format: Int::BinaryPrefixFormat::IEC), "1.0GiB" } From 87edf74d7f5bedf5de38eb2a3d45f681e3b89a3a Mon Sep 17 00:00:00 2001 From: CTC97 Date: Thu, 21 Nov 2024 21:26:52 -0500 Subject: [PATCH 6/6] nit clean :broom: --- src/humanize.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize.cr b/src/humanize.cr index 18aa2744382f..07e2a68fe14d 100644 --- a/src/humanize.cr +++ b/src/humanize.cr @@ -259,7 +259,7 @@ struct Number number.format(io, separator, delimiter, decimal_places: decimal_places, only_significant: significant) - io << unit_separator.to_s if unit + io << unit_separator if unit io << unit end