wintr 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61ab5b7f12b66cddc01c68cf6ae80a291f647468
4
- data.tar.gz: 94c81209b089347fa155a581983dc1ad875e7bdb
3
+ metadata.gz: c211ba5de98d2c20f5618d5113efec566fba93b6
4
+ data.tar.gz: ff40cda74a2495cf4c4f52a258dd704cf4bf7566
5
5
  SHA512:
6
- metadata.gz: 3943a4886fd78ea55786cff45dab2cffc6792652dd986813d70143177d4ef8d500d38fa0c3a97084ebb6d8f505662bc449c399dca419c55676e5e30424b218ca
7
- data.tar.gz: d1f58301a5e4780080b8554b70b432f02bfb69001f1916dc6dbc38518271295439ddfe4ea72decd140d2e6d210400a18f6b9f914abce5a3b92808c9dddeb4171
6
+ metadata.gz: fa8c452c75a9e2f15a3dde4c8dc90f783237eccff76c50531addd02fa955603ae8bbf5cc8a48ff4c0a5d0c14a109fdb22268195ac1c3914e70ff5c4d2083993b
7
+ data.tar.gz: a9b7757053821a864b574f768e385d950e3fc943a40bcf22ee09d1d9a80390315c666f1f45f90ba8572c01e2418f1a43459d1d80c9ab2f1aef744416e9e0c13c
@@ -1,2 +1,3 @@
1
1
  require 'wintr/version'
2
+ require 'wintr/constants'
2
3
  require 'wintr/number'
@@ -0,0 +1,75 @@
1
+ module Wintr
2
+ UNITS = {
3
+ '0' => '',
4
+ '1' => 'one',
5
+ '2' => 'two',
6
+ '3' => 'three',
7
+ '4' => 'four',
8
+ '5' => 'five',
9
+ '6' => 'six',
10
+ '7' => 'seven',
11
+ '8' => 'eight',
12
+ '9' => 'nine'
13
+ }
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
+ TEN_TO_NINETEEN = {
27
+ '10' => 'ten',
28
+ '11' => 'eleven',
29
+ '12' => 'twelve',
30
+ '13' => 'thirteen',
31
+ '14' => 'fourteen',
32
+ '15' => 'fifteen',
33
+ '16' => 'sixteen',
34
+ '17' => 'seventeen',
35
+ '18' => 'eighteen',
36
+ '19' => 'nineteen'
37
+ }
38
+
39
+ POWER_OF_THOUSAND = {
40
+ 0 => '',
41
+ 1 => 'thousand',
42
+ 2 => 'million',
43
+ 3 => 'billion',
44
+ 4 => 'trillion',
45
+ 5 => 'quadrillion',
46
+ 6 => 'quintillion',
47
+ 7 => 'sextillion',
48
+ 8 => 'septillion',
49
+ 9 => 'octillion',
50
+ 10 => 'nonillion',
51
+ 11 => 'decillion',
52
+ 12 => 'undecillion',
53
+ 13 => 'duodecillion',
54
+ 14 => 'tredecillion',
55
+ 15 => 'quattuordecillion',
56
+ 16 => 'quindecillion',
57
+ 17 => 'sexdecillion',
58
+ 18 => 'septendecillion',
59
+ 19 => 'octodecillion',
60
+ 20 => 'novemdecillion',
61
+ 21 => 'vigintillion',
62
+ 22 => 'unvigintillion',
63
+ 23 => 'duovigintillion',
64
+ 24 => 'trevigintillion',
65
+ 25 => 'quattuorvigintillion',
66
+ 26 => 'quinvigintillion',
67
+ 27 => 'sexvigintillion',
68
+ 28 => 'septenvigintillion',
69
+ 29 => 'octovigintillion',
70
+ 30 => 'novemvigintillion',
71
+ 31 => 'trigintillion',
72
+ 32 => 'untrigintillion',
73
+ 33 => 'duotrigintillion'
74
+ }
75
+ end
@@ -0,0 +1,22 @@
1
+ require 'wintr/units'
2
+ require 'wintr/tens_units'
3
+ require 'wintr/hundreds_tens_units'
4
+
5
+ module Wintr
6
+ class DigitArray
7
+ def initialize(digits)
8
+ @digits = digits
9
+ end
10
+
11
+ def to_s
12
+ case @digits.size
13
+ when 1
14
+ Units.new(*@digits)
15
+ when 2
16
+ TensUnits.new(*@digits)
17
+ when 3
18
+ HundredsTensUnits.new(*@digits)
19
+ end.to_s
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Wintr
2
+ class HundredsTensUnits
3
+ def initialize(hundreds, tens, units)
4
+ @hundreds, @tens, @units = hundreds, tens, units
5
+ end
6
+
7
+ def to_s
8
+ return '' if @hundreds == '0' && @tens == '0' && @units == '0'
9
+ "#{hundreds} #{and_join} #{tens_and_units}".strip
10
+ end
11
+
12
+ def hundreds
13
+ Units.new(@hundreds).to_s + ' hundred' if @hundreds != '0'
14
+ end
15
+
16
+ def and_join
17
+ 'and' unless @tens == '0' && @units == '0'
18
+ end
19
+
20
+ def tens_and_units
21
+ TensUnits.new(@tens, @units).to_s
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,4 @@
1
- require 'wintr/weighted_digit_group'
2
- require 'wintr/word_array'
1
+ require 'wintr/weighted_digit_array'
3
2
 
