wintr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # Wintr
2
+
3
+ A number to word converter.
4
+
5
+ ## Usage
6
+
7
+ Wintr::Number.new(0).to_w
8
+ # => "zero"
9
+
10
+ Wintr::Number.new(9*9).to_w
11
+ # => "eighty one"
12
+
13
+ Wintr::Number.new(951_213_724).to_w
14
+ # => "nine hundred and fifty one million two hundred and thirteen thousand seven hundred and twenty four"
@@ -0,0 +1,22 @@
1
+ require 'wintr/one_digit_group'
2
+ require 'wintr/two_digit_group'
3
+ require 'wintr/three_digit_group'
4
+
5
+ module Wintr
6
+ class DigitGroup
7
+ def initialize(digits)
8
+ @digits = digits
9
+ end
10
+
11
+ def to_w
12
+ case @digits.size
13
+ when 1
14
+ OneDigitGroup.new(@digits[0]).to_w
15
+ when 2
16
+ TwoDigitGroup.new(@digits[0], @digits[1]).to_w
17
+ when 3
18
+ ThreeDigitGroup.new(@digits[0], @digits[1], @digits[2]).to_w
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'wintr/weighted_digit_group'
2
+ require 'wintr/word_array'
3
+
4
+ module Wintr
5
+ class Number
6
+ def initialize(number)
7
+ @number = number
8
+ end
9
+
10
+ def to_w
11
+ return 'zero' if @number == 0
12
+
13
+ word_array = []
14
+ digit_array = @number.to_s.chars.to_a
15
+ power_of_thousand = 0
16
+
17
+ until digit_array == [] do
18
+ digit_group = digit_array.pop(3)
19
+ word_array.unshift(WeightedDigitGroup.new(digit_group, power_of_thousand).to_w)
20
+ power_of_thousand += 1
21
+ end
22
+ WordArray.new(word_array).to_w
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module Wintr
2
+ class OneDigitGroup
3
+ UNITS = {
4
+ '0' => '',
5
+ '1' => 'one',
6
+ '2' => 'two',
7
+ '3' => 'three',
8
+ '4' => 'four',
9
+ '5' => 'five',
10
+ '6' => 'six',
11
+ '7' => 'seven',
12
+ '8' => 'eight',
13
+ '9' => 'nine'
14
+ }
15
+
16
+ def initialize(units)
17
+ @units = units
18
+ end
19
+
20
+ def to_w
21
+ UNITS[@units]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module Wintr
2
+ class PowerOfThousand
3
+ POWER = {
4
+ 1 => 'thousand',
5
+ 2 => 'million'
6
+ }
7
+
8
+ def initialize(power)
9
+ @power = power
10
+ end
11
+
12
+ def to_w
13
+ POWER[@power] || ''
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,21 @@
1
+ module Wintr
2
+ class ThreeDigitGroup
3
+ def initialize(hundreds, tens, units)
4
+ @hundreds, @tens, @units = hundreds, tens, units
5
+ end
6
+
7
+ def to_w
8
+ word_array = []
9
+ if @hundreds == '0' && @tens == '0' && @units == '0'
10
+ ''
11
+ else
12
+ word_array << OneDigitGroup.new(@hundreds).to_w
13
+ word_array << 'hundred' if @hundreds != '0'
14
+ word_array << 'and' unless @tens == '0' && @units == '0'
15
+ word_array << TwoDigitGroup.new(@tens, @units).to_w
16
+ WordArray.new(word_array).to_w
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,45 @@
1
+ module Wintr
2
+ class TwoDigitGroup
3
+ TEN_TO_NINETEEN = {
4
+ '10' => 'ten',
5
+ '11' => 'eleven',
6
+ '12' => 'twelve',
7
+ '13' => 'thirteen',
8
+ '14' => 'fourteen',
9
+ '15' => 'fifteen',
10
+ '16' => 'sixteen',
11
+ '17' => 'seventeen',
12
+ '18' => 'eighteen',
13
+ '19' => 'nineteen'
14
+ }
15
+ TENS = {
16
+ '2' => 'twenty',
17
+ '3' => 'thirty',
18
+ '4' => 'forty',
19
+ '5' => 'fifty',
20
+ '6' => 'sixty',
21
+ '7' => 'seventy',
22
+ '8' => 'eighty',
23
+ '9' => 'ninety'
24
+ }
25
+
26
+ def initialize(tens, units)
27
+ @tens, @units = tens, units
28
+ end
29
+
30
+ def to_w
31
+ word_array = []
32
+
33
+ if @tens == '1'
34
+ TEN_TO_NINETEEN[@tens + @units]
35
+ elsif @tens == '0' && @units == '0'
36
+ ''
37
+ else
38
+ word_array << TENS[@tens]
39
+ word_array << OneDigitGroup.new(@units).to_w
40
+ WordArray.new(word_array).to_w
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,3 @@
1
+ module Wintr
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'wintr/digit_group'
2
+ require 'wintr/power_of_thousand'
3
+ require 'wintr/word_array'
4
+
5
+ module Wintr
6
+ class WeightedDigitGroup
7
+ def initialize(digit_group, power_of_thousand)
8
+ @digit_group, @power_of_thousand = digit_group, power_of_thousand
9
+ end
10
+
11
+ def to_w
12
+ base_digit_group_in_words = DigitGroup.new(@digit_group).to_w
13
+ power_of_thousand_in_words = PowerOfThousand.new(@power_of_thousand).to_w if base_digit_group_in_words != ''
14
+ WordArray.new([base_digit_group_in_words, power_of_thousand_in_words]).to_w
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Wintr
2
+ class WordArray
3
+ def initialize(array)
4
+ @array = array
5
+ end
6
+
7
+ def to_w
8
+ @array.join(' ').squeeze(' ').strip
9
+ end
10
+ end
11
+ end
12
+
data/lib/wintr.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'wintr/version'
2
+ require 'wintr/number'
@@ -0,0 +1,2 @@
1
+ require 'wintr'
2
+
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+
5
+ describe DigitGroup do
6
+ describe "#to_w" do
7
+ it "should convert 0 to empty string" do
8
+ DigitGroup.new(['0']).to_w.should == ''
9
+ end
10
+ it "should convert 9" do
11
+ DigitGroup.new(['9']).to_w.should == 'nine'
12
+ end
13
+ it "should convert 10" do
14
+ DigitGroup.new(%w[1 0]).to_w.should == 'ten'
15
+ end
16
+ it "should convert 19" do
17
+ DigitGroup.new(%w[1 9]).to_w.should == 'nineteen'
18
+ end
19
+ it "should convert 20" do
20
+ DigitGroup.new(%w[2 0]).to_w.should == 'twenty'
21
+ end
22
+ it "should convert 702" do
23
+ DigitGroup.new(%w[7 0 2]).to_w.should == 'seven hundred and two'
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+
5
+ describe Number do
6
+ describe "#to_w" do
7
+ it "should convert 1" do
8
+ Number.new(1).to_w.should == 'one'
9
+ end
10
+ it "should convert 27" do
11
+ Number.new(27).to_w.should == 'twenty seven'
12
+ end
13
+ it "should convert 115" do
14
+ Number.new(115).to_w.should == 'one hundred and fifteen'
15
+ end
16
+ it "should convert 3,700" do
17
+ Number.new(3_700).to_w.should == 'three thousand seven hundred'
18
+ end
19
+ it "should convert 56,945,781" do
20
+ Number.new(56_945_781).to_w.should == 'fifty six million nine hundred and forty five thousand seven hundred and eighty one'
21
+ end
22
+ it "should convert 126,000,010" do
23
+ Number.new(126_000_010).to_w.should == 'one hundred and twenty six million and ten'
24
+ end
25
+ it "should convert 999,999,999" do
26
+ Number.new(999_999_999).to_w.should == 'nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine'
27
+ end
28
+
29
+ # extra tests
30
+ it "should convert 9" do
31
+ Number.new(9).to_w.should == 'nine'
32
+ end
33
+ it "should convert 10" do
34
+ Number.new(10).to_w.should == 'ten'
35
+ end
36
+ it "should convert 11" do
37
+ Number.new(11).to_w.should == 'eleven'
38
+ end
39
+ it "should convert 12" do
40
+ Number.new(12).to_w.should == 'twelve'
41
+ end
42
+ it "should convert 13" do
43
+ Number.new(13).to_w.should == 'thirteen'
44
+ end
45
+ it "should convert 14" do
46
+ Number.new(14).to_w.should == 'fourteen'
47
+ end
48
+ it "should convert 15" do
49
+ Number.new(15).to_w.should == 'fifteen'
50
+ end
51
+ it "should convert 16" do
52
+ Number.new(16).to_w.should == 'sixteen'
53
+ end
54
+ it "should convert 17" do
55
+ Number.new(17).to_w.should == 'seventeen'
56
+ end
57
+ it "should convert 18" do
58
+ Number.new(18).to_w.should == 'eighteen'
59
+ end
60
+ it "should convert 19" do
61
+ Number.new(19).to_w.should == 'nineteen'
62
+ end
63
+ it "should convert 20" do
64
+ Number.new(20).to_w.should == 'twenty'
65
+ end
66
+ it "should convert 21" do
67
+ Number.new(21).to_w.should == 'twenty one'
68
+ end
69
+ it "should convert 99" do
70
+ Number.new(99).to_w.should == 'ninety nine'
71
+ end
72
+ it "should convert 100" do
73
+ Number.new(100).to_w.should == 'one hundred'
74
+ end
75
+ it "should convert 102" do
76
+ Number.new(102).to_w.should == 'one hundred and two'
77
+ end
78
+ it "should convert 3,000" do
79
+ Number.new(3000).to_w.should == 'three thousand'
80
+ end
81
+ it "should convert 3,007" do
82
+ Number.new(3007).to_w.should == 'three thousand and seven'
83
+ end
84
+ end
85
+ end
86
+
87
+ end
88
+
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+ describe OneDigitGroup do
5
+ describe "#to_w" do
6
+ it "should convert 0 to empty string" do
7
+ OneDigitGroup.new('0').to_w.should == ''
8
+ end
9
+ it "should convert 1" do
10
+ OneDigitGroup.new('1').to_w.should == 'one'
11
+ end
12
+ it "should convert 2" do
13
+ OneDigitGroup.new('2').to_w.should == 'two'
14
+ end
15
+ it "should convert 3" do
16
+ OneDigitGroup.new('3').to_w.should == 'three'
17
+ end
18
+ it "should convert 4" do
19
+ OneDigitGroup.new('4').to_w.should == 'four'
20
+ end
21
+ it "should convert 5" do
22
+ OneDigitGroup.new('5').to_w.should == 'five'
23
+ end
24
+ it "should convert 6" do
25
+ OneDigitGroup.new('6').to_w.should == 'six'
26
+ end
27
+ it "should convert 7" do
28
+ OneDigitGroup.new('7').to_w.should == 'seven'
29
+ end
30
+ it "should convert 8" do
31
+ OneDigitGroup.new('8').to_w.should == 'eight'
32
+ end
33
+ it "should convert 9" do
34
+ OneDigitGroup.new('9').to_w.should == 'nine'
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+
5
+ describe PowerOfThousand do
6
+ describe "#to_w" do
7
+ it "should convert 0 to empty string" do
8
+ PowerOfThousand.new(0).to_w.should == ''
9
+ end
10
+ it "should convert 1 into it's power-of-a-thousand equivalent" do
11
+ PowerOfThousand.new(1).to_w.should == 'thousand'
12
+ end
13
+ it "should convert 2 into it's power-of-a-thousand equivalent" do
14
+ PowerOfThousand.new(2).to_w.should == 'million'
15
+ end
16
+ it "should convert rubbish input into empty string" do
17
+ PowerOfThousand.new('foo').to_w.should == ''
18
+ end
19
+ it "should convert integers over 2 into empty string" do
20
+ PowerOfThousand.new(3).to_w.should == ''
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+ describe ThreeDigitGroup do
5
+ describe "#to_w" do
6
+ it "should convert 000 to empty string" do
7
+ ThreeDigitGroup.new('0', '0', '0').to_w.should == ''
8
+ end
9
+ it "should convert 101" do
10
+ ThreeDigitGroup.new('1', '0', '1').to_w.should == 'one hundred and one'
11
+ end
12
+ it "should convert 900" do
13
+ ThreeDigitGroup.new('9', '0', '0').to_w.should == 'nine hundred'
14
+ end
15
+ it "should convert 070" do
16
+ ThreeDigitGroup.new('0', '7', '0').to_w.should == 'and seventy'
17
+ end
18
+ it "should convert 005" do
19
+ ThreeDigitGroup.new('0', '0', '5').to_w.should == 'and five'
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+ describe TwoDigitGroup do
5
+ describe "#to_w" do
6
+ it "should convert 00 to empty string" do
7
+ TwoDigitGroup.new('0', '0').to_w.should == ''
8
+ end
9
+ it "should convert 01 to one" do
10
+ TwoDigitGroup.new('0', '1').to_w.should == 'one'
11
+ end
12
+ it "should convert 09 to nine" do
13
+ TwoDigitGroup.new('0', '9').to_w.should == 'nine'
14
+ end
15
+ it "should convert 10" do
16
+ TwoDigitGroup.new('1', '0').to_w.should == 'ten'
17
+ end
18
+ it "should convert 11" do
19
+ TwoDigitGroup.new('1', '1').to_w.should == 'eleven'
20
+ end
21
+ it "should convert 12" do
22
+ TwoDigitGroup.new('1', '2').to_w.should == 'twelve'
23
+ end
24
+ it "should convert 13" do
25
+ TwoDigitGroup.new('1', '3').to_w.should == 'thirteen'
26
+ end
27
+ it "should convert 14" do
28
+ TwoDigitGroup.new('1', '4').to_w.should == 'fourteen'
29
+ end
30
+ it "should convert 15" do
31
+ TwoDigitGroup.new('1', '5').to_w.should == 'fifteen'
32
+ end
33
+ it "should convert 16" do
34
+ TwoDigitGroup.new('1', '6').to_w.should == 'sixteen'
35
+ end
36
+ it "should convert 17" do
37
+ TwoDigitGroup.new('1', '7').to_w.should == 'seventeen'
38
+ end
39
+ it "should convert 18" do
40
+ TwoDigitGroup.new('1', '8').to_w.should == 'eighteen'
41
+ end
42
+ it "should convert 19" do
43
+ TwoDigitGroup.new('1', '9').to_w.should == 'nineteen'
44
+ end
45
+ it "should convert 20" do
46
+ TwoDigitGroup.new('2', '0').to_w.should == 'twenty'
47
+ end
48
+ it "should convert 21" do
49
+ TwoDigitGroup.new('2', '1').to_w.should == 'twenty one'
50
+ end
51
+ it "should convert 77" do
52
+ TwoDigitGroup.new('7', '7').to_w.should == 'seventy seven'
53
+ end
54
+ it "should convert 99" do
55
+ TwoDigitGroup.new('9', '9').to_w.should == 'ninety nine'
56
+ end
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+ describe WeightedDigitGroup do
5
+ describe "#to_w" do
6
+ it "should should convert 23 thousand" do
7
+ WeightedDigitGroup.new(%w[2 1], 1).to_w.should == 'twenty one thousand'
8
+ end
9
+ it "should should convert 123 thousand" do
10
+ WeightedDigitGroup.new(%w[1 2 3], 1).to_w.should == 'one hundred and twenty three thousand'
11
+ end
12
+ it "should should convert 103 million" do
13
+ WeightedDigitGroup.new(%w[1 0 3], 2).to_w.should == 'one hundred and three million'
14
+ end
15
+ it "should should convert 000 thousand to empty string" do
16
+ WeightedDigitGroup.new(%w[0 0 0], 1).to_w.should == ''
17
+ end
18
+ it "should should convert 000 million to empty string" do
19
+ WeightedDigitGroup.new(%w[0 0 0], 2).to_w.should == ''
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ module Wintr
4
+ describe WordArray do
5
+ describe "#to_w" do
6
+ it "should join it's array then strip and squeeze spaces" do
7
+ WordArray.new([' foo ', ' bar ']).to_w.should == 'foo bar'
8
+ end
9
+ end
10
+ end
11
+ end
12
+
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wintr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - R Headley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &11773780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.8'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *11773780
25
+ description: A number to word converter
26
+ email:
27
+ - headleyra@yahoo.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files:
31
+ - README.md
32
+ files:
33
+ - lib/wintr/weighted_digit_group.rb
34
+ - lib/wintr/number.rb
35
+ - lib/wintr/digit_group.rb
36
+ - lib/wintr/power_of_thousand.rb
37
+ - lib/wintr/word_array.rb
38
+ - lib/wintr/two_digit_group.rb
39
+ - lib/wintr/version.rb
40
+ - lib/wintr/three_digit_group.rb
41
+ - lib/wintr/one_digit_group.rb
42
+ - lib/wintr.rb
43
+ - README.md
44
+ - spec/wintr/power_of_thousand_spec.rb
45
+ - spec/wintr/word_array_spec.rb
46
+ - spec/wintr/weighted_digit_group_spec.rb
47
+ - spec/wintr/one_digit_group_spec.rb
48
+ - spec/wintr/number_spec.rb
49
+ - spec/wintr/two_digit_group_spec.rb
50
+ - spec/wintr/three_digit_group_spec.rb
51
+ - spec/wintr/digit_group_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: http://github.com/headleyra/wintr
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.15
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A number to word converter
77
+ test_files:
78
+ - spec/wintr/power_of_thousand_spec.rb
79
+ - spec/wintr/word_array_spec.rb
80
+ - spec/wintr/weighted_digit_group_spec.rb
81
+ - spec/wintr/one_digit_group_spec.rb
82
+ - spec/wintr/number_spec.rb
83
+ - spec/wintr/two_digit_group_spec.rb
84
+ - spec/wintr/three_digit_group_spec.rb
85
+ - spec/wintr/digit_group_spec.rb
86
+ - spec/spec_helper.rb