numbers_in_words 0.3.0 → 1.0.0
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 +5 -5
- data/.codeclimate.yml +2 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.rubocop.yml +35 -1148
- data/.travis.yml +14 -4
- data/Gemfile +4 -1
- data/README.md +6 -43
- data/Rakefile +3 -1
- data/bin/spec +0 -0
- data/lib/numbers_in_words.rb +44 -19
- data/lib/numbers_in_words/duck_punch.rb +12 -8
- data/lib/numbers_in_words/exceptional_numbers.rb +115 -0
- data/lib/numbers_in_words/fraction.rb +136 -0
- data/lib/numbers_in_words/number_group.rb +34 -25
- data/lib/numbers_in_words/parsing/fraction_parsing.rb +34 -0
- data/lib/numbers_in_words/parsing/number_parser.rb +98 -0
- data/lib/numbers_in_words/parsing/pair_parsing.rb +64 -0
- data/lib/numbers_in_words/parsing/parse_fractions.rb +45 -0
- data/lib/numbers_in_words/parsing/parse_individual_number.rb +68 -0
- data/lib/numbers_in_words/parsing/parse_status.rb +17 -0
- data/lib/numbers_in_words/parsing/special.rb +67 -0
- data/lib/numbers_in_words/parsing/to_number.rb +77 -0
- data/lib/numbers_in_words/powers_of_ten.rb +49 -0
- data/lib/numbers_in_words/to_word.rb +78 -13
- data/lib/numbers_in_words/version.rb +3 -1
- data/lib/numbers_in_words/writer.rb +69 -0
- data/numbers_in_words.gemspec +14 -13
- data/spec/exceptional_numbers_spec.rb +26 -0
- data/spec/fraction_spec.rb +152 -0
- data/spec/fractions_spec.rb +31 -0
- data/spec/non_monkey_patch_spec.rb +40 -15
- data/spec/number_group_spec.rb +12 -12
- data/spec/number_parser_spec.rb +31 -0
- data/spec/numbers_in_words_spec.rb +63 -70
- data/spec/numerical_strings_spec.rb +35 -0
- data/spec/spec_helper.rb +24 -4
- data/spec/to_word_spec.rb +18 -0
- data/spec/words_in_numbers_spec.rb +133 -116
- data/spec/writer_spec.rb +26 -0
- data/spec/years_spec.rb +27 -0
- metadata +49 -27
- data/lib/numbers_in_words/english/constants.rb +0 -93
- data/lib/numbers_in_words/english/language_writer_english.rb +0 -109
- data/lib/numbers_in_words/language_writer.rb +0 -31
- data/lib/numbers_in_words/number_parser.rb +0 -81
- data/lib/numbers_in_words/to_number.rb +0 -82
- data/spec/language_writer_spec.rb +0 -23
@@ -1,82 +0,0 @@
|
|
1
|
-
class NumbersInWords::ToNumber
|
2
|
-
delegate :to_s, to: :that
|
3
|
-
delegate :powers_of_ten_to_i, :exceptions_to_i, to: :language
|
4
|
-
attr_reader :that, :language
|
5
|
-
|
6
|
-
def initialize that, language=NumbersInWords.language
|
7
|
-
@that = that
|
8
|
-
@language = language
|
9
|
-
end
|
10
|
-
|
11
|
-
def language
|
12
|
-
if @language.is_a? Module
|
13
|
-
@language
|
14
|
-
else
|
15
|
-
@language = NumbersInWords.const_get(@language)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def handle_negative text
|
20
|
-
-1 * (text.gsub(/^minus /, "")).in_numbers if text =~ /^minus /
|
21
|
-
end
|
22
|
-
|
23
|
-
def in_numbers
|
24
|
-
text = to_s
|
25
|
-
|
26
|
-
text = strip_punctuation text
|
27
|
-
i = handle_negative text
|
28
|
-
return i if i
|
29
|
-
|
30
|
-
h = handle_decimals text
|
31
|
-
return h if h
|
32
|
-
|
33
|
-
integers = word_array_to_integers text.split(" ")
|
34
|
-
|
35
|
-
NumbersInWords::NumberParser.parse integers
|
36
|
-
end
|
37
|
-
|
38
|
-
def strip_punctuation text
|
39
|
-
text = text.downcase.gsub(/[^a-z ]/, " ")
|
40
|
-
to_remove = true
|
41
|
-
|
42
|
-
to_remove = text.gsub! " ", " " while to_remove
|
43
|
-
|
44
|
-
text
|
45
|
-
end
|
46
|
-
|
47
|
-
def handle_decimals text
|
48
|
-
match = text.match(/\spoint\s/)
|
49
|
-
if match
|
50
|
-
integer = match.pre_match.in_numbers
|
51
|
-
|
52
|
-
decimal = decimal_portion match.post_match
|
53
|
-
|
54
|
-
integer + decimal
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
def decimal_portion text
|
60
|
-
words = text.split " "
|
61
|
-
integers = word_array_to_integers words
|
62
|
-
decimal = "0." + integers.join()
|
63
|
-
decimal.to_f
|
64
|
-
end
|
65
|
-
|
66
|
-
#handles simple single word numbers
|
67
|
-
#e.g. one, seven, twenty, eight, thousand etc
|
68
|
-
def word_to_integer word
|
69
|
-
text = word.to_s.chomp.strip
|
70
|
-
|
71
|
-
exception = exceptions_to_i[text]
|
72
|
-
return exception if exception
|
73
|
-
|
74
|
-
power = powers_of_ten_to_i[text]
|
75
|
-
return 10 ** power if power
|
76
|
-
end
|
77
|
-
|
78
|
-
def word_array_to_integers words
|
79
|
-
words.map { |i| word_to_integer i }.compact
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require './spec/spec_helper'
|
2
|
-
|
3
|
-
describe NumbersInWords::English::LanguageWriterEnglish do
|
4
|
-
it "should display numbers grouped" do
|
5
|
-
count = 0
|
6
|
-
@writer = NumbersInWords::English::LanguageWriterEnglish.new(2111)
|
7
|
-
@writer.group_words(3) do |power, name, digits|
|
8
|
-
case count
|
9
|
-
when 0
|
10
|
-
expect(power).to eq(3)
|
11
|
-
expect(name).to eq("thousand")
|
12
|
-
expect(digits).to eq(2)
|
13
|
-
when 1
|
14
|
-
expect(power).to eq(0)
|
15
|
-
expect(name).to eq("one")
|
16
|
-
expect(digits).to eq(111)
|
17
|
-
end
|
18
|
-
count += 1
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
|