4
3
  module Wintr
5
4
  class Number
@@ -10,16 +9,16 @@ module Wintr
10
9
  def to_s
11
10
  return 'zero' if @number == 0
12
11
 
13
- word_array = []
14
- digit_array = @number.to_s.chars.to_a
15
- power_of_thousand = 0
12
+ number_in_groups_of_three.each_with_index.inject([]) do |number_as_words_array, (digit_array, power_of_thousand)|
13
+ number_as_words_array.unshift(WeightedDigitArray.new(digit_array, power_of_thousand).to_s)
14
+ end.join(' ').squeeze(' ').strip
15
+ end
16
+
17
+ def number_in_groups_of_three
18
+ # 1,234,567 #=> [%w[5 6 7], %w[2 3 4], %w[1]]
16
19
 
17
- until digit_array == [] do
18
- digit_group = digit_array.pop(3)
19
- word_array.unshift(WeightedDigitGroup.new(digit_group, power_of_thousand).to_s)
20
- power_of_thousand += 1
21
- end
22
- WordArray.new(word_array).to_s
20
+ # TODO: Simplify!
21
+ @number.to_s.chars.to_a.reverse.each_slice(3).inject([]) {|number_as_words_array, three_digit_array| number_as_words_array << three_digit_array.reverse}
23
22
  end
24
23
  end
25
24
  end
@@ -0,0 +1,13 @@
1
+ module Wintr
2
+ class TensUnits
3
+ def initialize(tens, units)
4
+ @tens, @units = tens, units
5
+ end
6
+
7
+ def to_s
8
+ return '' if @tens == '0' && @units == '0'
9
+ return TEN_TO_NINETEEN[@tens + @units] if @tens == '1'
10
+ "#{TENS[@tens]} #{Units.new(@units)}".strip
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Wintr
2
+ class Units
3
+ def initialize(units)
4
+ @units = units
5
+ end
6
+
7
+ def to_s
8
+ UNITS[@units]
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Wintr
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'wintr/digit_array'
2
+
3
+ module Wintr
4
+ class WeightedDigitArray
5
+ def initialize(digit_array, power_of_thousand)
6
+ @digit_array, @power_of_thousand = digit_array, power_of_thousand
7
+ end
8
+
9
+ def to_s
10
+ [digit_array_in_words, power_of_thousand_in_words].join(' ').strip
11
+ end
12
+
13
+ def digit_array_in_words
14
+ DigitArray.new(@digit_array).to_s
15
+ end
16
+
17
+ def power_of_thousand_in_words
18
+ POWER_OF_THOUSAND[@power_of_thousand] if digit_array_in_words != ''
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Wintr::DigitArray do
4
+ describe '#to_s' do
5
+ it 'converts 0 to an empty string' do
6
+ expect(Wintr::DigitArray.new(['0']).to_s).to eq('')
7
+ end
8
+
9
+ it 'converts 9' do
10
+ expect(Wintr::DigitArray.new(['9']).to_s).to eq('nine')
11
+ end
12
+
13
+ it 'converts 10' do
14
+ expect(Wintr::DigitArray.new(%w[1 0]).to_s).to eq('ten')
15
+ end
16
+
17
+ it 'converts 19' do
18
+ expect(Wintr::DigitArray.new(%w[1 9]).to_s).to eq('nineteen')
19
+ end
20
+
21
+ it 'converts 20' do
22
+ expect(Wintr::DigitArray.new(%w[2 0]).to_s).to eq('twenty')
23
+ end
24
+
25
+ it 'converts 702' do
26
+ expect(Wintr::DigitArray.new(%w[7 0 2]).to_s).to eq('seven hundred and two')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Wintr::HundredsTensUnits do
4
+ describe '#to_s' do
5
+ it 'converts 000 to an empty string' do
6
+ expect(Wintr::HundredsTensUnits.new('0', '0', '0').to_s).to eq('')
7
+ end
8
+
9
+ it 'converts 101' do
10
+ expect(Wintr::HundredsTensUnits.new('1', '0', '1').to_s).to eq('one hundred and one')
11
+ end
12
+
13
+ it 'converts 900' do
14
+ expect(Wintr::HundredsTensUnits.new('9', '0', '0').to_s).to eq('nine hundred')
15
+ end
16
+
17
+ it 'converts 070' do
18
+ expect(Wintr::HundredsTensUnits.new('0', '7', '0').to_s).to eq('and seventy')
19
+ end
20
+
21
+ it 'converts 005' do
22
+ expect(Wintr::HundredsTensUnits.new('0', '0', '5').to_s).to eq('and five')
23
+ end
24
+ end
25
+ end
@@ -1,72 +1,106 @@
1
1
  require 'spec_helper'
