humanize 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +56 -6
- data/humanize.gemspec +1 -1
- data/lib/humanize/core_ext.rb +20 -0
- data/lib/humanize/locales/constants/fr_ch.rb +10 -0
- data/lib/humanize/locales/constants/zh_tw.rb +10 -0
- data/lib/humanize/locales/fr_ch.rb +43 -0
- data/lib/humanize/locales/zh_tw.rb +30 -0
- data/lib/humanize/locales.rb +2 -2
- data/lib/humanize/module.rb +122 -0
- data/lib/humanize.rb +3 -128
- data/spec/core_ext_spec.rb +46 -0
- data/spec/humanize_spec.rb +30 -42
- data/spec/locales/fr_ch_spec.rb +48 -0
- data/spec/locales/zh_tw_spec.rb +55 -0
- metadata +11 -2
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require_relative 'locales'
|
3
|
+
|
4
|
+
module Humanize
|
5
|
+
# Big numbers are big: http://wiki.answers.com/Q/What_number_is_after_vigintillion&src=ansTT
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def format(number,
|
9
|
+
locale: Humanize.config.default_locale,
|
10
|
+
decimals_as: Humanize.config.decimals_as)
|
11
|
+
locale_class, spacer = Humanize.for_locale(locale)
|
12
|
+
|
13
|
+
return locale_class::SUB_ONE_GROUPING[0] if number.zero?
|
14
|
+
|
15
|
+
infinity = number.to_f.infinite?
|
16
|
+
if infinity
|
17
|
+
infinity_word = locale_class::INFINITY
|
18
|
+
return infinity == 1 ? infinity_word : "#{locale_class::NEGATIVE}#{spacer}#{infinity_word}"
|
19
|
+
elsif number.is_a?(Float) && number.nan?
|
20
|
+
return locale_class::UNDEFINED
|
21
|
+
end
|
22
|
+
|
23
|
+
sign = locale_class::NEGATIVE if number.negative?
|
24
|
+
|
25
|
+
parts = locale_class.new.humanize(number.abs)
|
26
|
+
process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
|
27
|
+
Humanize.stringify(parts, sign, spacer)
|
28
|
+
end
|
29
|
+
|
30
|
+
def for_locale(locale)
|
31
|
+
case locale.to_sym
|
32
|
+
# NOTE: add locales here in alphabetical order
|
33
|
+
when :az, :de, :en, :es, :fr, :id, :ms, :pt, :ru, :vi
|
34
|
+
[Object.const_get("Humanize::#{locale.capitalize}"), ' ']
|
35
|
+
when :th
|
36
|
+
[Humanize::Th, '']
|
37
|
+
when :tr
|
38
|
+
[Humanize::Tr, ' ']
|
39
|
+
when :jp
|
40
|
+
[Humanize::Jp, '']
|
41
|
+
when :'zh-tw'
|
42
|
+
[Humanize::ZhTw, '']
|
43
|
+
when :'fr-CH'
|
44
|
+
[Humanize::FrCh, ' ']
|
45
|
+
else
|
46
|
+
raise "Unsupported humanize locale: #{locale}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def stringify(parts, sign, spacer)
|
51
|
+
output = parts.reverse.join(spacer).squeeze(spacer)
|
52
|
+
if locale_is?(:es) && sign
|
53
|
+
"#{output}#{spacer}#{sign}"
|
54
|
+
elsif sign
|
55
|
+
"#{sign}#{spacer}#{output}"
|
56
|
+
else
|
57
|
+
output
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def locale_is?(locale)
|
62
|
+
Humanize.config.default_locale == locale
|
63
|
+
end
|
64
|
+
|
65
|
+
# rubocop:disable Metrics/ParameterLists
|
66
|
+
def process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
|
67
|
+
# rubocop:enable Metrics/ParameterLists
|
68
|
+
return unless number.is_a?(Float) || number.is_a?(BigDecimal)
|
69
|
+
|
70
|
+
# Why 15?
|
71
|
+
# (byebug) BigDecimal.new(number, 15)
|
72
|
+
# 0.8000015e1
|
73
|
+
# (byebug) BigDecimal.new(number, 16)
|
74
|
+
# 0.8000014999999999e1
|
75
|
+
decimal = BigDecimal(number, 15) - BigDecimal(number.to_i)
|
76
|
+
|
77
|
+
_sign, significant_digits, _base, exponent = decimal.split
|
78
|
+
return if significant_digits == "0"
|
79
|
+
|
80
|
+
grouping = locale_class::SUB_ONE_GROUPING
|
81
|
+
leading_zeroes = [grouping[0]] * exponent.abs
|
82
|
+
decimals_as = :digits if leading_zeroes.any?
|
83
|
+
|
84
|
+
decimals_as_words =
|
85
|
+
case decimals_as
|
86
|
+
when :digits
|
87
|
+
digits = significant_digits.chars.map do |num|
|
88
|
+
grouping[num.to_i]
|
89
|
+
end
|
90
|
+
|
91
|
+
(leading_zeroes + digits).join(spacer)
|
92
|
+
when :number
|
93
|
+
Humanize.format(significant_digits.to_i, locale:)
|
94
|
+
end
|
95
|
+
|
96
|
+
parts.insert(0, decimals_as_words, locale_class::POINT)
|
97
|
+
end
|
98
|
+
|
99
|
+
attr_writer :config
|
100
|
+
|
101
|
+
def config
|
102
|
+
@config ||= Configuration.new
|
103
|
+
end
|
104
|
+
|
105
|
+
def reset_config
|
106
|
+
@config = Configuration.new
|
107
|
+
end
|
108
|
+
|
109
|
+
def configure
|
110
|
+
yield(config)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Configuration
|
115
|
+
attr_accessor :default_locale, :decimals_as
|
116
|
+
|
117
|
+
def initialize
|
118
|
+
@default_locale = :en
|
119
|
+
@decimals_as = :digits
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/lib/humanize.rb
CHANGED
@@ -1,129 +1,4 @@
|
|
1
|
-
|
2
|
-
require_relative 'humanize/locales'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
module
|
5
|
-
|
6
|
-
EMPTY = ''.freeze
|
7
|
-
# Big numbers are big: http://wiki.answers.com/Q/What_number_is_after_vigintillion&src=ansTT
|
8
|
-
|
9
|
-
def humanize(locale: Humanize.config.default_locale,
|
10
|
-
decimals_as: Humanize.config.decimals_as)
|
11
|
-
locale_class, spacer = Humanize.for_locale(locale)
|
12
|
-
|
13
|
-
return locale_class::SUB_ONE_GROUPING[0] if zero?
|
14
|
-
|
15
|
-
infinity = to_f.infinite?
|
16
|
-
if infinity
|
17
|
-
infinity_word = locale_class::INFINITY
|
18
|
-
return infinity == 1 ? infinity_word : "#{locale_class::NEGATIVE}#{spacer}#{infinity_word}"
|
19
|
-
elsif is_a?(Float) && nan?
|
20
|
-
return locale_class::UNDEFINED
|
21
|
-
end
|
22
|
-
|
23
|
-
sign = locale_class::NEGATIVE if negative?
|
24
|
-
|
25
|
-
parts = locale_class.new.humanize(abs)
|
26
|
-
process_decimals(locale_class, locale, parts, decimals_as, spacer)
|
27
|
-
Humanize.stringify(parts, sign, spacer)
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.for_locale(locale)
|
31
|
-
case locale.to_sym
|
32
|
-
# NOTE: add locales here in ealphabetical order
|
33
|
-
when :az, :de, :en, :es, :fr, :id, :ms, :pt, :ru, :vi
|
34
|
-
[Object.const_get("Humanize::#{locale.capitalize}"), SPACE]
|
35
|
-
when :th
|
36
|
-
[Humanize::Th, EMPTY]
|
37
|
-
when :tr
|
38
|
-
[Humanize::Tr, SPACE]
|
39
|
-
when :jp
|
40
|
-
[Humanize::Jp, EMPTY]
|
41
|
-
else
|
42
|
-
raise "Unsupported humanize locale: #{locale}"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.stringify(parts, sign, spacer)
|
47
|
-
output = parts.reverse.join(spacer).squeeze(spacer)
|
48
|
-
if locale_is?(:es) && sign
|
49
|
-
"#{output}#{spacer}#{sign}"
|
50
|
-
elsif sign
|
51
|
-
"#{sign}#{spacer}#{output}"
|
52
|
-
else
|
53
|
-
output
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.locale_is?(locale)
|
58
|
-
Humanize.config.default_locale == locale
|
59
|
-
end
|
60
|
-
|
61
|
-
def process_decimals(locale_class, locale, parts, decimals_as, spacer)
|
62
|
-
return unless is_a?(Float) || is_a?(BigDecimal)
|
63
|
-
|
64
|
-
# Why 15?
|
65
|
-
# (byebug) BigDecimal.new(number, 15)
|
66
|
-
# 0.8000015e1
|
67
|
-
# (byebug) BigDecimal.new(number, 16)
|
68
|
-
# 0.8000014999999999e1
|
69
|
-
decimal = BigDecimal(self, 15) - BigDecimal(to_i)
|
70
|
-
|
71
|
-
_sign, significant_digits, _base, exponent = decimal.split
|
72
|
-
return if significant_digits == "0"
|
73
|
-
|
74
|
-
grouping = locale_class::SUB_ONE_GROUPING
|
75
|
-
leading_zeroes = [grouping[0]] * exponent.abs
|
76
|
-
decimals_as = :digits if leading_zeroes.any?
|
77
|
-
|
78
|
-
decimals_as_words =
|
79
|
-
case decimals_as
|
80
|
-
when :digits
|
81
|
-
digits = significant_digits.chars.map do |num|
|
82
|
-
grouping[num.to_i]
|
83
|
-
end
|
84
|
-
|
85
|
-
(leading_zeroes + digits).join(spacer)
|
86
|
-
when :number
|
87
|
-
significant_digits.to_i.humanize(locale:)
|
88
|
-
end
|
89
|
-
|
90
|
-
parts.insert(0, decimals_as_words, locale_class::POINT)
|
91
|
-
end
|
92
|
-
|
93
|
-
class << self
|
94
|
-
attr_writer :config
|
95
|
-
end
|
96
|
-
|
97
|
-
def self.config
|
98
|
-
@config ||= Configuration.new
|
99
|
-
end
|
100
|
-
|
101
|
-
def self.reset_config
|
102
|
-
@config = Configuration.new
|
103
|
-
end
|
104
|
-
|
105
|
-
def self.configure
|
106
|
-
yield(config)
|
107
|
-
end
|
108
|
-
|
109
|
-
class Configuration
|
110
|
-
attr_accessor :default_locale, :decimals_as
|
111
|
-
|
112
|
-
def initialize
|
113
|
-
@default_locale = :en
|
114
|
-
@decimals_as = :digits
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
class Integer
|
120
|
-
include Humanize
|
121
|
-
end
|
122
|
-
|
123
|
-
class Float
|
124
|
-
include Humanize
|
125
|
-
end
|
126
|
-
|
127
|
-
class BigDecimal
|
128
|
-
include Humanize
|
129
|
-
end
|
3
|
+
require "humanize/module" # require just this if you don't need the core_ext extensions to Integer, Float, BigDecimal
|
4
|
+
require "humanize/core_ext"
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe "Humanize core extensions to Ruby" do
|
4
|
+
after do
|
5
|
+
Humanize.reset_config
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'both options work together' do
|
9
|
+
it 'work together' do
|
10
|
+
expect(
|
11
|
+
0.42.humanize(locale: :fr, decimals_as: :number)
|
12
|
+
).to eql('zéro virgule quarante-deux')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'when called on instances of Rational, Complex, and Date::Infinity' do
|
17
|
+
it 'will raise NoMethodError' do
|
18
|
+
expect { Rational(1, 3).humanize }.to raise_error(NoMethodError, /humanize/)
|
19
|
+
expect { Complex(1 + 2i).humanize }.to raise_error(NoMethodError, /humanize/)
|
20
|
+
if defined? Date::Infinity
|
21
|
+
expect do
|
22
|
+
Date::Infinity.new.humanize
|
23
|
+
end.to raise_error(NoMethodError, /humanize/)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'when called on conceptual number' do
|
29
|
+
it 'reads correctly' do
|
30
|
+
inf = Float::INFINITY
|
31
|
+
neg_inf = -inf
|
32
|
+
nan = inf + neg_inf
|
33
|
+
|
34
|
+
expect(inf.humanize).to eql('infinity')
|
35
|
+
expect(neg_inf.humanize).to eql('negative infinity')
|
36
|
+
expect(nan.humanize).to eql('undefined')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'when called on bigdecimal with decimal fractions' do
|
41
|
+
it 'reads the decimal digits' do
|
42
|
+
expect(BigDecimal('123').humanize).to eql('one hundred and twenty-three')
|
43
|
+
expect(BigDecimal('123.45').humanize).to eql('one hundred and twenty-three point four five')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/humanize_spec.rb
CHANGED
@@ -8,25 +8,25 @@ RSpec.describe "Humanize" do
|
|
8
8
|
describe 'locale option' do
|
9
9
|
it 'uses default locale' do
|
10
10
|
Humanize.config.default_locale = :fr
|
11
|
-
expect(42
|
11
|
+
expect(Humanize.format(42)).to eql('quarante-deux')
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'uses locale passed as argument if given' do
|
15
15
|
Humanize.config.default_locale = :en
|
16
|
-
expect(
|
16
|
+
expect(Humanize.format(42, locale: :fr)).to eql('quarante-deux')
|
17
17
|
end
|
18
18
|
|
19
19
|
describe 'turkish specific rules' do
|
20
20
|
it 'one thousand and two equals "bin iki"' do
|
21
|
-
expect(
|
21
|
+
expect(Humanize.format(1002, locale: :tr)).to eql('bin iki')
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'two thousand and one equals "iki bin bir' do
|
25
|
-
expect(
|
25
|
+
expect(Humanize.format(2001, locale: :tr)).to eql('iki bin bir')
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'ten thousand equals "on bin"' do
|
29
|
-
expect(
|
29
|
+
expect(Humanize.format(10_000, locale: :tr)).to eql('on bin')
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -37,30 +37,30 @@ RSpec.describe "Humanize" do
|
|
37
37
|
|
38
38
|
context 'one thousand' do
|
39
39
|
it 'equals "satu ribu" when it is not the only thousand in its thousands range' do
|
40
|
-
expect(1_101_000
|
41
|
-
expect(2_201_042
|
40
|
+
expect(Humanize.format(1_101_000)).to eql('satu juta seratus satu ribu')
|
41
|
+
expect(Humanize.format(2_201_042)).to eql('dua juta dua ratus satu ribu empat puluh dua')
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'equals "seribu" when it is the lone thousand in its thousands range' do
|
45
|
-
expect(1_000
|
46
|
-
expect(1_042
|
47
|
-
expect(1_001_042
|
48
|
-
expect(1_000_001_042
|
45
|
+
expect(Humanize.format(1_000)).to eql('seribu')
|
46
|
+
expect(Humanize.format(1_042)).to eql('seribu empat puluh dua')
|
47
|
+
expect(Humanize.format(1_001_042)).to eql('satu juta seribu empat puluh dua')
|
48
|
+
expect(Humanize.format(1_000_001_042)).to eql('satu bilion seribu empat puluh dua')
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
describe 'azerbaijani specific rules' do
|
54
54
|
it 'one thousand and two equals "min iki"' do
|
55
|
-
expect(
|
55
|
+
expect(Humanize.format(1002, locale: :az)).to eql('min iki')
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'two thousand and one equals "iki min bir' do
|
59
|
-
expect(
|
59
|
+
expect(Humanize.format(2001, locale: :az)).to eql('iki min bir')
|
60
60
|
end
|
61
61
|
|
62
62
|
it 'ten thousand equals "on min"' do
|
63
|
-
expect(
|
63
|
+
expect(Humanize.format(10_000, locale: :az)).to eql('on min')
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -71,15 +71,15 @@ RSpec.describe "Humanize" do
|
|
71
71
|
|
72
72
|
context 'one thousand' do
|
73
73
|
it 'equals "satu ribu" when it is not the only thousand in its thousands range' do
|
74
|
-
expect(1_101_000
|
75
|
-
expect(2_201_042
|
74
|
+
expect(Humanize.format(1_101_000)).to eql('satu juta seratus satu ribu')
|
75
|
+
expect(Humanize.format(2_201_042)).to eql('dua juta dua ratus satu ribu empat puluh dua')
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'equals "seribu" when it is the lone thousand in its thousands range' do
|
79
|
-
expect(1_000
|
80
|
-
expect(1_042
|
81
|
-
expect(1_001_042
|
82
|
-
expect(1_000_001_042
|
79
|
+
expect(Humanize.format(1_000)).to eql('seribu')
|
80
|
+
expect(Humanize.format(1_042)).to eql('seribu empat puluh dua')
|
81
|
+
expect(Humanize.format(1_001_042)).to eql('satu juta seribu empat puluh dua')
|
82
|
+
expect(Humanize.format(1_000_001_042)).to eql('satu miliar seribu empat puluh dua')
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
@@ -88,12 +88,12 @@ RSpec.describe "Humanize" do
|
|
88
88
|
describe 'decimals_as option' do
|
89
89
|
it 'uses value from configuration' do
|
90
90
|
Humanize.config.decimals_as = :number
|
91
|
-
expect(0.42
|
91
|
+
expect(Humanize.format(0.42)).to eql('zero point forty-two')
|
92
92
|
end
|
93
93
|
|
94
94
|
it 'uses value passed as argument if given' do
|
95
95
|
Humanize.config.decimals_as = :number
|
96
|
-
expect(0.42
|
96
|
+
expect(Humanize.format(0.42, decimals_as: :digits)).to eql('zero point four two')
|
97
97
|
end
|
98
98
|
|
99
99
|
describe 'when set as number' do
|
@@ -102,8 +102,8 @@ RSpec.describe "Humanize" do
|
|
102
102
|
end
|
103
103
|
|
104
104
|
it 'reads the decimals as digits if led by zero(s)' do
|
105
|
-
expect(0.042
|
106
|
-
expect(0.0042
|
105
|
+
expect(Humanize.format(0.042)).to eql('zero point zero four two')
|
106
|
+
expect(Humanize.format(0.0042)).to eql('zero point zero zero four two')
|
107
107
|
end
|
108
108
|
end
|
109
109
|
end
|
@@ -111,39 +111,27 @@ RSpec.describe "Humanize" do
|
|
111
111
|
describe 'both options work together' do
|
112
112
|
it 'work together' do
|
113
113
|
expect(
|
114
|
-
0.42
|
114
|
+
Humanize.format(0.42, locale: :fr, decimals_as: :number)
|
115
115
|
).to eql('zéro virgule quarante-deux')
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
describe 'when called on instances of Rational, Complex, and Date::Infinity' do
|
120
|
-
it 'will raise NoMethodError' do
|
121
|
-
expect { Rational(1, 3).humanize }.to raise_error(NoMethodError, /humanize/)
|
122
|
-
expect { Complex(1 + 2i).humanize }.to raise_error(NoMethodError, /humanize/)
|
123
|
-
if defined? Date::Infinity
|
124
|
-
expect do
|
125
|
-
Date::Infinity.new.humanize
|
126
|
-
end.to raise_error(NoMethodError, /humanize/)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
119
|
describe 'when called on conceptual number' do
|
132
120
|
it 'reads correctly' do
|
133
121
|
inf = Float::INFINITY
|
134
122
|
neg_inf = -inf
|
135
123
|
nan = inf + neg_inf
|
136
124
|
|
137
|
-
expect(inf
|
138
|
-
expect(neg_inf
|
139
|
-
expect(nan
|
125
|
+
expect(Humanize.format(inf)).to eql('infinity')
|
126
|
+
expect(Humanize.format(neg_inf)).to eql('negative infinity')
|
127
|
+
expect(Humanize.format(nan)).to eql('undefined')
|
140
128
|
end
|
141
129
|
end
|
142
130
|
|
143
131
|
describe 'when called on bigdecimal with decimal fractions' do
|
144
132
|
it 'reads the decimal digits' do
|
145
|
-
expect(BigDecimal('123')
|
146
|
-
expect(BigDecimal('123.45')
|
133
|
+
expect(Humanize.format(BigDecimal('123'))).to eql('one hundred and twenty-three')
|
134
|
+
expect(Humanize.format(BigDecimal('123.45'))).to eql('one hundred and twenty-three point four five')
|
147
135
|
end
|
148
136
|
end
|
149
137
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Humanize, "fr CH locale" do
|
4
|
+
before do
|
5
|
+
Humanize.configure do |config|
|
6
|
+
config.default_locale = :'fr-CH'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
tests = [
|
11
|
+
[71, 'septante-et-un'],
|
12
|
+
[80.15, 'huitante virgule un cinq'],
|
13
|
+
[1090, 'mille nonante'],
|
14
|
+
[2095, 'deux mille nonante-cinq'],
|
15
|
+
[10_000, 'dix mille'],
|
16
|
+
[1_000_000, "un million"],
|
17
|
+
[2_000_000, "deux millions"],
|
18
|
+
[5_000_000, "cinq millions"],
|
19
|
+
[1_000_000_000, "un milliard"],
|
20
|
+
[2_000_000_000, "deux milliards"],
|
21
|
+
[5_000_000_000, "cinq milliards"]
|
22
|
+
]
|
23
|
+
|
24
|
+
tests.each do |num, output|
|
25
|
+
it "#{num} equals #{output}" do
|
26
|
+
expect(num.humanize).to eql(output)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'decimals: number' do
|
31
|
+
it 'returns the decimals as whole numbers' do
|
32
|
+
num = 8.15
|
33
|
+
expect(num.humanize(decimals_as: :number)).to eq('huit virgule quinze')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'when called on conceptual number' do
|
38
|
+
it 'reads correctly' do
|
39
|
+
inf = Float::INFINITY
|
40
|
+
neg_inf = -inf
|
41
|
+
nan = inf + neg_inf
|
42
|
+
|
43
|
+
expect(inf.humanize).to eql('infini')
|
44
|
+
expect(neg_inf.humanize).to eql('négatif infini')
|
45
|
+
expect(nan.humanize).to eql('indéfini')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Humanize, 'zh tw locale' do
|
4
|
+
before do
|
5
|
+
Humanize.configure do |config|
|
6
|
+
config.default_locale = :'zh-tw'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
tests = [
|
11
|
+
[1, '壹'],
|
12
|
+
[48, '肆拾捌'],
|
13
|
+
[100, '佰'],
|
14
|
+
[101, '佰壹'],
|
15
|
+
[110, '佰拾'],
|
16
|
+
[199, '佰玖拾玖'],
|
17
|
+
[999, '玖佰玖拾玖'],
|
18
|
+
[1000, '壹仟'],
|
19
|
+
[3456, '参仟肆佰伍拾陸'],
|
20
|
+
[9999, '玖仟玖佰玖拾玖'],
|
21
|
+
[21_111, '貳萬壹仟佰拾壹'],
|
22
|
+
[99_999, '玖萬玖仟玖佰玖拾玖'],
|
23
|
+
[9_876_543, '玖佰捌拾柒萬陸仟伍佰肆拾参'],
|
24
|
+
[10_000_000, '壹仟萬'],
|
25
|
+
[100_000_000, '壹億'],
|
26
|
+
[765_432_109, '柒億陸仟伍佰肆拾参萬貳仟佰玖'],
|
27
|
+
[-123_422_223.48_948_753, '負壹億貳仟参佰肆拾貳萬貳仟貳佰貳拾参・肆捌玖肆捌捌']
|
28
|
+
]
|
29
|
+
|
30
|
+
tests.each do |num, output|
|
31
|
+
it "#{num} equals #{output}" do
|
32
|
+
expect(num.humanize).to eql(output)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'decimals: number' do
|
37
|
+
it 'returns the decimals as whole numbers' do
|
38
|
+
num = 8.1592
|
39
|
+
expect(num.humanize).to eq('捌・壹伍玖貳')
|
40
|
+
expect(num.humanize(decimals_as: :number)).to eq('捌・壹仟伍佰玖拾貳')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'when called on conceptual number' do
|
45
|
+
it 'reads correctly' do
|
46
|
+
inf = Float::INFINITY
|
47
|
+
neg_inf = -inf
|
48
|
+
nan = inf + neg_inf
|
49
|
+
|
50
|
+
expect(inf.humanize).to eql('無限大')
|
51
|
+
expect(neg_inf.humanize).to eql('負無限大')
|
52
|
+
expect(nan.humanize).to eql('未定義')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humanize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Chen
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-
|
12
|
+
date: 2024-05-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mutant
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- bin/run_mutant
|
100
100
|
- humanize.gemspec
|
101
101
|
- lib/humanize.rb
|
102
|
+
- lib/humanize/core_ext.rb
|
102
103
|
- lib/humanize/locales.rb
|
103
104
|
- lib/humanize/locales/az.rb
|
104
105
|
- lib/humanize/locales/constants/az.rb
|
@@ -106,6 +107,7 @@ files:
|
|
106
107
|
- lib/humanize/locales/constants/en.rb
|
107
108
|
- lib/humanize/locales/constants/es.rb
|
108
109
|
- lib/humanize/locales/constants/fr.rb
|
110
|
+
- lib/humanize/locales/constants/fr_ch.rb
|
109
111
|
- lib/humanize/locales/constants/id.rb
|
110
112
|
- lib/humanize/locales/constants/jp.rb
|
111
113
|
- lib/humanize/locales/constants/ms.rb
|
@@ -114,10 +116,12 @@ files:
|
|
114
116
|
- lib/humanize/locales/constants/th.rb
|
115
117
|
- lib/humanize/locales/constants/tr.rb
|
116
118
|
- lib/humanize/locales/constants/vi.rb
|
119
|
+
- lib/humanize/locales/constants/zh_tw.rb
|
117
120
|
- lib/humanize/locales/de.rb
|
118
121
|
- lib/humanize/locales/en.rb
|
119
122
|
- lib/humanize/locales/es.rb
|
120
123
|
- lib/humanize/locales/fr.rb
|
124
|
+
- lib/humanize/locales/fr_ch.rb
|
121
125
|
- lib/humanize/locales/id.rb
|
122
126
|
- lib/humanize/locales/jp.rb
|
123
127
|
- lib/humanize/locales/ms.rb
|
@@ -126,12 +130,16 @@ files:
|
|
126
130
|
- lib/humanize/locales/th.rb
|
127
131
|
- lib/humanize/locales/tr.rb
|
128
132
|
- lib/humanize/locales/vi.rb
|
133
|
+
- lib/humanize/locales/zh_tw.rb
|
134
|
+
- lib/humanize/module.rb
|
129
135
|
- spec/configuration_spec.rb
|
136
|
+
- spec/core_ext_spec.rb
|
130
137
|
- spec/humanize_spec.rb
|
131
138
|
- spec/locales/az_spec.rb
|
132
139
|
- spec/locales/de_spec.rb
|
133
140
|
- spec/locales/en_spec.rb
|
134
141
|
- spec/locales/es_spec.rb
|
142
|
+
- spec/locales/fr_ch_spec.rb
|
135
143
|
- spec/locales/fr_spec.rb
|
136
144
|
- spec/locales/id_spec.rb
|
137
145
|
- spec/locales/jp_spec.rb
|
@@ -141,6 +149,7 @@ files:
|
|
141
149
|
- spec/locales/th_spec.rb
|
142
150
|
- spec/locales/tr_spec.rb
|
143
151
|
- spec/locales/vi_spec.rb
|
152
|
+
- spec/locales/zh_tw_spec.rb
|
144
153
|
- spec/spec_helper.rb
|
145
154
|
homepage: https://github.com/radar/humanize
|
146
155
|
licenses: []
|