bangou 1.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/changelog.md +19 -0
- data/lib/bangou.rb +83 -60
- data/lib/extensions/integer.rb +2 -0
- data/readme.md +2 -2
- data/spec/integer_spec.rb +16 -0
- metadata +7 -3
data/changelog.md
ADDED
data/lib/bangou.rb
CHANGED
@@ -5,100 +5,123 @@ require 'extensions/integer.rb'
|
|
5
5
|
class Bangou
|
6
6
|
class OutOfRangeException < Exception; end
|
7
7
|
|
8
|
-
|
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
|
-
|
18
|
+
end
|
11
19
|
|
12
|
-
|
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
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
49
|
+
NUMERALS = {
|
50
|
+
0 => ["",""], 1 => ["一","いち"], 2 => ["二","に"],
|
51
|
+
3 => ["三","さん"], 4 => ["四","よん"], 5 => ["五","ご"],
|
52
|
+
6 => ["六","ろく"], 7 => ["七","なな"], 8 => ["八","はち"],
|
53
|
+
9 => ["九","きゅう"]
|
44
54
|
}
|
45
55
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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.
|
60
|
-
format == :text ?
|
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
|
-
|
87
|
+
NUMERALS[num][index_for_format(format)]
|
65
88
|
end
|
66
89
|
|
67
90
|
def self.bases num, format
|
68
|
-
|
91
|
+
BASES[num][index_for_format(format)]
|
69
92
|
end
|
70
93
|
|
71
94
|
def self.convert_number int, format
|
72
|
-
raise OutOfRangeException.new(
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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.
|
82
|
-
|
83
|
-
|
84
|
-
text
|
85
|
-
end
|
106
|
+
def self.strip_leading_ones text
|
107
|
+
text.sub(/^(一|いち)(.+)/,'\2')
|
108
|
+
end
|
86
109
|
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
data/lib/extensions/integer.rb
CHANGED
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)](
|
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:
|
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-
|
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
|