2
2
 
3
- CONVERSIONS = {
4
- 1 => 'one',
5
- 27 => 'twenty seven',
6
- 115 => 'one hundred and fifteen',
7
- 3_700 => 'three thousand seven hundred',
8
- 56_945_781 => 'fifty six million nine hundred and forty five thousand seven hundred and eighty one',
9
- 126_000_010 => 'one hundred and twenty six million and ten',
10
- 999_999_999 => 'nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine',
3
+ def conversions
4
+ {
5
+ 0 => 'zero',
6
+ 1 => 'one',
7
+ 2 => 'two',
8
+ 3 => 'three',
9
+ 4 => 'four',
10
+ 5 => 'five',
11
+ 6 => 'six',
12
+ 7 => 'seven',
13
+ 8 => 'eight',
14
+ 9 => 'nine',
15
+ 10 => 'ten',
11
16
 
12
- 9 => 'nine',
13
- 10 => 'ten',
14
- 11 => 'eleven',
15
- 12 => 'twelve',
16
- 13 => 'thirteen',
17
- 14 => 'fourteen',
18
- 15 => 'fifteen',
19
- 16 => 'sixteen',
20
- 17 => 'seventeen',
21
- 18 => 'eighteen',
22
- 19 => 'nineteen',
23
- 20 => 'twenty',
24
- 21 => 'twenty one',
17
+ 11 => 'eleven',
18
+ 12 => 'twelve',
19
+ 13 => 'thirteen',
20
+ 14 => 'fourteen',
21
+ 15 => 'fifteen',
22
+ 16 => 'sixteen',
23
+ 17 => 'seventeen',
24
+ 18 => 'eighteen',
25
+ 19 => 'nineteen',
25
26
 
26
- 99 => 'ninety nine',
27
- 100 => 'one hundred',
28
- 102 => 'one hundred and two',
29
- 3000 => 'three thousand',
30
- 3007 => 'three thousand and seven',
27
+ 20 => 'twenty',
28
+ 21 => 'twenty one',
29
+ 30 => 'thirty',
30
+ 32 => 'thirty two',
31
+ 40 => 'forty',
32
+ 43 => 'forty three',
33
+ 50 => 'fifty',
34
+ 54 => 'fifty four',
35
+ 60 => 'sixty',
36
+ 65 => 'sixty five',
37
+ 70 => 'seventy',
38
+ 76 => 'seventy six',
39
+ 80 => 'eighty',
40
+ 87 => 'eighty seven',
41
+ 90 => 'ninety',
42
+ 98 => 'ninety eight',
31
43
 
32
- 9 * (10**9) + 9 => 'nine billion and nine',
33
- 12 * (10**12) + 12 => 'twelve trillion and twelve',
34
- 15 * (10**15) + 15 => 'fifteen quadrillion and fifteen',
35
- 18 * (10**18) + 18 => 'eighteen quintillion and eighteen',
36
- 21 * (10**21) + 21 => 'twenty one sextillion and twenty one',
37
- 24 * (10**24) + 24 => 'twenty four septillion and twenty four',
38
- 27 * (10**27) + 27 => 'twenty seven octillion and twenty seven',
39
- 30 * (10**30) + 30 => 'thirty nonillion and thirty',
40
- 33 * (10**33) + 33 => 'thirty three decillion and thirty three',
41
- 36 * (10**36) + 36 => 'thirty six undecillion and thirty six',
42
- 39 * (10**39) + 39 => 'thirty nine duodecillion and thirty nine',
43
- 42 * (10**42) + 42 => 'forty two tredecillion and forty two',
44
- 45 * (10**45) + 45 => 'forty five quattuordecillion and forty five',
45
- 48 * (10**48) + 48 => 'forty eight quindecillion and forty eight',
46
- 51 * (10**51) + 51 => 'fifty one sexdecillion and fifty one',
47
- 54 * (10**54) + 54 => 'fifty four septendecillion and fifty four',
48
- 57 * (10**57) + 57 => 'fifty seven octodecillion and fifty seven',
49
- 60 * (10**60) + 60 => 'sixty novemdecillion and sixty',
50
- 63 * (10**63) + 63 => 'sixty three vigintillion and sixty three',
51
- 66 * (10**66) + 66 => 'sixty six unvigintillion and sixty six',
52
- 69 * (10**69) + 69 => 'sixty nine duovigintillion and sixty nine',
53
- 72 * (10**72) + 72 => 'seventy two trevigintillion and seventy two',
54
- 75 * (10**75) + 75 => 'seventy five quattuorvigintillion and seventy five',
55
- 78 * (10**78) + 78 => 'seventy eight quinvigintillion and seventy eight',
56
- 81 * (10**81) + 81 => 'eighty one sexvigintillion and eighty one',
57
- 84 * (10**84) + 84 => 'eighty four septenvigintillion and eighty four',
58
- 87 * (10**87) + 87 => 'eighty seven octovigintillion and eighty seven',
59
- 90 * (10**90) + 90 => 'ninety novemvigintillion and ninety',
60
- 93 * (10**93) + 93 => 'ninety three trigintillion and ninety three',
61
- 96 * (10**96) + 96 => 'ninety six untrigintillion and ninety six',
62
- 99 * (10**99) + 99 => 'ninety nine duotrigintillion and ninety nine'
63
- }
44
+ 100 => 'one hundred',
45
+ 102 => 'one hundred and two',
46
+ 115 => 'one hundred and fifteen',
64
47
 
65
- describe Wintr::Number do
48
+ 5_000 => 'five thousand',
49
+ 5_001 => 'five thousand and one',
50
+ 5_010 => 'five thousand and ten',
51
+ 5_011 => 'five thousand and eleven',
52
+ 5_100 => 'five thousand one hundred',
53
+ 5_101 => 'five thousand one hundred and one',
54
+ 5_110 => 'five thousand one hundred and ten',
55
+ 5_111 => 'five thousand one hundred and eleven',
56
+
57
+ # 17_011_007 => 'seventeen million eleven thousand and seven',
58
+ # 17_011_075 => 'seventeen million eleven thousand and seven five',
59
+ # 17_011_705 => 'seventeen million eleven thousand seven hundred and five',
60
+
61
+ 56_945_781 => 'fifty six million nine hundred and forty five thousand seven hundred and eighty one',
62
+ 126_000_010 => 'one hundred and twenty six million and ten',
63
+ 999_999_999 => 'nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine',
64
+
65
+ 9 * (10**9) + 9 => 'nine billion and nine',
66
+ 12 * (10**12) + 12 => 'twelve trillion and twelve',
67
+ 15 * (10**15) + 15 => 'fifteen quadrillion and fifteen',
68
+ 18 * (10**18) + 18 => 'eighteen quintillion and eighteen',
69
+ 21 * (10**21) + 21 => 'twenty one sextillion and twenty one',
70
+ 24 * (10**24) + 24 => 'twenty four septillion and twenty four',
71
+ 27 * (10**27) + 27 => 'twenty seven octillion and twenty seven',
72
+ 30 * (10**30) + 30 => 'thirty nonillion and thirty',
73
+ 33 * (10**33) + 33 => 'thirty three decillion and thirty three',
74
+ 36 * (10**36) + 36 => 'thirty six undecillion and thirty six',
75
+ 39 * (10**39) + 39 => 'thirty nine duodecillion and thirty nine',
76
+ 42 * (10**42) + 42 => 'forty two tredecillion and forty two',
77
+ 45 * (10**45) + 45 => 'forty five quattuordecillion and forty five',
78
+ 48 * (10**48) + 48 => 'forty eight quindecillion and forty eight',
79
+ 51 * (10**51) + 51 => 'fifty one sexdecillion and fifty one',
80
+ 54 * (10**54) + 54 => 'fifty four septendecillion and fifty four',
81
+ 57 * (10**57) + 57 => 'fifty seven octodecillion and fifty seven',
82
+ 60 * (10**60) + 60 => 'sixty novemdecillion and sixty',
83
+ 63 * (10**63) + 63 => 'sixty three vigintillion and sixty three',
84
+ 66 * (10**66) + 66 => 'sixty six unvigintillion and sixty six',
85
+ 69 * (10**69) + 69 => 'sixty nine duovigintillion and sixty nine',
86
+ 72 * (10**72) + 72 => 'seventy two trevigintillion and seventy two',
87
+ 75 * (10**75) + 75 => 'seventy five quattuorvigintillion and seventy five',
88
+ 78 * (10**78) + 78 => 'seventy eight quinvigintillion and seventy eight',
89
+ 81 * (10**81) + 81 => 'eighty one sexvigintillion and eighty one',
90
+ 84 * (10**84) + 84 => 'eighty four septenvigintillion and eighty four',
91
+ 87 * (10**87) + 87 => 'eighty seven octovigintillion and eighty seven',
92
+ 90 * (10**90) + 90 => 'ninety novemvigintillion and ninety',
93
+ 93 * (10**93) + 93 => 'ninety three trigintillion and ninety three',
94
+ 96 * (10**96) + 96 => 'ninety six untrigintillion and ninety six',
95
+ 99 * (10**99) + 99 => 'ninety nine duotrigintillion and ninety nine'
96
+ }
97
+ end
98
+
99
+ RSpec.describe Wintr::Number do
66
100
  describe "#to_s" do
