From ae05a4f4dad11e578ac32a1c726083dd017c6ed8 Mon Sep 17 00:00:00 2001 From: Fox Chao Date: Thu, 29 May 2014 18:52:33 +0800 Subject: [PATCH] initial import --- .gitignore | 22 ++++++++++++ Gemfile | 4 +++ LICENSE.txt | 22 ++++++++++++ README.md | 29 ++++++++++++++++ Rakefile | 3 ++ chinese_numerals.gemspec | 24 ++++++++++++++ lib/chinese_numerals.rb | 59 +++++++++++++++++++++++++++++++++ lib/chinese_numerals/version.rb | 3 ++ spec/chinese_number_spec.rb | 41 +++++++++++++++++++++++ 9 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 chinese_numerals.gemspec create mode 100644 lib/chinese_numerals.rb create mode 100644 lib/chinese_numerals/version.rb create mode 100644 spec/chinese_number_spec.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31cafb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +*.bundle +*.so +*.o +*.a +mkmf.log diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..50f88fe --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in chinese_numerals.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..0b27ac1 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Fox Chao + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..661414d --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# ChineseNumerals + +This simple monkey patching enables the conversion from Integer to Chinese Numerals easily. The single method `to_chinese(:simple => true, :decimal => false)` is the only thing you need to take care. + +## Installation + +Add this line to your application's Gemfile: + + gem 'chinese_numerals' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install chinese_numerals + +## Usage + +Check the `spec/chinese_numerals_spec.rb` to see the various outcomes. + +## Contributing + +1. Fork it ( https://github.com/[my-github-username]/chinese_numerals/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..8e6b258 --- /dev/null +++ b/Rakefile @@ -0,0 +1,3 @@ +require "bundler/gem_tasks" + +require 'tasks/test' diff --git a/chinese_numerals.gemspec b/chinese_numerals.gemspec new file mode 100644 index 0000000..6d8dabd --- /dev/null +++ b/chinese_numerals.gemspec @@ -0,0 +1,24 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'chinese_numerals/version' + +Gem::Specification.new do |spec| + spec.name = "chinese_numerals" + spec.version = ChineseNumerals::VERSION + spec.authors = ["Fox Chao"] + spec.email = ["fox@mcuapps.com"] + spec.summary = %q{Convert integers to Chinese Numerals} + spec.description = %q{A little tool from Steam Computing, Co., Inc.} + spec.homepage = "https://rubygems.org/gems/chinese_numerals" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "rake", "~> 10.3" + spec.add_development_dependency "minitest", "~> 5.3" +end diff --git a/lib/chinese_numerals.rb b/lib/chinese_numerals.rb new file mode 100644 index 0000000..cb91fbc --- /dev/null +++ b/lib/chinese_numerals.rb @@ -0,0 +1,59 @@ +# encoding: UTF-8 + +Integer.class_eval do + CHINESE_GROUPS = [ '', '萬', '億', '兆', '京', '陔' ] + + FORMAL_CHINESE_NUMBERS = %w{ 零 壹 貳 參 肆 伍 陸 柒 捌 玖 } + FORMAL_CHINESE_DECIMALS = [ '', '拾', '佰', '仟' ] + + SIMPLE_CHINESE_NUMBERS = %w{ 零 一 二 三 四 五 六 七 八 九 } + SIMPLE_CHINESE_DECIMALS = [ '', '十', '百', '千' ] + + def to_chinese(args = {}) + opts = { + :simple => true, + :decimal => false + }.merge args + + chinese_numbers = opts[:simple] ? SIMPLE_CHINESE_NUMBERS : FORMAL_CHINESE_NUMBERS + chinese_decimals = opts[:simple] ? SIMPLE_CHINESE_DECIMALS : FORMAL_CHINESE_DECIMALS + + str = self.to_s + len = str.length + res = "" + + if opts[:decimal] && (len > 1) + # counting zeros if it meets + zeros = 0 + + 0.upto len - 1 do |i| + digit = str[i].to_i + if digit == 0 + zeros += 1 + else + if (zeros > 0) + zeros = 0 + res << chinese_numbers[0] + end + res << chinese_numbers[digit] + chinese_decimals[(len - 1 - i) % 4] + end + # append group name unless 4 zeros met + if (len - 1 - i) % 4 == 0 + res << CHINESE_GROUPS[(len - 1 - i) / 4] unless zeros == 4 + # reset zeros for each group + zeros = 0 + end + end + + else + # simply maps each digit + 0.upto len - 1 do |i| + digit = str[i].to_i + res << chinese_numbers[digit] + end + end + + res + end + +end diff --git a/lib/chinese_numerals/version.rb b/lib/chinese_numerals/version.rb new file mode 100644 index 0000000..7a0de62 --- /dev/null +++ b/lib/chinese_numerals/version.rb @@ -0,0 +1,3 @@ +module ChineseNumerals + VERSION = "0.0.2" +end diff --git a/spec/chinese_number_spec.rb b/spec/chinese_number_spec.rb new file mode 100644 index 0000000..862b783 --- /dev/null +++ b/spec/chinese_number_spec.rb @@ -0,0 +1,41 @@ +$VERBOSE = true +require 'minitest/autorun' +require 'minitest/spec' + +require_relative '../lib/chinese_numerals.rb' + +describe Integer do + it "maps each digit" do + 10000.to_chinese.must_equal("一零零零零") + end + + it "maps each digit with formal Chinese digits" do + 10000.to_chinese(:simple => false).must_equal("壹零零零零") + end + + it "emits zero in decimal mode" do + 0.to_chinese(:decimal => true).must_equal("零") + end + + it "generates decimal notations in decimal mode" do + 12.to_chinese(:decimal => true).must_equal("一十二") + end + + it "shortens middle zeros in decimal mode" do + 1002.to_chinese(:decimal => true).must_equal("一千零二") + end + + it "omits the tail zero in decimal mode" do + 1200.to_chinese(:decimal => true).must_equal("一千二百") + end + + it "omits all tail zeros in decimal mode" do + 10000000000.to_chinese(:decimal => true).must_equal("一百億") + end + + it "knows output and omit zeros in decimal mode" do + 10000002000.to_chinese(:decimal => true).must_equal("一百億二千") + end + +end +