number_muncher 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +58 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/.travis.yml +22 -0
- data/CHANGES.md +5 -0
- data/Gemfile +8 -0
- data/README.md +109 -0
- data/Rakefile +6 -0
- data/assets/muncher.jpg +0 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/number_muncher.rb +28 -0
- data/lib/number_muncher/loader.rb +16 -0
- data/lib/number_muncher/numeric.rb +43 -0
- data/lib/number_muncher/parser.rb +26 -0
- data/lib/number_muncher/to_fraction.rb +50 -0
- data/lib/number_muncher/token/base.rb +67 -0
- data/lib/number_muncher/token/float.rb +17 -0
- data/lib/number_muncher/token/fraction.rb +32 -0
- data/lib/number_muncher/token/int.rb +17 -0
- data/lib/number_muncher/tokenizer.rb +51 -0
- data/lib/number_muncher/unicode.rb +27 -0
- data/lib/number_muncher/version.rb +3 -0
- data/number_muncher.gemspec +45 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 054afad7e465929f95956bb6250855c84df434d33831092ad0a68a7974c275a1
|
4
|
+
data.tar.gz: 612eca0243891138e05ced1b7083604b02e4658ca8b5334d0084350dac126b3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ceccc4d7568743c5449c1c4be6aaab05c46f715411366afe8a057f9c3bf9cb116855df447f5b5e53b24fc654106eb35bc145dcd7eaab83ba2f09fafe84812c3f
|
7
|
+
data.tar.gz: 1a530c6481e40cbf762e959b0b0cf623104addd647be4fbb0866a8010c8c8568744477922264dc787f3a65c9698d41fd04a93b9340ae38cc3f76b9b0955a7461
|
data/.gitignore
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/bin/rubocop
|
4
|
+
/.config
|
5
|
+
/coverage/
|
6
|
+
/InstalledFiles
|
7
|
+
/pkg/
|
8
|
+
/.rspec_status
|
9
|
+
/spec/reports/
|
10
|
+
/spec/examples.txt
|
11
|
+
/test/tmp/
|
12
|
+
/test/version_tmp/
|
13
|
+
/tmp/
|
14
|
+
|
15
|
+
# Used by dotenv library to load environment variables.
|
16
|
+
.env
|
17
|
+
|
18
|
+
# Ignore Byebug command history file.
|
19
|
+
.byebug_history
|
20
|
+
|
21
|
+
## Specific to RubyMotion:
|
22
|
+
.dat*
|
23
|
+
.repl_history
|
24
|
+
build/
|
25
|
+
*.bridgesupport
|
26
|
+
build-iPhoneOS/
|
27
|
+
build-iPhoneSimulator/
|
28
|
+
|
29
|
+
## Specific to RubyMotion (use of CocoaPods):
|
30
|
+
#
|
31
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
32
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
33
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
34
|
+
#
|
35
|
+
# vendor/Pods/
|
36
|
+
|
37
|
+
## Documentation cache and generated files:
|
38
|
+
/.yardoc/
|
39
|
+
/_yardoc/
|
40
|
+
/doc/
|
41
|
+
/rdoc/
|
42
|
+
|
43
|
+
## Environment normalization:
|
44
|
+
/.bundle/
|
45
|
+
/vendor/bundle
|
46
|
+
/lib/bundler/man/
|
47
|
+
|
48
|
+
# for a library or gem, you might want to ignore these files since the code is
|
49
|
+
# intended to run in multiple environments; otherwise, check them in:
|
50
|
+
Gemfile.lock
|
51
|
+
.ruby-version
|
52
|
+
.ruby-gemset
|
53
|
+
|
54
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
55
|
+
.rvmrc
|
56
|
+
|
57
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
58
|
+
# .rubocop-https?--*
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop_defaults: .rubocop.yml
|
3
|
+
|
4
|
+
inherit_mode:
|
5
|
+
merge:
|
6
|
+
- Exclude
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
TargetRubyVersion: 2.7
|
10
|
+
|
11
|
+
Gemspec/RequiredRubyVersion:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Layout/EmptyLinesAroundExceptionHandlingKeywords:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Layout/FirstArrayElementIndentation:
|
18
|
+
EnforcedStyle: consistent
|
19
|
+
|
20
|
+
Lint/NonDeterministicRequireOrder:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
RSpec/ExpectActual:
|
24
|
+
Exclude:
|
25
|
+
- spec/number_muncher/parser_spec.rb
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
---
|
2
|
+
os: linux
|
3
|
+
language: ruby
|
4
|
+
cache: bundler
|
5
|
+
rvm:
|
6
|
+
- 2.4.9
|
7
|
+
- 2.5.7
|
8
|
+
- 2.6.5
|
9
|
+
- 2.7.0.preview3
|
10
|
+
before_install: gem install bundler -v 2.0.2
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
include:
|
14
|
+
- stage: lint
|
15
|
+
rvm: 2.6.5
|
16
|
+
script: bundle exec rubocop
|
17
|
+
allow_failures:
|
18
|
+
- rvm: 2.7.0.preview3
|
19
|
+
|
20
|
+
stages:
|
21
|
+
- lint
|
22
|
+
- test
|
data/CHANGES.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
3
|
+
|
4
|
+
# Specify your gem's dependencies in number_muncher.gemspec
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem 'rubocop', github: 'rubocop-hq/rubocop'
|
8
|
+
gem 'rubocop_defaults', github: 'dvandersluis/rubocop_defaults', branch: 'experimental'
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# NumberMuncher
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/dvandersluis/number_muncher.svg?branch=master)](https://travis-ci.org/dvandersluis/fractions)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/number_muncher.svg)](https://badge.fury.io/rb/number_muncher)
|
5
|
+
|
6
|
+
Parses strings into numbers, including integers, decimals, and fractions (including unicode fraction glyphs like `⅐`).
|
7
|
+
|
8
|
+
<p align="center">
|
9
|
+
<img src="assets/muncher.jpg" width="15%" height="15%">
|
10
|
+
<br/>
|
11
|
+
<sup align="center">
|
12
|
+
A <a href="https://en.wikipedia.org/wiki/Munchers#Number_Munchers">Number Muncher</a>
|
13
|
+
</sup>
|
14
|
+
</p>
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
### Formats
|
19
|
+
|
20
|
+
`NumberMuncher` accepts the following formats (with an optional leading `-`):
|
21
|
+
|
22
|
+
* Integers (with or without separators): `1`, `-1`, `5,394`, `9592`, etc.
|
23
|
+
* Decimals (with or without separators): `3.5`, `-7.4`, `9,104.94`, etc.
|
24
|
+
* Fractions: `3/5`, `-19/20`, `¾`, etc. (see [`unicode.rb`](lib/number_muncher/unicode.rb) for a full list of supported Unicode fraction glyphs).
|
25
|
+
* Mixed fractions (with or without separators): `3 3/4`, `1-1/3`, `-1⅔`, `1,234 ⅚`, etc.
|
26
|
+
|
27
|
+
Other inputs, including invalid fractions (eg. `1/0`), are considered invalid and will raise `NumberMuncher::InvalidNumber`.
|
28
|
+
|
29
|
+
### Parsing
|
30
|
+
|
31
|
+
Parsing a numeric string for a single `Rational` (which can then have `to_i`, `to_f`, etc. called on it as per your needs):
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
NumberMuncher.parse('4 1/2') #=> 9/2r
|
35
|
+
|
36
|
+
# or equivalent:
|
37
|
+
NumberMuncher::Numeric.new('4 1/2')
|
38
|
+
```
|
39
|
+
|
40
|
+
If the input string contains multiple numbers (other than mixed fractions), `NumberMuncher::InvalidParseExpression` will be raised.
|
41
|
+
|
42
|
+
### Scanning
|
43
|
+
|
44
|
+
Returns all the numbers in a string, as `Rational`s.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
NumberMuncher.scan('Cook at 375° for 10 minutes, flip and cook for another 5.5 minutes')
|
48
|
+
# => [375r, 10r, 11/2r]
|
49
|
+
```
|
50
|
+
|
51
|
+
### Formatting
|
52
|
+
|
53
|
+
Returns a fraction string for a given numeric value.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
NumberMuncher.to_fraction(1/4r) #=> "¼"
|
57
|
+
NumberMuncher.to_fraction(9.625) #=> "9⅝"
|
58
|
+
|
59
|
+
# Without using unicode glyphs:
|
60
|
+
NumberMuncher.to_fraction(1/4r, unicode: false) #=> "1/4"
|
61
|
+
NumberMuncher.to_fraction(9.625, unicode: false) #=> "9 5/8"
|
62
|
+
|
63
|
+
# Rounding:
|
64
|
+
NumberMuncher.to_fraction(3/7r, round_to: 1/4r) #=> "½"
|
65
|
+
|
66
|
+
# Rationalizing (adjust the fraction precision, default = 0.001):
|
67
|
+
NumberMuncher.to_fraction(Math::PI) #=> "3 9/64"
|
68
|
+
NumberMuncher.to_fraction(Math::PI, factor: 0.1) #=> "3⅕"
|
69
|
+
NumberMuncher.to_fraction(Math::PI, factor: 0.0000000000000001) #=> "3 39854788871587/281474976710656"
|
70
|
+
```
|
71
|
+
|
72
|
+
`to_fraction` can also be called as an instance method. Note that this method does not take a `round_to` argument, but you can chain `round` before calling `to_fraction`:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
NumberMuncher.parse('3.456').to_fraction #=> "3 21/46"
|
76
|
+
NumberMuncher.parse('3.456').round(0.1).to_fraction #=> "3½"
|
77
|
+
NumberMuncher.parse('3.456').round(0.1).to_fraction(unicode: false) #=> "3 1/2"
|
78
|
+
```
|
79
|
+
|
80
|
+
## Installation
|
81
|
+
|
82
|
+
`NumberMuncher` requires ruby >= 2.4.4.
|
83
|
+
|
84
|
+
Add this line to your application's Gemfile:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
gem 'number_muncher'
|
88
|
+
```
|
89
|
+
|
90
|
+
And then execute:
|
91
|
+
|
92
|
+
$ bundle
|
93
|
+
|
94
|
+
Or install it yourself as:
|
95
|
+
|
96
|
+
$ gem install number_muncher
|
97
|
+
|
98
|
+
## Configuration
|
99
|
+
|
100
|
+
Separators for thousands and decimals can be configured. Default values are shown below:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
NumberMuncher.thousands_separator = ','
|
104
|
+
NumberMuncher.decimal_separator = '.'
|
105
|
+
```
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dvandersluis/number_muncher.
|
data/Rakefile
ADDED
data/assets/muncher.jpg
ADDED
Binary file
|
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'number_muncher'
|
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
|
+
def reload!
|
10
|
+
puts 'Reloading...'
|
11
|
+
NumberMuncher::Loader.instance.reload
|
12
|
+
end
|
13
|
+
|
14
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
15
|
+
require 'pry'
|
16
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
# Setup zeitwerk
|
4
|
+
require 'number_muncher/loader'
|
5
|
+
NumberMuncher::Loader.instance.setup
|
6
|
+
|
7
|
+
module NumberMuncher
|
8
|
+
include ActiveSupport::Configurable
|
9
|
+
|
10
|
+
class InvalidNumber < StandardError; end
|
11
|
+
class InvalidParseExpression < StandardError; end
|
12
|
+
class IllegalRoundValue < StandardError; end
|
13
|
+
|
14
|
+
config_accessor(:thousands_separator, instance_accessor: false) { ',' }
|
15
|
+
config_accessor(:decimal_separator, instance_accessor: false) { '.' }
|
16
|
+
|
17
|
+
def self.parse(str)
|
18
|
+
Numeric.new(str)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.scan(str)
|
22
|
+
Tokenizer.new(str).tokenize
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.to_fraction(value, round_to: nil, **opts)
|
26
|
+
parse(value).round(round_to).to_fraction(**opts)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'zeitwerk'
|
2
|
+
|
3
|
+
module NumberMuncher
|
4
|
+
module Loader
|
5
|
+
def self.instance
|
6
|
+
@instance ||= begin
|
7
|
+
called_from = caller_locations(1, 1).first.path
|
8
|
+
Zeitwerk::Registry.loader_for_gem(called_from).tap do |loader| # rubocop:disable Style/SymbolProc
|
9
|
+
loader.enable_reloading
|
10
|
+
|
11
|
+
# loader.logger = method(:puts)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
class Numeric < SimpleDelegator
|
3
|
+
ARITHMETIC_OPERATORS = %i(+ - * / **).freeze
|
4
|
+
|
5
|
+
alias_method :rational, :__getobj__
|
6
|
+
|
7
|
+
def initialize(value)
|
8
|
+
super(parse(value))
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_r
|
12
|
+
rational
|
13
|
+
end
|
14
|
+
|
15
|
+
ARITHMETIC_OPERATORS.each do |operator|
|
16
|
+
define_method(operator) do |other|
|
17
|
+
self.class.new(to_r.send(operator, other.to_r))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def round(to = nil)
|
22
|
+
return self unless to
|
23
|
+
|
24
|
+
to = Numeric.new(to)
|
25
|
+
raise IllegalRoundValue, 'cannot round to nearest 0' if to.zero?
|
26
|
+
|
27
|
+
to * (self / to).to_f.round
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_fraction(**opts)
|
31
|
+
ToFraction.new(opts).call(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def parse(value)
|
37
|
+
Rational(value)
|
38
|
+
|
39
|
+
rescue ArgumentError
|
40
|
+
Parser.new(value).call.to_r
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
class Parser
|
3
|
+
def initialize(value)
|
4
|
+
@value = value
|
5
|
+
end
|
6
|
+
|
7
|
+
def call
|
8
|
+
return Numeric.new(value) unless value.is_a?(String)
|
9
|
+
return nil if tokens.empty?
|
10
|
+
raise InvalidParseExpression, 'parse requires a single number' unless tokens.size == 1
|
11
|
+
|
12
|
+
tokens.first.value
|
13
|
+
|
14
|
+
rescue ArgumentError, ZeroDivisionError
|
15
|
+
raise InvalidNumber, "#{value} is not valid input to parse."
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_reader :value
|
21
|
+
|
22
|
+
def tokens
|
23
|
+
NumberMuncher::Tokenizer.new(value).call(raise: true)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
class ToFraction
|
3
|
+
def initialize(unicode: true, separator: nil, factor: 0.001)
|
4
|
+
@unicode = unicode
|
5
|
+
@separator = separator
|
6
|
+
@factor = factor
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(number, round_to: nil)
|
10
|
+
return '' unless number
|
11
|
+
|
12
|
+
@rational = rationalize(number, round_to)
|
13
|
+
return '0' if rational.zero?
|
14
|
+
|
15
|
+
separator = glyph ? '' : ' '
|
16
|
+
parts = [whole, glyph || fraction].compact
|
17
|
+
"#{sign}#{parts.map(&:to_s).join(separator)}"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :rational, :unicode, :separator, :factor
|
23
|
+
alias_method :unicode?, :unicode
|
24
|
+
|
25
|
+
def rationalize(number, round_to)
|
26
|
+
Numeric.new(number).round(round_to).rationalize(factor)
|
27
|
+
end
|
28
|
+
|
29
|
+
def glyph
|
30
|
+
return nil unless unicode? && fraction
|
31
|
+
|
32
|
+
Unicode::MAPPING.key(fraction)
|
33
|
+
end
|
34
|
+
|
35
|
+
def whole
|
36
|
+
return if (-1.0.next_float...1).cover?(rational)
|
37
|
+
|
38
|
+
(rational.numerator / rational.denominator).abs
|
39
|
+
end
|
40
|
+
|
41
|
+
def fraction
|
42
|
+
value = (whole ? rational.abs - whole : rational).abs
|
43
|
+
value.zero? ? nil : value
|
44
|
+
end
|
45
|
+
|
46
|
+
def sign
|
47
|
+
'-' if rational.negative?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
module Token
|
3
|
+
class Base
|
4
|
+
attr_reader :value, :text
|
5
|
+
delegate :to_r, to: :value
|
6
|
+
|
7
|
+
def self.scan(scanner)
|
8
|
+
new(scanner.matched, captures(scanner, scanner.matched)) if scanner.scan(regex)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.captures(scanner, text)
|
12
|
+
if scanner.respond_to?(:captures)
|
13
|
+
scanner.captures.map(&:presence)
|
14
|
+
else
|
15
|
+
match = regex.match(text)
|
16
|
+
|
17
|
+
match.regexp.named_captures.each_with_object([]) do |(capture, _), arr|
|
18
|
+
arr << match[capture]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(text, captures = nil)
|
24
|
+
@text = text
|
25
|
+
@captures = captures
|
26
|
+
@value = Numeric.new(parse)
|
27
|
+
end
|
28
|
+
|
29
|
+
def int?
|
30
|
+
false
|
31
|
+
end
|
32
|
+
|
33
|
+
def float?
|
34
|
+
false
|
35
|
+
end
|
36
|
+
|
37
|
+
def fraction?
|
38
|
+
false
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_a
|
42
|
+
[
|
43
|
+
self.class.name.demodulize.underscore.to_sym,
|
44
|
+
value
|
45
|
+
]
|
46
|
+
end
|
47
|
+
|
48
|
+
def ==(other)
|
49
|
+
return to_a == other if other.is_a?(Array)
|
50
|
+
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
def inspect
|
55
|
+
to_a.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def parse
|
61
|
+
Rational(text)
|
62
|
+
end
|
63
|
+
|
64
|
+
attr_reader :captures
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
module Token
|
3
|
+
class Float < Base
|
4
|
+
def self.regex
|
5
|
+
/(#{Int.regex}|-?)#{Regexp.quote(NumberMuncher.decimal_separator)}\d+/
|
6
|
+
end
|
7
|
+
|
8
|
+
def text
|
9
|
+
super.delete(NumberMuncher.thousands_separator).tr(NumberMuncher.decimal_separator, '.')
|
10
|
+
end
|
11
|
+
|
12
|
+
def float?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
module Token
|
3
|
+
class Fraction < Base
|
4
|
+
def self.regex
|
5
|
+
%r{
|
6
|
+
(?<sign>-)?
|
7
|
+
(?:(?<whole>#{Int.regex})(?:-|\s*))??
|
8
|
+
(
|
9
|
+
(?<numerator>\d+)\s*/\s*(?<denominator>\d+)
|
10
|
+
|
|
11
|
+
(?<unicode>#{Unicode::REGEX})
|
12
|
+
)
|
13
|
+
}x
|
14
|
+
end
|
15
|
+
|
16
|
+
def fraction?
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def parse
|
23
|
+
sign, whole, numerator, denominator, unicode = captures
|
24
|
+
|
25
|
+
r = unicode ? Unicode::MAPPING[unicode] : Rational(numerator, denominator)
|
26
|
+
r += Int.new(whole).to_r if whole
|
27
|
+
r *= -1 if sign
|
28
|
+
r
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
module Token
|
3
|
+
class Int < Base
|
4
|
+
def self.regex
|
5
|
+
/-?(\d{1,3}(#{Regexp.quote(NumberMuncher.thousands_separator)}\d{3})+|\d+)/
|
6
|
+
end
|
7
|
+
|
8
|
+
def text
|
9
|
+
super.delete(NumberMuncher.thousands_separator)
|
10
|
+
end
|
11
|
+
|
12
|
+
def int?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
class Tokenizer
|
3
|
+
INVALID_REGEX = /(?![#{Unicode::VALUES.join}])\S+/.freeze
|
4
|
+
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
delegate :each, :empty?, to: :tokens
|
8
|
+
|
9
|
+
attr_reader :string, :tokens
|
10
|
+
|
11
|
+
def initialize(string)
|
12
|
+
@string = string
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(raise: false)
|
16
|
+
@tokens = []
|
17
|
+
scan(raise) until scanner.eos?
|
18
|
+
tokens
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def scanner
|
24
|
+
@scanner ||= StringScanner.new(string)
|
25
|
+
end
|
26
|
+
|
27
|
+
def scan(raise)
|
28
|
+
scanner.skip(/\s+/)
|
29
|
+
token = next_token
|
30
|
+
tokens << token if token
|
31
|
+
handle_invalid(raise)
|
32
|
+
|
33
|
+
rescue ZeroDivisionError
|
34
|
+
Kernel.raise if raise
|
35
|
+
end
|
36
|
+
|
37
|
+
def next_token
|
38
|
+
Token::Fraction.scan(scanner) ||
|
39
|
+
Token::Float.scan(scanner) ||
|
40
|
+
Token::Int.scan(scanner)
|
41
|
+
end
|
42
|
+
|
43
|
+
def handle_invalid(raise)
|
44
|
+
if raise
|
45
|
+
raise InvalidNumber if scanner.match?(INVALID_REGEX)
|
46
|
+
else
|
47
|
+
scanner.skip(INVALID_REGEX)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module NumberMuncher
|
2
|
+
module Unicode
|
3
|
+
MAPPING = {
|
4
|
+
'½' => 1/2r,
|
5
|
+
'⅓' => 1/3r,
|
6
|
+
'⅔' => 2/3r,
|
7
|
+
'¼' => 1/4r,
|
8
|
+
'¾' => 3/4r,
|
9
|
+
'⅕' => 1/5r,
|
10
|
+
'⅖' => 2/5r,
|
11
|
+
'⅗' => 3/5r,
|
12
|
+
'⅘' => 4/5r,
|
13
|
+
'⅙' => 1/6r,
|
14
|
+
'⅚' => 5/6r,
|
15
|
+
'⅐' => 1/7r,
|
16
|
+
'⅛' => 1/8r,
|
17
|
+
'⅜' => 3/8r,
|
18
|
+
'⅝' => 5/8r,
|
19
|
+
'⅞' => 7/8r,
|
20
|
+
'⅑' => 1/9r,
|
21
|
+
'⅒' => 1/10r
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
VALUES = MAPPING.keys
|
25
|
+
REGEX = Regexp.union(VALUES).freeze
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'number_muncher/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'number_muncher'
|
7
|
+
spec.version = NumberMuncher::VERSION
|
8
|
+
spec.authors = ['Daniel Vandersluis']
|
9
|
+
spec.email = ['daniel.vandersluis@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Tiny ruby library for dealing with fractions.'
|
12
|
+
spec.homepage = 'https://github.com/dvandersluis/number_muncher'
|
13
|
+
spec.licenses = ['MIT']
|
14
|
+
|
15
|
+
spec.required_ruby_version = '>= 2.4.4'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
23
|
+
spec.metadata['changelog_uri'] = 'https://github.com/dvandersluis/number_muncher/CHANGES.md'
|
24
|
+
else
|
25
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
26
|
+
'public gem pushes.'
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = 'exe'
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ['lib']
|
37
|
+
|
38
|
+
spec.add_dependency 'activesupport'
|
39
|
+
spec.add_dependency 'zeitwerk'
|
40
|
+
|
41
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
42
|
+
spec.add_development_dependency 'pry'
|
43
|
+
spec.add_development_dependency 'rake'
|
44
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: number_muncher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Vandersluis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: zeitwerk
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- daniel.vandersluis@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CHANGES.md
|
109
|
+
- Gemfile
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- assets/muncher.jpg
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- lib/number_muncher.rb
|
116
|
+
- lib/number_muncher/loader.rb
|
117
|
+
- lib/number_muncher/numeric.rb
|
118
|
+
- lib/number_muncher/parser.rb
|
119
|
+
- lib/number_muncher/to_fraction.rb
|
120
|
+
- lib/number_muncher/token/base.rb
|
121
|
+
- lib/number_muncher/token/float.rb
|
122
|
+
- lib/number_muncher/token/fraction.rb
|
123
|
+
- lib/number_muncher/token/int.rb
|
124
|
+
- lib/number_muncher/tokenizer.rb
|
125
|
+
- lib/number_muncher/unicode.rb
|
126
|
+
- lib/number_muncher/version.rb
|
127
|
+
- number_muncher.gemspec
|
128
|
+
homepage: https://github.com/dvandersluis/number_muncher
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata:
|
132
|
+
allowed_push_host: https://rubygems.org
|
133
|
+
homepage_uri: https://github.com/dvandersluis/number_muncher
|
134
|
+
changelog_uri: https://github.com/dvandersluis/number_muncher/CHANGES.md
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: 2.4.4
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubygems_version: 3.1.0.pre3
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Tiny ruby library for dealing with fractions.
|
154
|
+
test_files: []
|