67
- CONVERSIONS.each do |integer, word|
68
- it "converts #{integer}" do
69
- Wintr::Number.new(integer).to_s.should == word
101
+ conversions.each do |integer, word|
102
+ it "converts #{integer} to #{word}" do
103
+ expect(Wintr::Number.new(integer).to_s).to eq(word)
70
104
  end
71
105
  end
72
106
  end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Wintr::TensUnits do
4
+ describe '#to_s' do
5
+ it 'converts 00 to empty string' do
6
+ expect(Wintr::TensUnits.new('0', '0').to_s).to eq('')
7
+ end
8
+
9
+ it 'converts 01' do
10
+ expect(Wintr::TensUnits.new('0', '1').to_s).to eq('one')
11
+ end
12
+
13
+ it 'converts 02' do
14
+ expect(Wintr::TensUnits.new('0', '2').to_s).to eq('two')
15
+ end
16
+
17
+ it 'converts 03' do
18
+ expect(Wintr::TensUnits.new('0', '3').to_s).to eq('three')
19
+ end
20
+
21
+ it 'converts 04' do
22
+ expect(Wintr::TensUnits.new('0', '4').to_s).to eq('four')
23
+ end
24
+
25
+ it 'converts 05' do
26
+ expect(Wintr::TensUnits.new('0', '5').to_s).to eq('five')
27
+ end
28
+
29
+ it 'converts 06' do
30
+ expect(Wintr::TensUnits.new('0', '6').to_s).to eq('six')
31
+ end
32
+
33
+ it 'converts 07' do
34
+ expect(Wintr::TensUnits.new('0', '7').to_s).to eq('seven')
35
+ end
36
+
37
+ it 'converts 08' do
38
+ expect(Wintr::TensUnits.new('0', '8').to_s).to eq('eight')
39
+ end
40
+
41
+ it 'converts 09' do
42
+ expect(Wintr::TensUnits.new('0', '9').to_s).to eq('nine')
43
+ end
44
+
45
+ it 'converts 10' do
46
+ expect(Wintr::TensUnits.new('1', '0').to_s).to eq('ten')
47
+ end
48
+
49
+ it 'converts 11' do
50
+ expect(Wintr::TensUnits.new('1', '1').to_s).to eq('eleven')
51
+ end
52
+
53
+ it 'converts 12' do
54
+ expect(Wintr::TensUnits.new('1', '2').to_s).to eq('twelve')
55
+ end
56
+
57
+ it 'converts 13' do
58
+ expect(Wintr::TensUnits.new('1', '3').to_s).to eq('thirteen')
59
+ end
60
+
61
+ it 'converts 14' do
62
+ expect(Wintr::TensUnits.new('1', '4').to_s).to eq('fourteen')
63
+ end
64
+
65
+ it 'converts 15' do
66
+ expect(Wintr::TensUnits.new('1', '5').to_s).to eq('fifteen')
67
+ end
68
+
69
+ it 'converts 16' do
70
+ expect(Wintr::TensUnits.new('1', '6').to_s).to eq('sixteen')
71
+ end
72
+
73
+ it 'converts 17' do
74
+ expect(Wintr::TensUnits.new('1', '7').to_s).to eq('seventeen')
75
+ end
76
+
77
+ it 'converts 18' do
78
+ expect(Wintr::TensUnits.new('1', '8').to_s).to eq('eighteen')
79
+ end
80
+
81
+ it 'converts 19' do
82
+ expect(Wintr::TensUnits.new('1', '9').to_s).to eq('nineteen')
83
+ end
84
+
85
+ it 'converts 20' do
86
+ expect(Wintr::TensUnits.new('2', '0').to_s).to eq('twenty')
87
+ end
88
+
89
+ it 'converts 21' do
90
+ expect(Wintr::TensUnits.new('2', '1').to_s).to eq('twenty one')
91
+ end
92
+
93
+ it 'converts 77' do
94
+ expect(Wintr::TensUnits.new('7', '7').to_s).to eq('seventy seven')
95
+ end
96
+
97
+ it 'converts 99' do
98
+ expect(Wintr::TensUnits.new('9', '9').to_s).to eq('ninety nine')
99
+ end
100
+ end
101
+ end
@@ -15,11 +15,11 @@ def single_digits
15
15
  }
