bangou 1.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ # 1.1.1
2
+
3
+ - Added better class documentation
4
+
5
+ # 1.1
6
+
7
+ - Added extension on Integer to simplify calling the interface:
8
+
9
+ ```
10
+ 89.to_j
11
+ # => "八十九"
12
+
13
+ 289.to_jtext
14
+ # => "二百八十九"
15
+ ```
16
+
17
+ # 1.0
18
+
19
+ Initial release
@@ -5,100 +5,123 @@ require 'extensions/integer.rb'
5
5
  class Bangou
6
6
  class OutOfRangeException < Exception; end
7
7
 
8
- def self.integer_to_japanese_numerals int
8
+ # Convert an Integer into Japanese numerals
9
+ #
10
+ # Example:
11
+ # Bangou.integer_to_japanese_numerals(89)
12
+ # # => "八十九"
13
+ #
14
+ # @param [Integer] the number to convert
15
+ # @return [String] The Japanese numeric equivalent in kanji
16
+ def self.integer_to_japanese_numerals int
9
17
  convert_number(int, :numeral)
10
- end
18
+ end
11
19
 
12
- def self.integer_to_japanese_text int
20
+ # Convert an Integer into Sino-Japanese number
21
+ # spelling in hiragana or katakana
22
+ #
23
+ # Examples:
24
+ # Bangou.integer_to_japanese_text(823)
25
+ # # => "はっぴゃくにじゅうさん"
26
+ # Bangou.integer_to_japanese_text(0)
27
+ # # => "ゼロ"
28
+ #
29
+ # @param [Integer] the number to convert
30
+ # @return [String] The Sino-Japanese numeric equivalent in kana
31
+ def self.integer_to_japanese_text int
13
32
  convert_number(int, :text)
14
- end
33
+ end
15
34
 
35
+ # Detect whether a given number can be processed
36
+ # by this class
37
+ #
38
+ # @return [Boolean]
16
39
  def self.in_range? int
17
40
  int >= 0 and int < 100000000
18
41
  end
19
42
 
20
43
  private
21
44
 
22
- NUMERALS = {
23
- 0 => "", 1 => "一", 2 => "二",
24
- 3 => "三", 4 => "四", 5 => "五",
25
- 6 => "六", 7 => "七", 8 => "八",
26
- 9 => "九"
27
- }
28
-
29
- NUMERALS_TEXT = {
30
- 0 => "", 1 => "いち", 2 => "に",
31
- 3 => "さん", 4 => "よん", 5 => "ご",
32
- 6 => "ろく", 7 => "なな", 8 => "はち",
33
- 9 => "きゅう"
34
- }
35
-
36
- BASES = {
37
- 1 => "", 10 => "十", 100 => "百",
38
- 1000 => "千", 10000 => "万"
39
- }
45
+ NUMERAL_INDEX = 0
46
+ TEXT_INDEX = 1
47
+ RANGE_ERROR_MESSAGE = "Bangou can only process numbers between 0 and 99,999,999"
40
48
 
