number_name_string 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf8b28bc55c5809c07f758cd08e689e48996cbd1
4
+ data.tar.gz: 10426658553b2dd85f945c760fb5e245469f738a
5
+ SHA512:
6
+ metadata.gz: b5a9b9075609c0fc3ee4184c12b3bbb28d3c8310785c5106f7907ec60a9b5afa6c3204aee34839dcde65c279a34ec71a7a2428a0e0e4a512b8d8df61c7e08c80
7
+ data.tar.gz: 643ba458cc9609bcb9d2d123afeaaa10008b4e0c6e6e6eaed9eb82baa3ad9827d566baf43d7daa18b6afba32837137285dd94126aa363e45f7bdd1428bf7b18c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rob@zwissler.org. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in number_name_string.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Rob Zwissler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # NumberNameString
2
+
3
+ Converts between numbers and their name strings (ex: 62 <=> "sixetytwo")
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'number_name_string'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install number_name_string
20
+
21
+ ## Usage
22
+
23
+ NumberNameString has a few interfaces, the simplest is using Module methods:
24
+
25
+ ```ruby
26
+ NumberNameString[4032] # "four hundred thirtytwo"
27
+ NumberNameString['six thousand'] # 6000
28
+ NumberNameString << 1019 # "one thousand nineteen"
29
+ ```
30
+
31
+ The Convert class can be instantiated and used directly:
32
+
33
+ ```ruby
34
+ nnns = NumberNameString::Convert.new
35
+ nnns[2000099] # "two million ninetynine"
36
+ nnns << 'sixtytwo' # 62
37
+ ```
38
+
39
+ Or, as a mixin directly on Fixnum and String classes:
40
+
41
+ ```ruby
42
+ include NumberNameString
43
+ 716.to_s # "seven hundred sixteen"
44
+ "four thousand two".to_i # 4002
45
+ 91346.to_comma # "91,346"
46
+ 'five thousand'.to_comma # "5,000"
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/number_name_string. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
63
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "number_name_string"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/test.rb ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/number_name_string'
4
+
5
+ include NumberNameString
6
+
7
+ # Test Fixnum extension to_comma
8
+ puts "1000.to_comma: #{1000.to_comma}"
9
+ puts "\'1000\'.to_comma: #{'1000'.to_comma}"
10
+
11
+ # Test String.to_i
12
+ puts "'one hundered'.to_i: #{'one hundred'.to_i}"
13
+
14
+ # Call as a module method
15
+ puts "Module method ([]): #{NumberNameString[100]}"
16
+ puts "Module method (<<): #{NumberNameString << 100}"
17
+
18
+ # Instantiate and call
19
+ nnsc = NumberNameString::Convert.new
20
+ puts "Class method ([]): #{nnsc[100]}"
21
+ puts "Class method (<<): #{nnsc << 100}"
22
+
23
+ # Print table of test numbers
24
+ TEST_NUMBERS = [(0..22).to_a +
25
+ (98..102).to_a +
26
+ (999998..1000001).to_a +
27
+ [87006200514255917]].flatten.freeze
28
+
29
+ TEST_NUMBERS.each do |num|
30
+ rendered = NumberNameString[num]
31
+ unrendered = NumberNameString[rendered]
32
+ printf("%-10s -> %-10s -> \"%s\"\n",
33
+ num.to_comma,
34
+ NumberNameString[unrendered],
35
+ rendered)
36
+ end
@@ -0,0 +1,139 @@
1
+ module NumberNameString
2
+ class Convert
3
+ #
4
+ # TODO: convert to arrays for multiple spellings
5
+ # - [forty, fourty]
6
+ # - [hundred, hundered]
7
+ #
8
+ ONES = %W{ #{} one two three four five six seven eight nine }
9
+ ONES_ZERO = %W{ zero one two three four five six seven eight nine }
10
+ TEENS = %w{ ten eleven twelve thirteen fourteen fifteen sixteen
11
+ seventeen eighteen nineteen }
12
+ TENS = %W{ #{} #{} twenty thirty forty fifty sixty seventy eighty ninety }
13
+ SCALE = %W{ #{} thousand million billion trillion quadrillion quintillion
14
+ sextillion septillion octillion nonillion decillion undecillion duodecillion
15
+ tredecillion quattuordecillion quindecillion sexdecillion septendecillion
16
+ octodecillion novemdecillion vigintillion }
17
+ SCALE_MAX = (SCALE.length - 1) * 3
18
+
19
+ def [](arg)
20
+ if arg.is_a? Fixnum
21
+ to_s arg
22
+ elsif arg.is_a? String
23
+ to_i arg
24
+ else
25
+ raise NumberNameStringError.new("Invalid arg type: #{arg.class.name}")
26
+ end
27
+ end
28
+
29
+ alias << []
30
+
31
+ def to_i(arg)
32
+ num = 0
33
+ words = arg.downcase.gsub('-', '').split(/\s+/)
34
+ while word = words.shift
35
+ accum = 0
36
+ # (sub_hundred | ones(false) hundred [sub_hundred] )[scale]]
37
+ if sub_hundred?(word)
38
+ if ones?(word, false) && hundred?(words[0])
39
+ accum += ones?(word) * 100
40
+ words.shift
41
+ word = words.shift
42
+ else
43
+ accum += sub_hundred?(word)
44
+ end
45
+ word = words.shift
46
+ if scale? word
47
+ accum *= scale? word
48
+ end
49
+ end
50
+ # puts "accum: #{accum}"
51
+ num += accum
52
+ end
53
+ num
54
+ end
55
+
56
+ def to_s(num, include_zero = true)
57
+ case digits = num.to_s.length
58
+ when 1
59
+ include_zero ? ONES_ZERO[num] : ONES[num]
60
+ when 2
61
+ num < 20 ? TEENS[num - 10] : "#{TENS[num / 10]}#{ONES[num % 10]}"
62
+ when 3
63
+ "#{ONES[num / 100]} hundred#{space_pad(to_s(num % 100, false))}"
64
+ when (4..SCALE_MAX)
65
+ zeros = 10 ** (((digits - 1) / 3) * 3)
66
+ "%s%s%s" % [to_s(num / zeros, false),
67
+ space_pad(SCALE[(digits - 1) / 3]),
68
+ space_pad(to_s(num % zeros, false))]
69
+ else
70
+ raise NumberNameStringError.new('Number out of range')
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ # ONES = %W{ #{} one two three four five six seven eight nine }
77
+ # ONES_ZERO = %W{ zero one two three four five six seven eight nine }
78
+ # TEENS = %w{ ten eleven twelve thirteen fourteen fifteen sixteen
79
+ # seventeen eighteen nineteen }
80
+ # TENS = %W{ #{} #{} twenty thirty fourty fifty sixty seventy eighty ninety }
81
+ # SCALE = %W{ #{} thousand million billion trillion quadrillion quintillion
82
+
83
+ def hundred?(word)
84
+ word == 'hundred' && 100
85
+ end
86
+
87
+ def number?(word)
88
+ ones?(word) || teens?(word) || tens?(word) || hundred?(word)
89
+ end
90
+
91
+ def ones?(word, with_zero = true)
92
+ if with_zero
93
+ ONES_ZERO.find_index word
94
+ else
95
+ ONES.find_index word
96
+ end
97
+ end
98
+
99
+ def scale?(word)
100
+ _ = SCALE.find_index word
101
+ 10 ** (_ * 3) if _
102
+ end
103
+
104
+ def space_pad(arg)
105
+ if arg.is_a?(NilClass) || arg == ''
106
+ ''
107
+ elsif arg =~ /^\s/
108
+ arg
109
+ else
110
+ " #{arg}"
111
+ end
112
+ end
113
+
114
+ def sub_hundred?(word)
115
+ ones?(word) || teens?(word) || tens?(word)
116
+ end
117
+
118
+ def teens?(word)
119
+ _ = TEENS.find_index word
120
+ _ + 10 if _
121
+ end
122
+
123
+ def tens?(word)
124
+ if TENS.find_index(word)
125
+ TENS.find_index(word) * 10
126
+ elsif tens_prefix?(word)
127
+ prefix = tens_prefix?(word)
128
+ suffix = word.slice(prefix.length, 100)
129
+ if ones?(suffix, false)
130
+ TENS.find_index(prefix) * 10 + ones?(suffix, false)
131
+ end
132
+ end
133
+ end
134
+
135
+ def tens_prefix?(word)
136
+ TENS.select { |prefix| prefix != "" && word =~ /^#{prefix}/ }.first
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,3 @@
1
+ module NumberNameString
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "number_name_string/convert"
2
+ require_relative "number_name_string/version"
3
+
4
+ module NumberNameString
5
+ class NumberNameStringError < ArgumentError ; end
6
+
7
+ # Would be ideal if we could only add this functionality only if included
8
+ class ::Fixnum
9
+ def to_comma
10
+ self.to_s.to_comma
11
+ end
12
+ end
13
+
14
+ # Would be ideal if we could only add this functionality only if included
15
+ class ::String
16
+ alias_method :old_to_i, :to_i
17
+
18
+ def to_comma
19
+ self.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
20
+ end
21
+
22
+ def to_i
23
+ result = NumberNameString[self]
24
+ # TODO: verify output of above may be a string
25
+ result.is_a?(String) ? result.old_to_i : result
26
+ end
27
+ end
28
+
29
+ def self.<<(num)
30
+ self[num]
31
+ end
32
+
33
+ def self.[](num)
34
+ (instance ||= NumberNameString::Convert.new)[num]
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'number_name_string/convert'
5
+ require 'number_name_string/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "number_name_string"
9
+ spec.version = NumberNameString::VERSION
10
+ spec.authors = ["Rob Zwissler"]
11
+ spec.email = ["rob@zwissler.org"]
12
+
13
+ spec.summary = %q{Converts to and from numbers and their corresponding names (ex: 2 <=> 'two')}
14
+ spec.description = %q{Pure Ruby stdlib based parser to convert to and from numbers and their corresponding names (ex: 2 <=> 'two'). }
15
+ spec.homepage = "https://github.com/robzr/number_name_string"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_name_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Zwissler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: 'Pure Ruby stdlib based parser to convert to and from numbers and their
56
+ corresponding names (ex: 2 <=> ''two''). '
57
+ email:
58
+ - rob@zwissler.org
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - bin/test.rb
74
+ - lib/number_name_string.rb
75
+ - lib/number_name_string/convert.rb
76
+ - lib/number_name_string/version.rb
77
+ - number_name_string.gemspec
78
+ homepage: https://github.com/robzr/number_name_string
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: 'Converts to and from numbers and their corresponding names (ex: 2 <=> ''two'')'
102
+ test_files: []