Skip to content

Commit

Permalink
Zhongwen Tools 0.17.5
Browse files Browse the repository at this point in the history
+ Updated gemspec.
+ Bignum support.
+ Fixed ZhongwenTools::StringExtension.split_romanization method
  • Loading branch information
stevendaniels committed Feb 25, 2015
1 parent 768a570 commit e6367f6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
16 changes: 11 additions & 5 deletions lib/zhongwen_tools/number.rb
Expand Up @@ -44,7 +44,7 @@ def self.to_zh(obj, type = :zhs, from = nil)

def self.convert(obj, to, from, separator = '')
fail ArgumentError unless [:zhs, :zht, :i, :pyn].include?(to.to_sym)
fail ArgumentError unless [String, Integer, Fixnum].include?(obj.class)
fail ArgumentError unless [String, Integer, Fixnum, Bignum].include?(obj.class)

number = convert_from from, to, obj

Expand All @@ -61,7 +61,7 @@ def self.convert(obj, to, from, separator = '')
def self.number_type(obj)
klass = obj.class

if klass == Fixnum || klass == Integer
if [Fixnum, Integer, Bignum].include?(klass)
:i
else
if ZhongwenTools::Zhongwen.zh?(obj)
Expand Down Expand Up @@ -175,7 +175,13 @@ def self.convert_from_integer to, int
if i == 0
result << convert_integer(num, to) unless num == 0
else
result << convert_wan_level(i, to)
if i < 5
result << convert_wan_level(i, to)
elsif i == 8
result << convert_wan_level(i, to)
else
result << "#{ convert_wan_level(i - (i / 4 * 4), to) }#{ convert_wan_level((i / 4 * 4), to) }"
end
# checks the wan level and ...
result << convert_integer(num, to) if wan_ok?(num, wan, i)
end
Expand All @@ -189,7 +195,7 @@ def self.convert_integer_to_reversed_array_of_integers(int)
end

def self.wan_ok?(num, wan, i)
(num == 1 && (10**(i) / 10_000 ** wan) != 10) || num != 1
(num == 1 && (10**(i) / 10_000 ** wan) != 10) || num != 1
end

def self.wan_level(wan, i)
Expand All @@ -215,7 +221,7 @@ def self.finalize_number(number, separator = '')
# FIXME: is finalize_number the best name you can think of?
# NOTE: Figuring out usage of "liang" vs. "er" is pretty
# difficult, so always use "er" instead.
number.join(separator).gsub(/零#{ZhongwenTools::Regex.zh_number_multiple}/u,'')
number.join(separator).gsub(/零[#{ Regex.zh_number_multiple }]*/u,'')
end
end
end
7 changes: 5 additions & 2 deletions lib/zhongwen_tools/number/number_table.rb
Expand Up @@ -2,8 +2,10 @@

module ZhongwenTools
module Number
# TODO: Add huge numbers.
# 垓 秭 穰 溝 澗 正 載 --> beyond 100,000,000!
# TODO: Add huge numbers. http://en.wikipedia.org/wiki/Chinese_numerals#Large_numbers
# 垓=亿 (archaic)
# 秭: 1B or 100B or 1T or 10000T (archaic)
# 穰 溝 澗 正 載 --> beyond 100,000,000!
# NOTE: financial numbers i == 0 ? NT.select{ |x| x[:i] == i }.last[:zhs] : NT.find{ |x| x[:i] = i }
NUMBERS_TABLE = [
{ zhs: '零', zht: '零', i: 0, pyn: 'ling2' },
Expand Down Expand Up @@ -39,6 +41,7 @@ module Number
{ zhs: '仟', zht: '仟', i: 1_000, pyn: 'qian1' },
{ zhs: '万', zht: '萬', i: 10_000, pyn: 'wan4' },
{ zhs: '亿', zht: '億', i: 100_000_000, pyn: 'yi4' },
{ zhs: '京', zht: '京', i: 1_000_000_000_000_000, pyn: 'jing1' }
]
end
end
2 changes: 1 addition & 1 deletion lib/zhongwen_tools/string_extension.rb
Expand Up @@ -138,7 +138,7 @@ def romanization?
end

def split_romanization
ZhongwenTools::romanization.split(self)
ZhongwenTools::Romanization.split(self)
end

def zhs?
Expand Down
2 changes: 1 addition & 1 deletion lib/zhongwen_tools/version.rb
@@ -1,3 +1,3 @@
module ZhongwenTools
VERSION = '0.17.3'
VERSION = '0.17.5'
end
6 changes: 6 additions & 0 deletions test/test_number.rb
Expand Up @@ -76,4 +76,10 @@ def test_to_i
assert_equal n[:i], ZhongwenTools::Number.to_i(n[:zhs])
end
end

def test_large_numbers
assert_equal '五十万', ZhongwenTools::Number.to_zhs(500_000)
assert_equal '五十亿', ZhongwenTools::Number.to_zhs(5_000_000_000)
#assert_equal '五万亿', ZhongwenTools::Number.to_zhs(5_000_000_000_000)
end
end
26 changes: 13 additions & 13 deletions zhongwen_tools.gemspec
@@ -1,31 +1,31 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
$:.push File.expand_path('../lib', __FILE__)
require 'zhongwen_tools/version'

Gem::Specification.new do |s|
s.name = "zhongwen_tools"
s.license = "MIT"
s.name = 'zhongwen_tools'
s.license = 'MIT'
s.version = ZhongwenTools::VERSION
s.authors = ["Steven Daniels"]
s.email = ["steven@tastymantou.com"]
s.homepage = "https://github.com/stevendaniels/zhongwen_tools"
s.authors = ['Steven Daniels']
s.email = ['steven@tastymantou.com']
s.homepage = 'https://github.com/stevendaniels/zhongwen_tools'
s.summary = %q{Zhongwen Tools provide romanization conversions and helper methods for Chinese.}
s.description = %q{Chinese tools for romanization conversions and other helpful string functions for Chinese.}
s.rubyforge_project = "zhongwen_tools"
s.rubyforge_project = 'zhongwen_tools'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.require_paths = ['lib']

s.add_development_dependency('rake', "~> 10.1")
s.add_development_dependency('rake', '~> 10.1')
if RUBY_VERSION >= '1.9'
s.add_development_dependency('simplecov', '~> 0.7', '>= 0.7.0')
s.add_development_dependency('simplecov', '~> 0.9', '>= 0.9.1')
s.add_development_dependency('simplecov-gem-adapter', '~> 1.0', '>= 1.0.1')
s.add_development_dependency('coveralls', '~> 0.7', '>= 0.7.0')
s.add_development_dependency('minitest', '~> 5.3', '>= 5.3.0')
s.add_development_dependency('pry', '~> 0.9', '>= 0.9.12')
s.add_development_dependency('minitest-reporters', '~> 1.0', '>= 1.0.4')
s.add_development_dependency('minitest', '~> 5.5', '>= 5.5.1')
s.add_development_dependency('pry', '~> 0.10', '>= 0.10.1')
s.add_development_dependency('minitest-reporters', '~> 1.0', '>= 1.0.10')
end

if RUBY_VERSION >= '2.1'
Expand Down

0 comments on commit e6367f6

Please sign in to comment.