16
16
  end
17
17
 
18
- describe Wintr::OneDigitGroup do
19
- describe "#to_s" do
18
+ RSpec.describe Wintr::Units do
19
+ describe '#to_s' do
20
20
  single_digits.each do |digit_string, word|
21
21
  it "converts #{digit_string} to #{word}" do
22
- Wintr::OneDigitGroup.new(digit_string).to_s.should == word
22
+ expect(Wintr::Units.new(digit_string).to_s).to eq(word)
23
23
  end
24
24
  end
25
25
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Wintr::WeightedDigitArray do
4
+ describe '#to_s' do
5
+ it 'converts 21 thousand' do
6
+ expect(Wintr::WeightedDigitArray.new(%w[2 1], 1).to_s).to eq('twenty one thousand')
7
+ end
8
+
9
+ it 'converts 123 thousand' do
10
+ expect(Wintr::WeightedDigitArray.new(%w[1 2 3], 1).to_s).to eq('one hundred and twenty three thousand')
11
+ end
12
+
13
+ it 'converts 103 million' do
14
+ expect(Wintr::WeightedDigitArray.new(%w[1 0 3], 2).to_s).to eq('one hundred and three million')
15
+ end
16
+
17
+ it 'converts 000 thousand to empty string' do
18
+ expect(Wintr::WeightedDigitArray.new(%w[0 0 0], 1).to_s).to eq('')
19
+ end
20
+
21
+ it 'converts 000 million to empty string' do
22
+ expect(Wintr::WeightedDigitArray.new(%w[0 0 0], 2).to_s).to eq('')
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wintr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - R Headley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-23 00:00:00.000000000 Z
11
+ date: 2015-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.8'
19
+ version: '3.2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.8'
26
+ version: '3.2'
27
27
  description: Convert integers into their word equivalents
