formatter-number 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rubocop.yml +80 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +49 -0
- data/Rakefile +30 -0
- data/formatter-number.gemspec +26 -0
- data/lib/formatter/number/version.rb +8 -0
- data/lib/formatter/number.rb +86 -0
- data/lib/formatter-number.rb +3 -0
- data/test/integration_test.rb +86 -0
- data/test/test_helper.rb +5 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a958ca48f69073a657bda85bb491bc08ac9b4322
|
4
|
+
data.tar.gz: 7c665188d484612937c86c3c79fa8f278074bc6e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8cdb4b416ecec80cf80ce3a2d4e57bbbb0e2c7c05b247bd22c691401a8eec18a2a432dff4861437c7956fa666a8b01ca8219f0f46c76a6cdc48d7a9c2b85da59
|
7
|
+
data.tar.gz: 561e37d6b49006736c18e5e87701990b34a5b354c0d434574d72fda4f06542e8e6d5a548da2ea2f33ac8f193ebab717151df9aba7907be76413a581c1fb8fc7c
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- Gemfile
|
4
|
+
- bin/**/*
|
5
|
+
- vendor/**/*
|
6
|
+
|
7
|
+
Encoding:
|
8
|
+
Enabled: true
|
9
|
+
|
10
|
+
Documentation:
|
11
|
+
Enabled: true
|
12
|
+
Exclude:
|
13
|
+
- test/**/*
|
14
|
+
|
15
|
+
LineLength:
|
16
|
+
Max: 82
|
17
|
+
|
18
|
+
# Class
|
19
|
+
ClassLength:
|
20
|
+
Max: 80
|
21
|
+
|
22
|
+
EmptyLinesAroundAccessModifier:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
AccessModifierIndentation:
|
26
|
+
EnforcedStyle: indent
|
27
|
+
|
28
|
+
# Error
|
29
|
+
RaiseArgs:
|
30
|
+
EnforcedStyle: exploded
|
31
|
+
|
32
|
+
SignalException:
|
33
|
+
EnforcedStyle: semantic
|
34
|
+
|
35
|
+
# Method
|
36
|
+
MethodLength:
|
37
|
+
Max: 10
|
38
|
+
|
39
|
+
SingleLineMethods:
|
40
|
+
AllowIfMethodIsEmpty: false
|
41
|
+
|
42
|
+
CollectionMethods:
|
43
|
+
PreferredMethods:
|
44
|
+
collect: 'map'
|
45
|
+
collect!: 'map!'
|
46
|
+
reduce: 'inject'
|
47
|
+
detect: 'find'
|
48
|
+
find_all: 'select'
|
49
|
+
|
50
|
+
FormatString:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
# Parameter
|
54
|
+
ParameterLists:
|
55
|
+
Max: 3
|
56
|
+
CountKeywordArgs: true
|
57
|
+
|
58
|
+
BracesAroundHashParameters:
|
59
|
+
Enabled: false
|
60
|
+
EnforcedStyle: braces
|
61
|
+
|
62
|
+
# Block
|
63
|
+
BlockNesting:
|
64
|
+
Max: 3
|
65
|
+
|
66
|
+
# Brackets
|
67
|
+
SpaceInsideBrackets:
|
68
|
+
Enabled: true
|
69
|
+
|
70
|
+
# Hash
|
71
|
+
IndentHash:
|
72
|
+
EnforcedStyle: consistent
|
73
|
+
|
74
|
+
SpaceInsideHashLiteralBraces:
|
75
|
+
EnforcedStyle: space
|
76
|
+
EnforcedStyleForEmptyBraces: space
|
77
|
+
|
78
|
+
# String
|
79
|
+
StringLiterals:
|
80
|
+
EnforcedStyle: single_quotes
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Terje Larsen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# formatter-number
|
2
|
+
|
3
|
+
Format numbers
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/formatter-number.svg)](http://badge.fury.io/rb/formatter-number)
|
6
|
+
[![Build
|
7
|
+
Status](https://travis-ci.org/terlar/formatter-number.svg)](https://travis-ci.org/terlar/formatter-number)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/terlar/formatter-number.png)](https://codeclimate.com/github/terlar/formatter-number)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'formatter-number'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install formatter-number
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
formatter = Formatter::Number.new
|
28
|
+
formatter.format(10.556) # "10.56"
|
29
|
+
formatter.format(505_000) # "505,000"
|
30
|
+
|
31
|
+
formatter = Formatter::Number.new(grouping: 2)
|
32
|
+
formatter.format(10_000) # "10,000"
|
33
|
+
formatter.format(505_000) # "5,05,000"
|
34
|
+
|
35
|
+
formatter = Formatter::Number.new(grouping: 4)
|
36
|
+
formatter.format(100_004) # "10,0004"
|
37
|
+
formatter.format(10_050_026) # "1005,0026"
|
38
|
+
|
39
|
+
formatter = Formatter::Number.new(delimiter: ' ')
|
40
|
+
formatter.format(10_000) # "10 000"
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/terlar/formatter-number/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
|
6
|
+
require 'rake/testtask'
|
7
|
+
require 'rubocop/rake_task'
|
8
|
+
|
9
|
+
RuboCop::RakeTask.new
|
10
|
+
|
11
|
+
task default: :test
|
12
|
+
task test: 'test:all'
|
13
|
+
task ci: %w(rubocop test:all)
|
14
|
+
|
15
|
+
namespace :test do
|
16
|
+
desc 'Run all tests'
|
17
|
+
Rake::TestTask.new :all do |t|
|
18
|
+
t.test_files = Rake::FileList['test/*_test.rb']
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Run unit tests'
|
22
|
+
Rake::TestTask.new :unit do |t|
|
23
|
+
t.test_files = Rake::FileList['test/unit_test.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Run integration tests'
|
27
|
+
Rake::TestTask.new :integration do |t|
|
28
|
+
t.test_files = Rake::FileList['test/integration_test.rb']
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('../lib/formatter/number/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'formatter-number'
|
7
|
+
gem.version = Formatter::Number::VERSION.dup
|
8
|
+
gem.authors = ['Terje Larsen']
|
9
|
+
gem.email = 'terlar@gmail.com'
|
10
|
+
gem.description = 'Number formatter'
|
11
|
+
gem.summary = gem.description
|
12
|
+
gem.homepage = 'https://github.com/terlar/formatter-number'
|
13
|
+
gem.license = 'MIT'
|
14
|
+
|
15
|
+
gem.require_paths = %w(lib)
|
16
|
+
gem.files = `git ls-files`.split($RS)
|
17
|
+
gem.test_files = gem.files.grep(/^test\//)
|
18
|
+
gem.extra_rdoc_files = %w(LICENSE README.md)
|
19
|
+
|
20
|
+
gem.required_ruby_version = '>= 1.9.3'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'bundler', '~> 1.6'
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'minitest'
|
25
|
+
gem.add_development_dependency 'rubocop'
|
26
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Formatter
|
4
|
+
# Number formatter
|
5
|
+
class Number
|
6
|
+
DEFAULTS = {
|
7
|
+
decimals: 2,
|
8
|
+
fixed: false,
|
9
|
+
separator: '.',
|
10
|
+
grouping: 3,
|
11
|
+
delimiter: ','
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
# Initialize a formatter with the desired options.
|
15
|
+
#
|
16
|
+
# @param [Hash] options the options to create a formatter with
|
17
|
+
# @option options [Integer] :decimals (2) Number of decimal points
|
18
|
+
# @option options [Boolean] :fixed (false) Fixed decimal places
|
19
|
+
# @option options [String] :separator ('.') Decimal mark
|
20
|
+
# @option options [Integer] :grouping (3) Number of digits per group
|
21
|
+
# @option options [String] :delimiter (',') Delimiter between groups
|
22
|
+
def initialize(options = { }.freeze)
|
23
|
+
@options = defaults.merge(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def format(number)
|
27
|
+
case number
|
28
|
+
when Float then format_float(number)
|
29
|
+
when Integer then format_integer(number)
|
30
|
+
else fail ArgumentError
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def format_float(number)
|
36
|
+
if fixed?
|
37
|
+
number = "%0.0#{decimals}f" % number
|
38
|
+
else
|
39
|
+
number = number.round(decimals)
|
40
|
+
end
|
41
|
+
|
42
|
+
number = number.to_s.gsub('.', separator)
|
43
|
+
|
44
|
+
delimit(number)
|
45
|
+
end
|
46
|
+
|
47
|
+
def format_integer(number)
|
48
|
+
delimit(number)
|
49
|
+
end
|
50
|
+
|
51
|
+
def delimit(number)
|
52
|
+
if grouping == 2
|
53
|
+
# When grouped by two the first group is by three and the rest of them
|
54
|
+
# are grouped by two. This is according to the Indian Numbering System.
|
55
|
+
number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1#{delimiter}")
|
56
|
+
else
|
57
|
+
group = '\d' * grouping
|
58
|
+
number.to_s.gsub(/(\d)(?=(#{group})+(?!\d))/, "\\1#{delimiter}")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def defaults
|
63
|
+
DEFAULTS
|
64
|
+
end
|
65
|
+
|
66
|
+
def decimals
|
67
|
+
@options.fetch :decimals
|
68
|
+
end
|
69
|
+
|
70
|
+
def fixed?
|
71
|
+
@options.fetch :fixed
|
72
|
+
end
|
73
|
+
|
74
|
+
def separator
|
75
|
+
@options.fetch :separator
|
76
|
+
end
|
77
|
+
|
78
|
+
def grouping
|
79
|
+
@options.fetch :grouping
|
80
|
+
end
|
81
|
+
|
82
|
+
def delimiter
|
83
|
+
@options.fetch :delimiter
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'test_helper'
|
4
|
+
|
5
|
+
class IntegrationTest < Minitest::Test
|
6
|
+
attr_reader :options
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@options = { }
|
10
|
+
end
|
11
|
+
|
12
|
+
def format(number)
|
13
|
+
Formatter::Number.new(options).format(number)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_fails_with_invalid_number
|
17
|
+
assert_raises ArgumentError do
|
18
|
+
format('invalid number')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_decimals_defaults_to_two
|
23
|
+
assert_equal '10.56', format(10.556)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_can_set_decimals
|
27
|
+
@options[:decimals] = 3
|
28
|
+
|
29
|
+
assert_equal '10.556', format(10.556)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_decimals_are_not_fixed_by_default
|
33
|
+
assert_equal '10.5', format(10.5)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_can_set_fixed_to_make_decimals_fixed
|
37
|
+
@options[:fixed] = true
|
38
|
+
|
39
|
+
assert_equal '10.50', format(10.5)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_separator_defaults_to_dot
|
43
|
+
assert_equal '10.53', format(10.53)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_can_set_separator_delimiter
|
47
|
+
@options[:separator] = ','
|
48
|
+
|
49
|
+
assert_equal '10,53', format(10.53)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_grouping_defaults_to_three
|
53
|
+
assert_equal '100', format(100)
|
54
|
+
assert_equal '10,000', format(10_000)
|
55
|
+
assert_equal '505,000', format(505_000)
|
56
|
+
assert_equal '121,212,123', format(121_212_123)
|
57
|
+
assert_equal '7,000,000,000', format(7_000_000_000)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_can_set_the_grouping_with_2
|
61
|
+
@options[:grouping] = 2
|
62
|
+
|
63
|
+
assert_equal '100', format(100)
|
64
|
+
assert_equal '10,000', format(10_000)
|
65
|
+
assert_equal '5,05,000', format(505_000)
|
66
|
+
assert_equal '12,12,12,123', format(121_212_123)
|
67
|
+
assert_equal '7,00,00,00,000', format(7_000_000_000)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_can_set_the_grouping_with_4
|
71
|
+
@options[:grouping] = 4
|
72
|
+
assert_equal '10,0004', format(100_004)
|
73
|
+
assert_equal '1005,0026', format(10_050_026)
|
74
|
+
assert_equal '12,3456,7890,2345', format(12_345_678_902_345)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_delimiter_defaults_to_comma
|
78
|
+
assert_equal '10,000', format(10_000)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_can_set_the_delimiter
|
82
|
+
@options[:delimiter] = ' '
|
83
|
+
|
84
|
+
assert_equal '10 000', format(10_000)
|
85
|
+
end
|
86
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formatter-number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Terje Larsen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-21 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
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
|
+
description: Number formatter
|
70
|
+
email: terlar@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE
|
75
|
+
- README.md
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- formatter-number.gemspec
|
85
|
+
- lib/formatter-number.rb
|
86
|
+
- lib/formatter/number.rb
|
87
|
+
- lib/formatter/number/version.rb
|
88
|
+
- test/integration_test.rb
|
89
|
+
- test/test_helper.rb
|
90
|
+
homepage: https://github.com/terlar/formatter-number
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 1.9.3
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Number formatter
|
114
|
+
test_files:
|
115
|
+
- test/integration_test.rb
|
116
|
+
- test/test_helper.rb
|