41
- BASES_TEXT = {
42
- 1 => "", 10 => "じゅう", 100 => "ひゃく",
43
- 1000 => "せん", 10000 => "まん"
49
+ NUMERALS = {
50
+ 0 => ["",""], 1 => ["","いち"], 2 => ["","に"],
51
+ 3 => ["","さん"], 4 => ["","よん"], 5 => ["五","ご"],
52
+ 6 => ["六","ろく"], 7 => ["七","なな"], 8 => ["八","はち"],
53
+ 9 => ["九","きゅう"]
44
54
  }
45
55
 
46
- EXCEPTIONS_TEXT = {
47
- 300 => "さんびゃく", 600 => "ろっぴゃく",
48
- 800 => "はっぴゃく",
49
- 3000 => "さんぜん", 8000 => "はっせん"
50
- }
56
+ BASES = {
57
+ 1 => ["",""],
58
+ 10 => ["","じゅう"],
59
+ 100 => ["","ひゃく"],
60
+ 1000 => ["千","せん"],
61
+ 10000 => ["万","まん"]
62
+ }
51
63
 
52
- EXCEPTIONS = {}
64
+ EXCEPTIONS = {
65
+ 300 => [nil,"さんびゃく"],
66
+ 600 => [nil,"ろっぴゃく"],
67
+ 800 => [nil,"はっぴゃく"],
68
+ 3000 => [nil,"さんぜん"],
69
+ 8000 => [nil,"はっせん"]
70
+ }
53
71
 
54
72
  ZEROES = {
55
73
  :text => "ゼロ",
56
74
  :numeral => "0"
57
75
  }
58
76
 
59
- def self.exceptions num, format
60
- format == :text ? EXCEPTIONS_TEXT[num] : EXCEPTIONS[num]
77
+ def self.index_for_format format
78
+ format == :text ? TEXT_INDEX : NUMERAL_INDEX
79
+ end
80
+
81
+ def self.exceptions num, base, format
82
+ hash_index = num * base
83
+ EXCEPTIONS[hash_index][index_for_format(format)] if EXCEPTIONS[hash_index]
61
84
  end
62
85
 
63
86
  def self.numerals num, format
64
- format == :text ? NUMERALS_TEXT[num] : NUMERALS[num]
87
+ NUMERALS[num][index_for_format(format)]
65
88
  end
66
89
 
67
90
  def self.bases num, format
68
- format == :text ? BASES_TEXT[num] : BASES[num]
91
+ BASES[num][index_for_format(format)]
69
92
  end
70
93
 
71
94
  def self.convert_number int, format
72
- raise OutOfRangeException.new("Bangou can only process numbers between 0 and 99,999,999") unless in_range?(int)
95
+ raise OutOfRangeException.new(RANGE_ERROR_MESSAGE) unless in_range?(int)
73
96
  return ZEROES[format] if int == 0
97
+
74
98
  digits = int.to_s.split(//).reverse
75
- digits.each_with_index.map do |digit, power|
76
- hide_base_from_value = has_more_digits_before_index?(digits, power)
77
- characters_for_digit_in_base(digit.to_i, 10 ** power, format, hide_base_from_value)
78
- end.reverse.join
99
+
100
+ digits.each_with_index.map do |digit, power|
101
+ hide_base_from_value = has_more_digits_after_index?(digits, power)
102
+ characters_for_digit_in_base(digit.to_i, 10 ** power, format, hide_base_from_value)
103
+ end.reverse.join
79
104
  end
80
105
 
81
- def self.strip_unneeded_characters text
82
- text = text[1..-1] if text =~ /^().+/
83
- text = text[2..-1] if text =~ /^(いち).+/
84
- text
85
- end
106
+ def self.strip_leading_ones text
107
+ text.sub(/^(一|いち)(.+)/,'\2')
108
+ end
86
109
 
87
- def self.has_more_digits_before_index?(digits, index)
88
- digits.size > 1 and (digits[0..index - 1] & ("1".."9").to_a).size > 0
89
- end
110
+ def self.has_more_digits_after_index?(digits, index)
111
+ digits.size > 1 and (digits[0..index - 1] & ("1".."9").to_a).size > 0
112
+ end
90
113
 
91
114
  def self.characters_for_digit_in_base(num, base, format, hide_base_from_value)
92
- if text = exceptions(num * base, format)
93
- text
94
- else
95
- if num > 0 and base > 10000
96
- text = exceptions(num * base/10000, format) || characters_for_digit_in_base(num, base/10000, format, hide_base_from_value)
97
- text += bases(10000, format) unless hide_base_from_value
98
- elsif num > 0
99
- text = numerals(num, format) + bases(base, format)
100
- end
101
- strip_unneeded_characters(text)
102
- end
103
- end
115
+ return "" if num == 0
116
+
117
+ if text = exceptions(num, base, format)
118
+ text
119
+ elsif base <= 10000
120
+ text = numerals(num, format) + bases(base, format)
121
+ else
122
+ text = characters_for_digit_in_base(num, base/10000, format, hide_base_from_value)
123
+ text += bases(10000, format) unless hide_base_from_value
124
+ end
125
+ strip_leading_ones(text)
126
+ end
104
127
  end
@@ -1,10 +1,12 @@
1
1
 
2
2
  class Integer
3
3
 
4
+ # Convert integer to Japanese kanji equivalent
4
5
  def to_j
5
6
  Bangou.integer_to_japanese_numerals(self)
6
7
  end
7
8
 
9
+ # Convert integer to Sino-Japanese kana equivalent
8
10
  def to_jtext
9
11
  Bangou.integer_to_japanese_text(self)
10
12
  end
data/readme.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A Ruby library for converting between positive integers and Sino-Japanese numbers or text.
4
4
 
5
- [![Build Status](https://travis-ci.org/kattrali/bangou.png?branch=master)](Build Status)
5
+ [![Build Status](https://travis-ci.org/kattrali/bangou.png?branch=master)](https://travis-ci.org/kattrali/bangou) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/kattrali/bangou)
6
6
 
7
7
  ## Usage
8
8
 
@@ -37,4 +37,4 @@ Pull requests with tests accepted!
37
37
 
38
38
  ## Current Limitations
39
39
 
40
- Only works on numbers between 0 and 99,999,999 (otherwise throwing an OutOfRangeException).
40
+ Only works on numbers between 0 and 99,999,999 (otherwise throwing an OutOfRangeException).
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+ $:.unshift(File.expand_path('../../lib',__FILE__))
3
+
4
+ require 'bangou.rb'
5
+
6
+ describe "Integer additions" do
7
+ it "adds Integer#to_j which is the kanji equivalent of a given integer" do
8
+ 89.to_j.should.equal "八十九"
9
+ 289.to_j.should.equal "二百八十九"
10
+ end
11
+
12
+ it "adds Integer#to_jtext which is the hiragana equivalent of a given integer" do
13
+ 89.to_jtext.should.equal "はちじゅうきゅう"
14
+ 289.to_jtext.should.equal "にひゃくはちじゅうきゅう"
15
+ end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bangou
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-21 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bacon
@@ -34,14 +34,17 @@ executables: []
34
34
  extensions: []
35
35
  extra_rdoc_files:
36
36
  - readme.md
37
+ - changelog.md
37
38
  files:
38
39
  - Gemfile
39
- - Rakefile
40
40
  - LICENSE
41
+ - Rakefile
42
+ - changelog.md
41
43
  - readme.md
42
44
  - lib/bangou.rb
43
45
  - lib/extensions/integer.rb
44
46
  - spec/bangou_spec.rb
47
+ - spec/integer_spec.rb
45
48
  homepage: http://kattrali.github.com/bangou
46
49
  licenses: []
47
50
  post_install_message:
@@ -69,3 +72,4 @@ summary: A library for converting between positive integers and Sino-Japanese nu
69
72
  or text.
70
73
  test_files:
71
74
  - spec/bangou_spec.rb
75
+ - spec/integer_spec.rb