28
28
  email:
29
29
  - headleyra@yahoo.com
@@ -34,24 +34,21 @@ extra_rdoc_files:
34
34
  files:
35
35
  - README.md
36
36
  - lib/wintr.rb
37
- - lib/wintr/digit_group.rb
37
+ - lib/wintr/constants.rb
38
+ - lib/wintr/digit_array.rb
39
+ - lib/wintr/hundreds_tens_units.rb
38
40
  - lib/wintr/number.rb
39
- - lib/wintr/one_digit_group.rb
40
- - lib/wintr/power_of_thousand.rb
41
- - lib/wintr/three_digit_group.rb
42
- - lib/wintr/two_digit_group.rb
41
+ - lib/wintr/tens_units.rb
42
+ - lib/wintr/units.rb
43
43
  - lib/wintr/version.rb
44
- - lib/wintr/weighted_digit_group.rb
45
- - lib/wintr/word_array.rb
44
+ - lib/wintr/weighted_digit_array.rb
46
45
  - spec/spec_helper.rb
47
- - spec/wintr/digit_group_spec.rb
46
+ - spec/wintr/digit_array_spec.rb
47
+ - spec/wintr/hundreds_tens_units_spec.rb
48
48
  - spec/wintr/number_spec.rb
49
- - spec/wintr/one_digit_group_spec.rb
50
- - spec/wintr/power_of_thousand_spec.rb
51
- - spec/wintr/three_digit_group_spec.rb
52
- - spec/wintr/two_digit_group_spec.rb
53
- - spec/wintr/weighted_digit_group_spec.rb
54
- - spec/wintr/word_array_spec.rb
49
+ - spec/wintr/tens_units_spec.rb
50
+ - spec/wintr/units_spec.rb
51
+ - spec/wintr/weighted_digit_array_spec.rb
55
52
  homepage: http://github.com/headleyra/wintr
56
53
  licenses:
57
54
  - MIT
@@ -77,12 +74,10 @@ signing_key:
77
74
  specification_version: 4
78
75
  summary: A Ruby integer to word converter
79
76
  test_files:
80
- - spec/wintr/two_digit_group_spec.rb
77
+ - spec/wintr/digit_array_spec.rb
78
+ - spec/wintr/tens_units_spec.rb
81
79
  - spec/wintr/number_spec.rb
82
- - spec/wintr/digit_group_spec.rb
83
- - spec/wintr/one_digit_group_spec.rb
84
- - spec/wintr/three_digit_group_spec.rb
85
- - spec/wintr/power_of_thousand_spec.rb
86
- - spec/wintr/word_array_spec.rb
87
- - spec/wintr/weighted_digit_group_spec.rb
80
+ - spec/wintr/hundreds_tens_units_spec.rb
81
+ - spec/wintr/weighted_digit_array_spec.rb
82
+ - spec/wintr/units_spec.rb
88
83
  - spec/spec_helper.rb
