castwide_numbers_in_words 1.0.2

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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +14 -0
  3. data/.gitignore +7 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +58 -0
  6. data/.travis.yml +17 -0
  7. data/Gemfile +8 -0
  8. data/Gemfile.lock +74 -0
  9. data/LICENSE.txt +24 -0
  10. data/NUMBERS_IN_WORDS.txt +22 -0
  11. data/README.md +47 -0
  12. data/Rakefile +3 -0
  13. data/bin/spec +2 -0
  14. data/castwide_numbers_in_words.gemspec +24 -0
  15. data/lib/numbers_in_words/duck_punch.rb +23 -0
  16. data/lib/numbers_in_words/exceptional_numbers.rb +115 -0
  17. data/lib/numbers_in_words/fraction.rb +136 -0
  18. data/lib/numbers_in_words/number_group.rb +64 -0
  19. data/lib/numbers_in_words/parsing/fraction_parsing.rb +31 -0
  20. data/lib/numbers_in_words/parsing/number_parser.rb +98 -0
  21. data/lib/numbers_in_words/parsing/pair_parsing.rb +64 -0
  22. data/lib/numbers_in_words/parsing/parse_fractions.rb +45 -0
  23. data/lib/numbers_in_words/parsing/parse_individual_number.rb +68 -0
  24. data/lib/numbers_in_words/parsing/parse_status.rb +17 -0
  25. data/lib/numbers_in_words/parsing/special.rb +67 -0
  26. data/lib/numbers_in_words/parsing/to_number.rb +77 -0
  27. data/lib/numbers_in_words/powers_of_ten.rb +49 -0
  28. data/lib/numbers_in_words/to_word.rb +84 -0
  29. data/lib/numbers_in_words/version.rb +5 -0
  30. data/lib/numbers_in_words/writer.rb +69 -0
  31. data/lib/numbers_in_words.rb +58 -0
  32. data/spec/exceptional_numbers_spec.rb +26 -0
  33. data/spec/fraction_spec.rb +152 -0
  34. data/spec/fractions_spec.rb +31 -0
  35. data/spec/non_monkey_patch_spec.rb +51 -0
  36. data/spec/number_group_spec.rb +17 -0
  37. data/spec/number_parser_spec.rb +31 -0
  38. data/spec/numbers_in_words_spec.rb +99 -0
  39. data/spec/numerical_strings_spec.rb +35 -0
  40. data/spec/spec_helper.rb +26 -0
  41. data/spec/to_word_spec.rb +18 -0
  42. data/spec/words_in_numbers_spec.rb +152 -0
  43. data/spec/writer_spec.rb +26 -0
  44. data/spec/years_spec.rb +27 -0
  45. metadata +124 -0
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './spec/spec_helper'
4
+
5
+ describe NumbersInWords::StringExtension do # rubocop: disable Metrics/BlockLength
6
+ it 'should do the digits 0-10' do
7
+ expect('zero'.in_numbers).to eq(0)
8
+ expect('one'.in_numbers).to eq(1)
9
+ expect('two'.in_numbers).to eq(2)
10
+ expect('three'.in_numbers).to eq(3)
11
+ expect('four'.in_numbers).to eq(4)
12
+ expect('five'.in_numbers).to eq(5)
13
+ expect('six'.in_numbers).to eq(6)
14
+ expect('seven'.in_numbers).to eq(7)
15
+ expect('eight'.in_numbers).to eq(8)
16
+ expect('nine'.in_numbers).to eq(9)
17
+ end
18
+
19
+ it 'should handle numbers for which there is one word' do
20
+ expect('ten'.in_numbers).to eq(10)
21
+ expect('eleven'.in_numbers).to eq(11)
22
+ expect('twelve'.in_numbers).to eq(12)
23
+ expect('thirteen'.in_numbers).to eq(13)
24
+ expect('fourteen'.in_numbers).to eq(14)
25
+ expect('fifteen'.in_numbers).to eq(15)
26
+ expect('sixteen'.in_numbers).to eq(16)
27
+ expect('seventeen'.in_numbers).to eq(17)
28
+ expect('eighteen'.in_numbers).to eq(18)
29
+ expect('nineteen'.in_numbers).to eq(19)
30
+ expect('twenty'.in_numbers).to eq(20)
31
+ end
32
+
33
+ it 'should handle two word numbers up to 100' do
34
+ expect('twenty-one'.in_numbers).to eq(21)
35
+ expect('twenty-two'.in_numbers).to eq(22)
36
+ expect('twenty-three'.in_numbers).to eq(23)
37
+ expect('twenty-four'.in_numbers).to eq(24)
38
+ expect('twenty-five'.in_numbers).to eq(25)
39
+ expect('twenty-six'.in_numbers).to eq(26)
40
+ expect('twenty-seven'.in_numbers).to eq(27)
41
+ expect('twenty-eight'.in_numbers).to eq(28)
42
+ expect('seventy-six'.in_numbers).to eq(76)
43
+ expect('ninety-nine'.in_numbers).to eq(99)
44
+ end
45
+
46
+ it 'should handle hundreds' do
47
+ expect('one hundred'.in_numbers).to eq(100)
48
+ expect('two hundred'.in_numbers).to eq(200)
49
+ expect('three hundred'.in_numbers).to eq(300)
50
+ expect('nine hundred'.in_numbers).to eq(900)
51
+ expect('one hundred and seventy six'.in_numbers).to eq(176)
52
+ expect('one hundred and seventy nine'.in_numbers).to eq(179)
53
+ expect('nine hundred and ninety nine'.in_numbers).to eq(999)
54
+ end
55
+
56
+ it 'should handle unusual hundreds' do
57
+ expect('eleven hundred'.in_numbers).to eq(1100)
58
+ expect('twelve hundred'.in_numbers).to eq(1200)
59
+ expect('thirteen hundred'.in_numbers).to eq(1300)
60
+ expect('fifteen hundred'.in_numbers).to eq(1500)
61
+ expect('nineteen hundred'.in_numbers).to eq(1900)
62
+ end
63
+ it 'should handle thousands' do
64
+ expect('two thousand and one'.in_numbers).to eq(2001)
65
+ expect('one thousand'.in_numbers).to eq(1000)
66
+ expect('two thousand'.in_numbers).to eq(2000)
67
+ expect('three thousand'.in_numbers).to eq(3000)
68
+ expect('nine thousand'.in_numbers).to eq(9000)
69
+ expect('nine thousand two hundred'.in_numbers).to eq(9200)
70
+ expect('nine thousand two hundred and seven'.in_numbers).to eq(9207)
71
+ expect('nine thousand two hundred and ninety seven'.in_numbers).to eq(9297)
72
+ end
73
+
74
+ it 'handles numbers that begin with a instead of one', :aggregate_failures do
75
+ expect('a thousand six hundred'.in_numbers).to eq(1600)
76
+ expect('a thousand six hundred and fifty-five'.in_numbers).to eq(1655)
77
+ end
78
+
79
+ it 'should handle larger numbers' do
80
+ expect('one million'.in_numbers).to eq(1_000_000)
81
+ expect('two googol five billion and seventy six'.in_numbers).to eq(2 * 10**100 + 5 * 10**9 + 76)
82
+ expect('thirty seven million'.in_numbers).to eq(37 * 10**6)
83
+ expect('twenty six googol'.in_numbers).to eq(26 * 10**100)
84
+ end
85
+
86
+ it 'should handle numbers in hundreds of thousands etc' do
87
+ expect('nine hundred thousand'.in_numbers).to eq(900_000)
88
+ expect('three hundred and fifty seven thousand'.in_numbers).to eq(357_000)
89
+ expect('five million three hundred and fifty seven thousand'.in_numbers).to eq(5_357_000)
90
+ expect('nine hundred and ninety nine trillion'.in_numbers).to eq(999 * 10**12)
91
+ end
92
+ it 'should handle negative numbers' do
93
+ expect('minus one'.in_numbers).to eq(-1)
94
+ expect('minus two googol'.in_numbers).to eq(-2 * 10**100)
95
+ expect('minus nine hundred and ninety nine trillion'.in_numbers).to eq(-999 * 10**12)
96
+ end
97
+
98
+ it 'should ignore punctuation and capitalisation' do
99
+ expect('Minus one'.in_numbers).to eq(-1)
100
+ expect('FIVE Million, three hundred and fifty-seVen Thousand'.in_numbers).to eq(5_357_000)
101
+ expect("FIVE,,./';';';[] Million, three hundred and fifty-seVen Thousand".in_numbers).to eq(5_357_000)
102
+ end
103
+
104
+ it 'should handle decimal points' do # rubocop: disable Metrics/BlockLength
105
+ expect('one point one'.in_numbers).to eq(1.1)
106
+
107
+ expect('zero point seven six five three four'.in_numbers).to eq(0.76534)
108
+ expect('one trillion point six'.in_numbers).to eq(10**12 + 0.6)
109
+
110
+ long_number = <<~NUMBER
111
+ nine duotrigintillion seven hundred and seventy seven untrigintillion
112
+ fifty nine trigintillion one hundred and sixty novemvigintillion
113
+ eight hundred and six octovigintillion seven hundred and thirty six
114
+ septenvigintillion four hundred and seventy one sexvigintillion
115
+ nine hundred and seventy quinvigintillion six hundred and thirty two
116
+ quattuorvigintillion eight hundred and twenty seven trevigintillion
117
+ eight hundred and thirty six duovigintillion nine hundred and fifty
118
+ two unvigintillion seven hundred and ten vigintillion eight hundred and one
119
+ novemdecillion nine hundred and forty eight octodecillion seven hundred
120
+ and five septendecillion six hundred and eighty three sexdecillion
121
+ one hundred and six quindecillion seven hundred and seven quattuordecillion
122
+ seven hundred and fifty seven tredecillion four hundred and twenty six
123
+ duodecillion seven hundred and ninety five undecillion seven hundred
124
+ and forty six decillion eight hundred and thirteen nonillion one hundred
125
+ and twenty seven octillion four hundred and sixty five septillion two
126
+ hundred and thirty seven sextillion one hundred and thirty nine
127
+ quintillion one hundred and fifty three quadrillion forty six
128
+ trillion seven hundred and fifty two billion eight hundred and three
129
+ million ninety three thousand seven hundred and ninety one point
130
+ eight nine five six four three two one eight nine five six seven eight
131
+ NUMBER
132
+
133
+ # rubocop: disable Metrics/LineLength
134
+ expect(long_number.in_numbers).to eq(
135
+ 9_777_059_160_806_736_471_970_632_827_836_952_710_801_948_705_683_106_707_757_426_795_746_813_127_465_237_139_153_046_752_803_093_791.89564321895678
136
+ )
137
+ # rubocop: enable Metrics/LineLength
138
+
139
+ expect('seventy five point eight four three two seven six nine four five one eight'
140
+ .in_numbers).to eq(75.84327694518)
141
+ end
142
+
143
+ it 'should handle years notation' do
144
+ expect('fifteen sixteen'.in_numbers).to eq(1516)
145
+ expect('eighty five sixteen'.in_numbers).to eq(8516)
146
+ expect('nineteen ninety six'.in_numbers).to eq(1996)
147
+ expect('forty nine ninety eight forty seven seventy nine'.in_numbers).to eq(49_984_779)
148
+ expect('fifteen sixteen'.in_numbers).to eq(1516)
149
+ expect('fifteen sixteen seven'.in_numbers).to eq(15_167)
150
+ expect('fifteen sixteen seventeen'.in_numbers).to eq(151_617)
151
+ end
152
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe NumbersInWords::Writer do
4
+ let(:number) { 2_111 }
5
+ let(:writer) { described_class.new(number) }
6
+
7
+ it 'should display numbers grouped' do
8
+ count = 0
9
+
10
+ writer.group_words(3) do |power, name, digits|
11
+ case count
12
+ when 0
13
+ expect(power).to eq(3)
14
+ expect(name).to eq('thousand')
15
+ expect(digits).to eq(2)
16
+ when 1
17
+ expect(power).to eq(0)
18
+ expect(name).to eq('one')
19
+ expect(digits).to eq(111)
20
+ end
21
+ count += 1
22
+ end
23
+
24
+ expect(count).to eq 2
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './spec/spec_helper'
4
+
5
+ describe NumbersInWords::NumericExtension do
6
+ YEARS = {
7
+ 'thirteen hundred' => 13_00,
8
+ 'twenty-two hundred' => 22_00,
9
+ 'twenty-two hundred and five' => 22_05,
10
+ 'fifteen sixteen seventeen' => 151_617,
11
+ 'forty-nine ninety-eight forty-seven seventy-nine' => 49_984_779,
12
+ 'one fifty' => 150,
13
+ 'two fifty' => 250,
14
+ 'three fifty' => 350,
15
+ 'one point fifty-six fifty-seven' => 1.5657,
16
+ 'one three forty-seven' => 1347,
17
+ 'one three five-point forty-seven' => 135.47,
18
+ 'one ten sixty-three' => 11_063,
19
+ 'one nineteen ten oh five' => 1_191_005
20
+ }.freeze
21
+
22
+ YEARS.each do |k, v|
23
+ it do
24
+ expect(k.in_numbers).to eql(v)
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: castwide_numbers_in_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Fred Snyder
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rspec
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 3.4.0
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 3.4.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: rubocop
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: convert written numbers into Integers and vice-versa
41
+ email:
42
+ - castwide@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".codeclimate.yml"
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - NUMBERS_IN_WORDS.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/spec
59
+ - castwide_numbers_in_words.gemspec
60
+ - lib/numbers_in_words.rb
61
+ - lib/numbers_in_words/duck_punch.rb
62
+ - lib/numbers_in_words/exceptional_numbers.rb
63
+ - lib/numbers_in_words/fraction.rb
64
+ - lib/numbers_in_words/number_group.rb
65
+ - lib/numbers_in_words/parsing/fraction_parsing.rb
66
+ - lib/numbers_in_words/parsing/number_parser.rb
67
+ - lib/numbers_in_words/parsing/pair_parsing.rb
68
+ - lib/numbers_in_words/parsing/parse_fractions.rb
69
+ - lib/numbers_in_words/parsing/parse_individual_number.rb
70
+ - lib/numbers_in_words/parsing/parse_status.rb
71
+ - lib/numbers_in_words/parsing/special.rb
72
+ - lib/numbers_in_words/parsing/to_number.rb
73
+ - lib/numbers_in_words/powers_of_ten.rb
74
+ - lib/numbers_in_words/to_word.rb
75
+ - lib/numbers_in_words/version.rb
76
+ - lib/numbers_in_words/writer.rb
77
+ - spec/exceptional_numbers_spec.rb
78
+ - spec/fraction_spec.rb
79
+ - spec/fractions_spec.rb
80
+ - spec/non_monkey_patch_spec.rb
81
+ - spec/number_group_spec.rb
82
+ - spec/number_parser_spec.rb
83
+ - spec/numbers_in_words_spec.rb
84
+ - spec/numerical_strings_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/to_word_spec.rb
87
+ - spec/words_in_numbers_spec.rb
88
+ - spec/writer_spec.rb
89
+ - spec/years_spec.rb
90
+ homepage: http://github.com/castwide/castwide_numbers_in_words
91
+ licenses: []
92
+ metadata: {}
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.6.7
108
+ specification_version: 4
109
+ summary: 'Example: NumbersInWords.in_words(123) # => "one hundred and twenty three",
110
+ NumbersInWords.in_numbers("seventy-five point eight") # = > 75.8'
111
+ test_files:
112
+ - spec/exceptional_numbers_spec.rb
113
+ - spec/fraction_spec.rb
114
+ - spec/fractions_spec.rb
115
+ - spec/non_monkey_patch_spec.rb
116
+ - spec/number_group_spec.rb
117
+ - spec/number_parser_spec.rb
118
+ - spec/numbers_in_words_spec.rb
119
+ - spec/numerical_strings_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/to_word_spec.rb
122
+ - spec/words_in_numbers_spec.rb
123
+ - spec/writer_spec.rb
124
+ - spec/years_spec.rb