bangou 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source :rubygems
2
2
 
3
- group :development, :test do
4
- gem "bacon"
5
- end
3
+ gem "rake"
4
+ gem "bacon"
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  task :spec do
3
- sh "bacon spec/bangou_spec.rb"
3
+ sh "bacon spec/*_spec.rb"
4
4
  end
5
5
 
6
6
  task :default => :spec
@@ -1,5 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ require 'extensions/integer.rb'
4
+
3
5
  class Bangou
4
6
  class OutOfRangeException < Exception; end
5
7
 
@@ -50,7 +52,7 @@ class Bangou
50
52
  EXCEPTIONS = {}
51
53
 
52
54
  ZEROES = {
53
- :text => "ぜろ",
55
+ :text => "ゼロ",
54
56
  :numeral => "0"
55
57
  }
56
58
 
@@ -71,23 +73,32 @@ class Bangou
71
73
  return ZEROES[format] if int == 0
72
74
  digits = int.to_s.split(//).reverse
73
75
  digits.each_with_index.map do |digit, power|
74
- num = digit.to_i
75
- base = 10 ** power
76
- if value = exceptions(num * base, format)
77
- value
78
- else
79
- if num > 0
80
- if power > 4
81
- text = exceptions(num * base/10000, format) || numerals(num, format) + bases(base/10000, format)
82
- text += bases(10000, format) unless digits.size > 1 and (digits[0..power - 1] & ("1".."9").to_a).size > 0
83
- else
84
- text = numerals(num, format) + bases(base, format)
85
- end
86
- end
87
- text = text[1..-1] if text =~ /^(一).+/
88
- text = text[2..-1] if text =~ /^(いち).+/
89
- text
90
- end
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)
91
78
  end.reverse.join
92
79
  end
80
+
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
86
+
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
90
+
91
+ 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
93
104
  end
@@ -0,0 +1,11 @@
1
+
2
+ class Integer
3
+
4
+ def to_j
5
+ Bangou.integer_to_japanese_numerals(self)
6
+ end
7
+
8
+ def to_jtext
9
+ Bangou.integer_to_japanese_text(self)
10
+ end
11
+ end
data/readme.md CHANGED
@@ -2,11 +2,20 @@
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)
6
+
5
7
  ## Usage
6
8
 
7
9
  ```
8
10
  require 'bangou'
9
11
 
12
+
13
+ 89.to_j
14
+ # => "八十九"
15
+
16
+ 289.to_jtext
17
+ # => "二百八十九"
18
+
10
19
  Bangou.integer_to_japanese_numerals(2305)
11
20
  # => "二千三百五"
12
21
 
@@ -60,7 +60,7 @@ describe "Bangou" do
60
60
 
61
61
  it "ignores zeroes, unless the number is just zero" do
62
62
  compare(7509, "ななせんごひゃくきゅう")
63
- compare(0, "ぜろ")
63
+ compare(0, "ゼロ")
64
64
  compare(80000, "はちまん")
65
65
  compare(80001, "はちまんいち")
66
66
  compare(80020, "はちまんにじゅう")
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.0'
4
+ version: '1.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -40,6 +40,7 @@ files:
40
40
  - LICENSE
41
41
  - readme.md
42
42
  - lib/bangou.rb
43
+ - lib/extensions/integer.rb
43
44
  - spec/bangou_spec.rb
44
45
  homepage: http://kattrali.github.com/bangou
45
46
  licenses: []