@@ -1,22 +0,0 @@
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_s
12
- case @digits.size
13
- when 1
14
- OneDigitGroup.new(@digits[0]).to_s
15
- when 2
16
- TwoDigitGroup.new(@digits[0], @digits[1]).to_s
17
- when 3
18
- ThreeDigitGroup.new(@digits[0], @digits[1], @digits[2]).to_s
19
- end
20
- end
21
- end
22
- end
@@ -1,24 +0,0 @@
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_s
21
- UNITS[@units]
22
- end
23
- end
24
- end
@@ -1,48 +0,0 @@
1
- module Wintr
2
- class PowerOfThousand
3
- POWER = {
4
- 1 => 'thousand',
5
- 2 => 'million',
6
- 3 => 'billion',
7
- 4 => 'trillion',
8
- 5 => 'quadrillion',
9
- 6 => 'quintillion',
10
- 7 => 'sextillion',
11
- 8 => 'septillion',
12
- 9 => 'octillion',
13
- 10 => 'nonillion',
14
- 11 => 'decillion',
15
- 12 => 'undecillion',
16
- 13 => 'duodecillion',
17
- 14 => 'tredecillion',
18
- 15 => 'quattuordecillion',
19
- 16 => 'quindecillion',
20
- 17 => 'sexdecillion',
21
- 18 => 'septendecillion',
22
- 19 => 'octodecillion',
23
- 20 => 'novemdecillion',
24
- 21 => 'vigintillion',
25
- 22 => 'unvigintillion',
26
- 23 => 'duovigintillion',
27
- 24 => 'trevigintillion',
28
- 25 => 'quattuorvigintillion',
29
- 26 => 'quinvigintillion',
30
- 27 => 'sexvigintillion',
31
- 28 => 'septenvigintillion',
32
- 29 => 'octovigintillion',
33
- 30 => 'novemvigintillion',
34
- 31 => 'trigintillion',
35
- 32 => 'untrigintillion',
36
- 33 => 'duotrigintillion'
37
- }
38
-
39
- def initialize(power)
40
- @power = power
41
- end
42
-
43
- def to_s
44
- POWER[@power] || ''
45
- end
46
- end
47
- end
48
-
@@ -1,21 +0,0 @@
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_s
8
- word_array = []
9
- if @hundreds == '0' && @tens == '0' && @units == '0'
10
- ''
11
- else
12
- word_array << OneDigitGroup.new(@hundreds).to_s
13
- word_array << 'hundred' if @hundreds != '0'
14
- word_array << 'and' unless @tens == '0' && @units == '0'
15
- word_array << TwoDigitGroup.new(@tens, @units).to_s
16
- WordArray.new(word_array).to_s
17
- end
18
- end
19
- end
20
- end
21
-
@@ -1,45 +0,0 @@
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_s
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_s
40
- WordArray.new(word_array).to_s
41
- end
42
- end
43
- end
44
- end
45
-
@@ -1,17 +0,0 @@
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_s
12
- base_digit_group_in_words = DigitGroup.new(@digit_group).to_s
13
- power_of_thousand_in_words = PowerOfThousand.new(@power_of_thousand).to_s if base_digit_group_in_words != ''
14
- WordArray.new([base_digit_group_in_words, power_of_thousand_in_words]).to_s
15
- end
16
- end
17
- end
@@ -1,12 +0,0 @@
1
- module Wintr
2
- class WordArray
3
- def initialize(array)
4
- @array = array
5
- end
6
-
7
- def to_s
8
- @array.join(' ').squeeze(' ').strip
9
- end
10
- end
11
- end
12
-
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Wintr
4
-
5
- describe DigitGroup do
6
- describe "#to_s" do
7
- it "should convert 0 to empty string" do
8
- DigitGroup.new(['0']).to_s.should == ''
9
- end
10
- it "should convert 9" do
11
- DigitGroup.new(['9']).to_s.should == 'nine'
12
- end
13
- it "should convert 10" do
14
- DigitGroup.new(%w[1 0]).to_s.should == 'ten'
15
- end
16
- it "should convert 19" do
17
- DigitGroup.new(%w[1 9]).to_s.should == 'nineteen'
18
- end
19
- it "should convert 20" do
20
- DigitGroup.new(%w[2 0]).to_s.should == 'twenty'
21
- end
22
- it "should convert 702" do
23
- DigitGroup.new(%w[7 0 2]).to_s.should == 'seven hundred and two'
24
- end
25
- end
26
- end
27
-
28
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Wintr
4
-
5
- describe PowerOfThousand do
6
- describe "#to_s" do
7
- it "should convert 0 to empty string" do
8
- PowerOfThousand.new(0).to_s.should == ''
9
- end
10
- it "should convert 1 into it's power-of-a-thousand equivalent" do
11
- PowerOfThousand.new(1).to_s.should == 'thousand'
12
- end
13
- it "should convert 2 into it's power-of-a-thousand equivalent" do
14
- PowerOfThousand.new(2).to_s.should == 'million'
15
- end
16
- it "should convert rubbish input into empty string" do
17
- PowerOfThousand.new('foo').to_s.should == ''
18
- end
19
- end
20
- end
21
-
22
- end
23
-
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Wintr
4
- describe ThreeDigitGroup do
5
- describe "#to_s" do
6
- it "should convert 000 to empty string" do
7
- ThreeDigitGroup.new('0', '0', '0').to_s.should == ''
8
- end
9
- it "should convert 101" do
10
- ThreeDigitGroup.new('1', '0', '1').to_s.should == 'one hundred and one'
11
- end
12
- it "should convert 900" do
13
- ThreeDigitGroup.new('9', '0', '0').to_s.should == 'nine hundred'
14
- end
15
- it "should convert 070" do
16
- ThreeDigitGroup.new('0', '7', '0').to_s.should == 'and seventy'
17
- end
18
- it "should convert 005" do
19
- ThreeDigitGroup.new('0', '0', '5').to_s.should == 'and five'
20
- end
21
- end
22
- end
23
- end
24
-
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Wintr
4
- describe TwoDigitGroup do
5
- describe "#to_s" do
6
- it "should convert 00 to empty string" do
7
- TwoDigitGroup.new('0', '0').to_s.should == ''
8
- end
9
- it "should convert 01 to one" do
10
- TwoDigitGroup.new('0', '1').to_s.should == 'one'
11
- end
12
- it "should convert 09 to nine" do
13
- TwoDigitGroup.new('0', '9').to_s.should == 'nine'
14
- end
15
- it "should convert 10" do
16
- TwoDigitGroup.new('1', '0').to_s.should == 'ten'
17
- end
18
- it "should convert 11" do
19
- TwoDigitGroup.new('1', '1').to_s.should == 'eleven'
20
- end
21
- it "should convert 12" do
22
- TwoDigitGroup.new('1', '2').to_s.should == 'twelve'
23
- end
24
- it "should convert 13" do
25
- TwoDigitGroup.new('1', '3').to_s.should == 'thirteen'
26
- end
27
- it "should convert 14" do
28
- TwoDigitGroup.new('1', '4').to_s.should == 'fourteen'
29
- end
30
- it "should convert 15" do
31
- TwoDigitGroup.new('1', '5').to_s.should == 'fifteen'
32
- end
33
- it "should convert 16" do
34
- TwoDigitGroup.new('1', '6').to_s.should == 'sixteen'
35
- end
36
- it "should convert 17" do
37
- TwoDigitGroup.new('1', '7').to_s.should == 'seventeen'
38
- end
39
- it "should convert 18" do
40
- TwoDigitGroup.new('1', '8').to_s.should == 'eighteen'
41
- end
42
- it "should convert 19" do
43
- TwoDigitGroup.new('1', '9').to_s.should == 'nineteen'
44
- end
45
- it "should convert 20" do
46
- TwoDigitGroup.new('2', '0').to_s.should == 'twenty'
47
- end
48
- it "should convert 21" do
49
- TwoDigitGroup.new('2', '1').to_s.should == 'twenty one'
50
- end
51
- it "should convert 77" do
52
- TwoDigitGroup.new('7', '7').to_s.should == 'seventy seven'
53
- end
54
- it "should convert 99" do
55
- TwoDigitGroup.new('9', '9').to_s.should == 'ninety nine'
56
- end
57
- end
58
- end
59
- end
60
-
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Wintr
4
- describe WeightedDigitGroup do
5
- describe "#to_s" do
6
- it "should should convert 23 thousand" do
7
- WeightedDigitGroup.new(%w[2 1], 1).to_s.should == 'twenty one thousand'
8
- end
9
- it "should should convert 123 thousand" do
10
- WeightedDigitGroup.new(%w[1 2 3], 1).to_s.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_s.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_s.should == ''
17
- end
18
- it "should should convert 000 million to empty string" do
19
- WeightedDigitGroup.new(%w[0 0 0], 2).to_s.should == ''
20
- end
21
- end
22
- end
23
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Wintr::WordArray do
4
- describe "#to_s" do
5
- it "joins it's array then squeezes spaces" do
6
- Wintr::WordArray.new([' foo ', ' bar ']).to_s.should == 'foo bar'
7
- end
8
- end
9
- end