english_number 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/{README.md → README.rdoc} +4 -8
- data/english_number.gemspec +4 -3
- data/lib/digits/digits.rb +14 -0
- data/lib/digits/five_digits.rb +38 -0
- data/lib/digits/four_digits.rb +39 -0
- data/lib/digits/three_digits.rb +2 -7
- data/lib/digits/two_digits.rb +1 -6
- data/lib/english_number.rb +1 -2
- data/lib/modules/compute.rb +3 -1
- data/lib/modules/languages/english_numbers.yml +1 -1
- data/lib/modules/translate.rb +1 -1
- data/spec/unit_tests/five_digits_spec.rb +68 -0
- data/spec/unit_tests/four_digits_spec.rb +68 -0
- data/spec/unit_tests/three_digits_spec.rb +2 -1
- data/spec/unit_tests/two_digits_spec.rb +2 -1
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdf0d7574ca5907f46a01d20ff33b5dcffc4052e
|
4
|
+
data.tar.gz: e1e587b04a8245be90a070af212ed889399259bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b3d9cf1988aa803f1063ea1e4eeeed11481a92ef67975b76e2fddcdf78eeca882a584c6aed94854993f94e6c73b3b5e0d0f84f844324f20ac8392cd440cc6b6
|
7
|
+
data.tar.gz: 1ad634b277764c301d46ee8a515ba3ed062c156847a62b5b1a5a8163205fb53b671bfa9e34b1bd2e45233fca36ac39c2de29ffe8d03ea0fe0f0e99fce7f5f5ac
|
data/.travis.yml
ADDED
data/{README.md → README.rdoc}
RENAMED
@@ -1,13 +1,9 @@
|
|
1
1
|
== english_number
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
<!--{<img src="https://secure.travis-ci.org/kslazarev/numbers_and_words.svg?branch=master" />}[http://travis-ci.org/kslazarev/numbers_and_words]-->
|
8
|
-
<!--{<img src="https://coveralls.io/repos/kslazarev/numbers_and_words/badge.svg?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/kslazarev/numbers_and_words]-->
|
9
|
-
|
10
|
-
Convert numbers to words in English. Limit of digits handled can be set. For now works for up to 3 digits. Additionally EnglishNumber objects can be added, subtracted, multiplied and divided.
|
3
|
+
{<img src="https://travis-ci.org/bryszard/english_number.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/bryszard/english_number]
|
4
|
+
{<img src="https://codeclimate.com/github/bryszard/english_number/badges/gpa.svg" />}[https://codeclimate.com/github/bryszard/english_number]
|
5
|
+
|
6
|
+
Convert numbers to words in English. Limit of digits handled can be set. For now works for up to 5 digits. Additionally EnglishNumber objects can be added, subtracted, multiplied and divided.
|
11
7
|
|
12
8
|
|
13
9
|
== Supported Languages
|
data/english_number.gemspec
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'english_number'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.4'
|
4
4
|
s.date = '2016-05-15'
|
5
5
|
s.summary = "Converting English numbers to strings"
|
6
6
|
s.description = "Example: EnglishNumber.new(63).in_english # => 'sixty-three'.\n
|
7
7
|
EnglishNumber objects can be added, subtracted, multiplied, divided.\n
|
8
|
-
For now gem is deliberately limited to numbers maximum
|
8
|
+
For now gem is deliberately limited to numbers maximum five digits long.\n
|
9
|
+
Lower limit might be set manually (see the source code)"
|
9
10
|
|
10
11
|
s.authors = ["Piotr Brych"]
|
11
12
|
s.email = 'pbrych@gmail.com'
|
12
13
|
s.files = `git ls-files`.split($/)
|
13
|
-
s.homepage = '
|
14
|
+
s.homepage = 'https://github.com/bryszard/english_number'
|
14
15
|
|
15
16
|
s.add_dependency 'activesupport', '~> 4.2.6'
|
16
17
|
s.add_development_dependency 'rspec', '~> 3.4.0'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class FiveDigits < Digits
|
2
|
+
|
3
|
+
def self.run(num)
|
4
|
+
new(num).in_words
|
5
|
+
end
|
6
|
+
|
7
|
+
def in_words
|
8
|
+
num_mod(10000).zero? ? simple_ten_thous : complex_ten_thous
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
attr_reader :num
|
14
|
+
|
15
|
+
def initialize(num)
|
16
|
+
@num = num
|
17
|
+
end
|
18
|
+
|
19
|
+
def first_two_digits
|
20
|
+
TwoDigits.new(in_array[0,2].join.to_i).in_english
|
21
|
+
end
|
22
|
+
|
23
|
+
def rest_digits
|
24
|
+
in_array[-3..-1].join.to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
def rest
|
28
|
+
rest_digits < 10 ? single_digits(rest_digits) : Digits.new(rest_digits).in_english
|
29
|
+
end
|
30
|
+
|
31
|
+
def simple_ten_thous
|
32
|
+
[first_two_digits, prefix(power_of_ten)].join('-')
|
33
|
+
end
|
34
|
+
|
35
|
+
def complex_ten_thous
|
36
|
+
[simple_ten_thous, rest].join('-')
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class FourDigits < Digits
|
2
|
+
|
3
|
+
def self.run(num)
|
4
|
+
new(num).in_words
|
5
|
+
end
|
6
|
+
|
7
|
+
def in_words
|
8
|
+
num_mod(1000).zero? ? simple_thousends : complex_thousends
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
attr_reader :num
|
14
|
+
|
15
|
+
def initialize(num)
|
16
|
+
@num = num
|
17
|
+
end
|
18
|
+
|
19
|
+
def first_digit
|
20
|
+
single_digits(in_array[0].to_i)
|
21
|
+
end
|
22
|
+
|
23
|
+
def rest_digits
|
24
|
+
in_array[1..-1].join.to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
def rest
|
28
|
+
rest_digits < 10 ? single_digits(rest_digits) : Digits.new(rest_digits).in_english
|
29
|
+
end
|
30
|
+
|
31
|
+
def simple_thousends
|
32
|
+
[first_digit, prefix(power_of_ten)].join('-')
|
33
|
+
end
|
34
|
+
|
35
|
+
def complex_thousends
|
36
|
+
[first_digit, prefix(power_of_ten), rest].join('-')
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/lib/digits/three_digits.rb
CHANGED
@@ -1,9 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'modules/translate'
|
3
|
-
|
4
|
-
class ThreeDigits
|
5
|
-
include Compute
|
6
|
-
include Translate
|
1
|
+
class ThreeDigits < Digits
|
7
2
|
|
8
3
|
def self.run(num)
|
9
4
|
new(num).in_words
|
@@ -30,7 +25,7 @@ class ThreeDigits
|
|
30
25
|
end
|
31
26
|
|
32
27
|
def rest
|
33
|
-
rest_digits < 10 ? single_digits(rest_digits) :
|
28
|
+
rest_digits < 10 ? single_digits(rest_digits) : Digits.new(rest_digits).in_english
|
34
29
|
end
|
35
30
|
|
36
31
|
def simple_hundreds
|
data/lib/digits/two_digits.rb
CHANGED
data/lib/english_number.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
require 'active_support/inflector'
|
3
3
|
|
4
|
-
require 'digits/two_digits'
|
5
|
-
require 'digits/three_digits'
|
6
4
|
require 'modules/compute'
|
7
5
|
require 'modules/translate'
|
6
|
+
Dir["./lib/digits/*.rb"].each {|file| require file }
|
8
7
|
|
9
8
|
class EnglishNumber
|
10
9
|
include Compute
|
data/lib/modules/compute.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
ones: [zero, one, two, three, four, five, six, seven, eight, nine]
|
2
2
|
tenss: [ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen]
|
3
3
|
tens: [zero, ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety]
|
4
|
-
powers: [one, ten, hundred, thousand,
|
4
|
+
powers: [one, ten, hundred, thousand, thousand, thousand, million, million, million, billion]
|
data/lib/modules/translate.rb
CHANGED
@@ -0,0 +1,68 @@
|
|
1
|
+
RSpec.describe FiveDigits do
|
2
|
+
subject(:five_dig) { described_class }
|
3
|
+
|
4
|
+
context 'modules' do
|
5
|
+
|
6
|
+
it 'includes Compute module' do
|
7
|
+
expect(five_dig.included_modules).to include(Compute)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'includes Translate module' do
|
11
|
+
expect(five_dig.included_modules).to include(Translate)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
context '#run' do
|
17
|
+
let(:limit) { 10**Translate::DIGITS_LIMIT }
|
18
|
+
let(:num) { rand(limit/10..limit) }
|
19
|
+
let(:inst) { instance_double('FiveDigits') }
|
20
|
+
|
21
|
+
it 'responds to class method #run(num)' do
|
22
|
+
expect(five_dig).to respond_to(:run).with(1).argument
|
23
|
+
five_dig.run(num)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'when #run used it it invokes #in_words on new instance' do
|
27
|
+
allow(five_dig).to receive(:new).with(num) { inst }
|
28
|
+
expect(inst).to receive(:in_words)
|
29
|
+
five_dig.run(num)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
context '#in_words', if: Translate::DIGITS_LIMIT > 4 do
|
35
|
+
|
36
|
+
context 'works for five-digit numbers:' do
|
37
|
+
it '10000' do
|
38
|
+
expect(five_dig.new(10000).in_words).to eq('ten-thousand')
|
39
|
+
end
|
40
|
+
it '10010' do
|
41
|
+
expect(five_dig.new(10010).in_words).to eq('ten-thousand-ten')
|
42
|
+
end
|
43
|
+
it '15210' do
|
44
|
+
expect(five_dig.new(15210).in_words).to eq('fifteen-thousand-two-hundred-ten')
|
45
|
+
end
|
46
|
+
it '27378' do
|
47
|
+
expect(five_dig.new(27378).in_words).to eq('twenty-seven-thousand-three-hundred-seventy-eight')
|
48
|
+
end
|
49
|
+
it '30000' do
|
50
|
+
expect(five_dig.new(30000).in_words).to eq('thirty-thousand')
|
51
|
+
end
|
52
|
+
it '31500' do
|
53
|
+
expect(five_dig.new(31500).in_words).to eq('thirty-one-thousand-five-hundred')
|
54
|
+
end
|
55
|
+
it '60000' do
|
56
|
+
expect(five_dig.new(60000).in_words).to eq('sixty-thousand')
|
57
|
+
end
|
58
|
+
it '87324' do
|
59
|
+
expect(five_dig.new(87324).in_words).to eq('eighty-seven-thousand-three-hundred-twenty-four')
|
60
|
+
end
|
61
|
+
it '99999' do
|
62
|
+
expect(five_dig.new(99999).in_words).to eq('ninety-nine-thousand-nine-hundred-ninety-nine')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
RSpec.describe FourDigits do
|
2
|
+
subject(:four_dig) { described_class }
|
3
|
+
|
4
|
+
context 'modules' do
|
5
|
+
|
6
|
+
it 'includes Compute module' do
|
7
|
+
expect(four_dig.included_modules).to include(Compute)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'includes Translate module' do
|
11
|
+
expect(four_dig.included_modules).to include(Translate)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
context '#run' do
|
17
|
+
let(:limit) { 10**Translate::DIGITS_LIMIT }
|
18
|
+
let(:num) { rand(limit/10..limit) }
|
19
|
+
let(:inst) { instance_double('FourDigits') }
|
20
|
+
|
21
|
+
it 'responds to class method #run(num)' do
|
22
|
+
expect(four_dig).to respond_to(:run).with(1).argument
|
23
|
+
four_dig.run(num)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'when #run used it it invokes #in_words on new instance' do
|
27
|
+
allow(four_dig).to receive(:new).with(num) { inst }
|
28
|
+
expect(inst).to receive(:in_words)
|
29
|
+
four_dig.run(num)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
context '#in_words', if: Translate::DIGITS_LIMIT > 3 do
|
35
|
+
|
36
|
+
context 'works for four-digit numbers:' do
|
37
|
+
it '1000' do
|
38
|
+
expect(four_dig.new(1000).in_words).to eq('one-thousand')
|
39
|
+
end
|
40
|
+
it '1010' do
|
41
|
+
expect(four_dig.new(1010).in_words).to eq('one-thousand-ten')
|
42
|
+
end
|
43
|
+
it '1210' do
|
44
|
+
expect(four_dig.new(1210).in_words).to eq('one-thousand-two-hundred-ten')
|
45
|
+
end
|
46
|
+
it '2378' do
|
47
|
+
expect(four_dig.new(2378).in_words).to eq('two-thousand-three-hundred-seventy-eight')
|
48
|
+
end
|
49
|
+
it '3000' do
|
50
|
+
expect(four_dig.new(3000).in_words).to eq('three-thousand')
|
51
|
+
end
|
52
|
+
it '3500' do
|
53
|
+
expect(four_dig.new(3500).in_words).to eq('three-thousand-five-hundred')
|
54
|
+
end
|
55
|
+
it '6000' do
|
56
|
+
expect(four_dig.new(6001).in_words).to eq('six-thousand-one')
|
57
|
+
end
|
58
|
+
it '8732' do
|
59
|
+
expect(four_dig.new(8732).in_words).to eq('eight-thousand-seven-hundred-thirty-two')
|
60
|
+
end
|
61
|
+
it '9999' do
|
62
|
+
expect(four_dig.new(9999).in_words).to eq('nine-thousand-nine-hundred-ninety-nine')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -14,7 +14,8 @@ RSpec.describe ThreeDigits do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
context '#run' do
|
17
|
-
let(:
|
17
|
+
let(:limit) { 10**Translate::DIGITS_LIMIT }
|
18
|
+
let(:num) { rand(limit/10..limit) }
|
18
19
|
let(:inst) { instance_double('ThreeDigits') }
|
19
20
|
|
20
21
|
it 'responds to class method #run(num)' do
|
@@ -14,7 +14,8 @@ RSpec.describe TwoDigits do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
context '#run' do
|
17
|
-
let(:
|
17
|
+
let(:limit) { 10**Translate::DIGITS_LIMIT }
|
18
|
+
let(:num) { rand(limit/10..limit) }
|
18
19
|
let(:inst) { instance_double('TwoDigits') }
|
19
20
|
|
20
21
|
it 'responds to class method #run(num)' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: english_number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Brych
|
@@ -54,7 +54,8 @@ dependencies:
|
|
54
54
|
version: 0.11.2
|
55
55
|
description: "Example: EnglishNumber.new(63).in_english # => 'sixty-three'.\n \n EnglishNumber
|
56
56
|
objects can be added, subtracted, multiplied, divided.\n\n For
|
57
|
-
now gem is deliberately limited to numbers maximum
|
57
|
+
now gem is deliberately limited to numbers maximum five digits long.\n\n Lower
|
58
|
+
limit might be set manually (see the source code)"
|
58
59
|
email: pbrych@gmail.com
|
59
60
|
executables: []
|
60
61
|
extensions: []
|
@@ -62,10 +63,14 @@ extra_rdoc_files: []
|
|
62
63
|
files:
|
63
64
|
- ".gitignore"
|
64
65
|
- ".rspec"
|
66
|
+
- ".travis.yml"
|
65
67
|
- CHANGELOG.md
|
66
68
|
- Gemfile
|
67
|
-
- README.
|
69
|
+
- README.rdoc
|
68
70
|
- english_number.gemspec
|
71
|
+
- lib/digits/digits.rb
|
72
|
+
- lib/digits/five_digits.rb
|
73
|
+
- lib/digits/four_digits.rb
|
69
74
|
- lib/digits/three_digits.rb
|
70
75
|
- lib/digits/two_digits.rb
|
71
76
|
- lib/english_number.rb
|
@@ -76,10 +81,12 @@ files:
|
|
76
81
|
- spec/spec_helper.rb
|
77
82
|
- spec/unit_tests/compute_spec.rb
|
78
83
|
- spec/unit_tests/english_number_unit_spec.rb
|
84
|
+
- spec/unit_tests/five_digits_spec.rb
|
85
|
+
- spec/unit_tests/four_digits_spec.rb
|
79
86
|
- spec/unit_tests/three_digits_spec.rb
|
80
87
|
- spec/unit_tests/translate_spec.rb
|
81
88
|
- spec/unit_tests/two_digits_spec.rb
|
82
|
-
homepage:
|
89
|
+
homepage: https://github.com/bryszard/english_number
|
83
90
|
licenses:
|
84
91
|
- MIT
|
85
92
|
metadata: {}
|