Skip to content

Commit

Permalink
Merge branch 'master' into separate-core_ext
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed May 8, 2024
2 parents 0abcfc9 + 7bfa222 commit 68cdc35
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Currently supported locales:
* Malaysia `:ms`
* Japanese `:jp`
* Vietnamese `:vi`
* Traditional Chinese `:zh-tw`

## Testing

Expand Down
2 changes: 1 addition & 1 deletion lib/humanize/locales.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Humanize
%w[az de en es fr fr_ch id ms pt ru th tr jp vi].each do |locale|
%w[az de en es fr fr_ch id ms pt ru th tr jp vi zh_tw].each do |locale|
autoload locale.split('_').map(&:capitalize).join.to_sym, "humanize/locales/#{locale}.rb"
end
end
10 changes: 10 additions & 0 deletions lib/humanize/locales/constants/zh_tw.rb

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions lib/humanize/locales/zh_tw.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative 'constants/zh_tw'

module Humanize
class ZhTw
def humanize(number)
iteration = 0
parts = []
until number.zero?
number, remainder = number.divmod(10_000)
unless remainder.zero?
add_grouping(parts, iteration)
parts << SUB_ONE_GROUPING[remainder]
end

iteration += 1
end

parts
end

private

def add_grouping(parts, iteration)
grouping = LOTS[iteration]
return unless grouping

parts << grouping.to_s
end
end
end
11 changes: 4 additions & 7 deletions lib/humanize/module.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

require 'bigdecimal'
require_relative 'locales'

Expand All @@ -25,13 +23,13 @@ def format(number,
sign = locale_class::NEGATIVE if number.negative?

parts = locale_class.new.humanize(number.abs)
Humanize.process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
Humanize.stringify(parts, sign, spacer)
end

def for_locale(locale)
case locale.to_sym
# NOTE: add locales here in ealphabetical order
# NOTE: add locales here in alphabetical order
when :az, :de, :en, :es, :fr, :id, :ms, :pt, :ru, :vi
[Object.const_get("Humanize::#{locale.capitalize}"), ' ']
when :th
Expand All @@ -40,6 +38,8 @@ def for_locale(locale)
[Humanize::Tr, ' ']
when :jp
[Humanize::Jp, '']
when :'zh-tw'
[Humanize::ZhTw, '']
when :'fr-CH'
[Humanize::FrCh, ' ']
else
Expand All @@ -62,10 +62,7 @@ def locale_is?(locale)
Humanize.config.default_locale == locale
end

# rubocop:disable Metrics/ParameterLists
def process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
# rubocop:enable Metrics/ParameterLists

return unless number.is_a?(Float) || number.is_a?(BigDecimal)

# Why 15?
Expand Down
55 changes: 55 additions & 0 deletions spec/locales/zh_tw_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

RSpec.describe Humanize, 'zh tw locale' do
before do
Humanize.configure do |config|
config.default_locale = :'zh-tw'
end
end

tests = [
[1, '壹'],
[48, '肆拾捌'],
[100, '佰'],
[101, '佰壹'],
[110, '佰拾'],
[199, '佰玖拾玖'],
[999, '玖佰玖拾玖'],
[1000, '壹仟'],
[3456, '参仟肆佰伍拾陸'],
[9999, '玖仟玖佰玖拾玖'],
[21_111, '貳萬壹仟佰拾壹'],
[99_999, '玖萬玖仟玖佰玖拾玖'],
[9_876_543, '玖佰捌拾柒萬陸仟伍佰肆拾参'],
[10_000_000, '壹仟萬'],
[100_000_000, '壹億'],
[765_432_109, '柒億陸仟伍佰肆拾参萬貳仟佰玖'],
[-123_422_223.48_948_753, '負壹億貳仟参佰肆拾貳萬貳仟貳佰貳拾参・肆捌玖肆捌捌']
]

tests.each do |num, output|
it "#{num} equals #{output}" do
expect(num.humanize).to eql(output)
end
end

context 'decimals: number' do
it 'returns the decimals as whole numbers' do
num = 8.1592
expect(num.humanize).to eq('捌・壹伍玖貳')
expect(num.humanize(decimals_as: :number)).to eq('捌・壹仟伍佰玖拾貳')
end
end

describe 'when called on conceptual number' do
it 'reads correctly' do
inf = Float::INFINITY
neg_inf = -inf
nan = inf + neg_inf

expect(inf.humanize).to eql('無限大')
expect(neg_inf.humanize).to eql('負無限大')
expect(nan.humanize).to eql('未定義')
end
end
end

0 comments on commit 68cdc35

Please sign in to comment.