formatting 0.0.3 → 0.0.6
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 +4 -4
- data/README.md +2 -4
- data/formatting.gemspec +0 -2
- data/lib/formatting/currency.rb +1 -1
- data/lib/formatting/number.rb +27 -7
- data/lib/formatting/version.rb +1 -1
- data/spec/currency_spec.rb +12 -12
- data/spec/number_spec.rb +76 -16
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fee06b2bd9c054f9a95f99d0c32dbe00062eba7
|
4
|
+
data.tar.gz: 009dad129635e6941f3dfcfc1fde91aef601c13b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cdb71265b3c0566c73e414ee907e2335cc4533042234462e82aadfc19a159558a36014ee5f53a39eaa274674cc82126dd0b0d5fe6f67c6e84af4c5164d5eb40
|
7
|
+
data.tar.gz: 9a446f71f65a60f67effac58f61ce6853c555a3336dec342d12522d93fafbb4d073405e1982eb42abbc7e2620c3d0ea149389f00d939c386845361bd17f5f950
|
data/README.md
CHANGED
@@ -79,8 +79,6 @@ Or install it yourself as:
|
|
79
79
|
|
80
80
|
## TODO
|
81
81
|
|
82
|
-
*
|
83
|
-
* Don't depend on `i18n`?
|
84
|
-
* Rename? This name is boring and also generic enough that collisions seem likely.
|
82
|
+
* Use real i18n in specs so they're less fragile and ugly
|
85
83
|
* Document options
|
86
|
-
*
|
84
|
+
* Rename? This name is boring and also generic enough that collisions seem likely.
|
data/formatting.gemspec
CHANGED
@@ -17,8 +17,6 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "i18n"
|
21
|
-
|
22
20
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
21
|
spec.add_development_dependency "rake"
|
24
22
|
spec.add_development_dependency "rspec"
|
data/lib/formatting/currency.rb
CHANGED
data/lib/formatting/number.rb
CHANGED
@@ -3,13 +3,15 @@ module Formatting
|
|
3
3
|
def format_number(number, opts = {})
|
4
4
|
opts = Formatting.defaults.merge(opts)
|
5
5
|
|
6
|
-
thousands_separator = opts.fetch(:thousands_separator
|
7
|
-
decimal_separator = opts.fetch(:decimal_separator
|
8
|
-
round = opts.fetch(:round,
|
9
|
-
min_decimals = opts.fetch(:min_decimals,
|
6
|
+
thousands_separator = opts.fetch(:thousands_separator) { default_thousands_separator }
|
7
|
+
decimal_separator = opts.fetch(:decimal_separator) { default_decimal_separator }
|
8
|
+
round = opts.fetch(:round, 2)
|
9
|
+
min_decimals = opts.fetch(:min_decimals, 2)
|
10
10
|
explicit_sign = opts.fetch(:explicit_sign, false)
|
11
11
|
blank_when_zero = opts.fetch(:blank_when_zero, false)
|
12
12
|
|
13
|
+
has_decimals = number.to_s.include?(".")
|
14
|
+
|
13
15
|
if blank_when_zero
|
14
16
|
return "" if number.zero?
|
15
17
|
end
|
@@ -18,11 +20,10 @@ module Formatting
|
|
18
20
|
number = 0 if number.zero?
|
19
21
|
|
20
22
|
if round
|
21
|
-
number = number.round(round)
|
23
|
+
number = number.round(round) if has_decimals
|
22
24
|
end
|
23
25
|
|
24
26
|
integer, decimals = number.to_s.split(".")
|
25
|
-
decimals ||= "0"
|
26
27
|
|
27
28
|
integer.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{thousands_separator}")
|
28
29
|
|
@@ -31,10 +32,29 @@ module Formatting
|
|
31
32
|
end
|
32
33
|
|
33
34
|
if min_decimals
|
35
|
+
decimals ||= "0"
|
34
36
|
decimals = decimals.ljust(min_decimals, "0")
|
35
37
|
end
|
36
38
|
|
37
|
-
[integer, decimals].join(decimal_separator)
|
39
|
+
[integer, decimals].compact.join(decimal_separator)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def default_thousands_separator
|
45
|
+
t_format(:delimiter, NON_BREAKING_SPACE)
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_decimal_separator
|
49
|
+
t_format(:separator, ".")
|
50
|
+
end
|
51
|
+
|
52
|
+
def t_format(key, default)
|
53
|
+
if defined?(I18n)
|
54
|
+
I18n.t(key, scope: "number.format", default: default)
|
55
|
+
else
|
56
|
+
default
|
57
|
+
end
|
38
58
|
end
|
39
59
|
end
|
40
60
|
end
|
data/lib/formatting/version.rb
CHANGED
data/spec/currency_spec.rb
CHANGED
@@ -14,12 +14,12 @@ describe Formatting do
|
|
14
14
|
|
15
15
|
context "method signature" do
|
16
16
|
it "can take a record and a value" do
|
17
|
-
expect_formatted(item, 1).to eq space_to_nbsp("1.
|
17
|
+
expect_formatted(item, 1).to eq space_to_nbsp("1.00")
|
18
18
|
end
|
19
19
|
|
20
20
|
it "can take a record and a method name" do
|
21
21
|
item.stub(price: 2)
|
22
|
-
expect_formatted(item, :price).to eq space_to_nbsp("2.
|
22
|
+
expect_formatted(item, :price).to eq space_to_nbsp("2.00")
|
23
23
|
end
|
24
24
|
|
25
25
|
it "complains if the 'record' looks like a method name (a likely mistake)" do
|
@@ -27,8 +27,8 @@ describe Formatting do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
31
|
-
expect_formatted(item, nil).to
|
30
|
+
it "returns an empty string for nil" do
|
31
|
+
expect_formatted(item, nil).to eq ""
|
32
32
|
end
|
33
33
|
|
34
34
|
context "formatting" do
|
@@ -42,7 +42,7 @@ describe Formatting do
|
|
42
42
|
|
43
43
|
it "applies default options" do
|
44
44
|
Formatting.defaults = { currency: "FOO" }
|
45
|
-
expect_formatted(item, 1).to eq space_to_nbsp("1.
|
45
|
+
expect_formatted(item, 1).to eq space_to_nbsp("1.00 FOO")
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -53,34 +53,34 @@ describe Formatting do
|
|
53
53
|
|
54
54
|
it "is read from the record's #currency if present" do
|
55
55
|
item.stub(currency: "SEK")
|
56
|
-
expect_formatted(item, 1).to eq space_to_nbsp("1.
|
56
|
+
expect_formatted(item, 1).to eq space_to_nbsp("1.00 SEK")
|
57
57
|
end
|
58
58
|
|
59
59
|
it "is not added if the record's #currency is blank" do
|
60
60
|
item.stub(currency: "")
|
61
|
-
expect_formatted(item, 1).to eq space_to_nbsp("1.
|
61
|
+
expect_formatted(item, 1).to eq space_to_nbsp("1.00")
|
62
62
|
end
|
63
63
|
|
64
64
|
it "is not added if the record does not respond to #currency" do
|
65
|
-
expect_formatted(item, 1).to eq space_to_nbsp("1.
|
65
|
+
expect_formatted(item, 1).to eq space_to_nbsp("1.00")
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
69
|
context "format string option" do
|
70
70
|
it "is used if provided" do
|
71
71
|
expect_formatted(item, 123, format: "C: <currency> A: <amount>", currency: "XYZ").
|
72
|
-
to eq space_to_nbsp("C: XYZ A: 123.
|
72
|
+
to eq space_to_nbsp("C: XYZ A: 123.00")
|
73
73
|
end
|
74
74
|
|
75
75
|
it "will have spaces turned into non-breaking spaces" do
|
76
76
|
expect_actual = expect_formatted(item, 123, format: "<amount> <currency>", currency: "XYZ")
|
77
|
-
expect_actual.to eq space_to_nbsp("123.
|
78
|
-
expect_actual.not_to eq "123.
|
77
|
+
expect_actual.to eq space_to_nbsp("123.00 XYZ")
|
78
|
+
expect_actual.not_to eq "123.00 XYZ"
|
79
79
|
end
|
80
80
|
|
81
81
|
it "is stripped" do
|
82
82
|
expect_formatted(item, 123, format: "<amount> <currency>", currency: nil).
|
83
|
-
to eq space_to_nbsp("123.
|
83
|
+
to eq space_to_nbsp("123.00")
|
84
84
|
end
|
85
85
|
end
|
86
86
|
end
|
data/spec/number_spec.rb
CHANGED
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
describe Formatting do
|
12
12
|
describe ".format_number" do
|
13
|
-
it "
|
13
|
+
it "formats a number" do
|
14
14
|
expect_formatted(1234567.89).to eq space_to_nbsp("1 234 567.89")
|
15
15
|
end
|
16
16
|
|
@@ -19,19 +19,70 @@ describe Formatting do
|
|
19
19
|
expect_formatted(12.3456789).to eq "12.346"
|
20
20
|
end
|
21
21
|
|
22
|
+
context "thousands separator" do
|
23
|
+
context "with I18n" do
|
24
|
+
let(:i18n) { stub_const("I18n", double) }
|
25
|
+
|
26
|
+
it "uses I18n.t('number.format.delimiter') if present" do
|
27
|
+
allow(i18n).to receive(:t).with(:separator, instance_of(Hash))
|
28
|
+
expect(i18n).to receive(:t).
|
29
|
+
with(:delimiter, scope: "number.format", default: space_to_nbsp(" ")).
|
30
|
+
and_return(";")
|
31
|
+
expect_formatted(1234).to include "1;234"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "without I18n" do
|
36
|
+
it "defaults to a non-breaking space" do
|
37
|
+
expect_formatted(1234).to include space_to_nbsp("1 234")
|
38
|
+
expect_formatted(1234).not_to include "1 234"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can be customized" do
|
43
|
+
expect_formatted(1234, thousands_separator: ":").to include "1:234"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "decimal separator" do
|
48
|
+
context "with I18n" do
|
49
|
+
let(:i18n) { stub_const("I18n", double) }
|
50
|
+
|
51
|
+
it "uses I18n.t('number.format.separator') if present" do
|
52
|
+
allow(i18n).to receive(:t).with(:delimiter, instance_of(Hash))
|
53
|
+
expect(i18n).to receive(:t).with(:separator, scope: "number.format", default: ".").and_return(";")
|
54
|
+
expect_formatted(1.2).to eq "1;20"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "without I18n" do
|
59
|
+
it "defaults to ." do
|
60
|
+
expect_formatted(1.2).to eq "1.20"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "can be customized" do
|
65
|
+
expect_formatted(1.2, decimal_separator: ":").to eq "1:20"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
22
69
|
context "rounding" do
|
23
|
-
it "
|
24
|
-
expect_formatted(12.3456789).to eq "12.
|
70
|
+
it "rounds to 2 decimals by defaults" do
|
71
|
+
expect_formatted(12.3456789).to eq "12.35"
|
25
72
|
end
|
26
73
|
|
27
74
|
it "rounds to the given number of decimals" do
|
28
|
-
expect_formatted(12.3456789, round:
|
75
|
+
expect_formatted(12.3456789, round: 3).to eq "12.346"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "doesn't round if given false" do
|
79
|
+
expect_formatted(12.3456789, round: false).to eq "12.3456789"
|
29
80
|
end
|
30
81
|
end
|
31
82
|
|
32
83
|
context "blanking when zero" do
|
33
84
|
it "does not happen by default" do
|
34
|
-
expect_formatted(0).to eq "0.
|
85
|
+
expect_formatted(0).to eq "0.00"
|
35
86
|
end
|
36
87
|
|
37
88
|
it "can be enforced" do
|
@@ -41,31 +92,40 @@ describe Formatting do
|
|
41
92
|
|
42
93
|
|
43
94
|
context "minimum number of decimals" do
|
44
|
-
it "
|
45
|
-
expect_formatted(12).to eq "12.
|
46
|
-
expect_formatted(12.3).to eq "12.
|
95
|
+
it "defaults to two decimals" do
|
96
|
+
expect_formatted(12).to eq "12.00"
|
97
|
+
expect_formatted(12.3).to eq "12.30"
|
47
98
|
end
|
48
99
|
|
49
100
|
it "can be enforced" do
|
50
|
-
expect_formatted(12.3, min_decimals:
|
101
|
+
expect_formatted(12.3, min_decimals: 3).to eq "12.300"
|
51
102
|
end
|
103
|
+
|
104
|
+
it "is not enforced if given false" do
|
105
|
+
expect_formatted(0, min_decimals: false).to eq "0"
|
106
|
+
expect_formatted(12, min_decimals: false).to eq "12"
|
107
|
+
expect_formatted(0.0, min_decimals: false).to eq "0.0"
|
108
|
+
expect_formatted(12.0, min_decimals: false).to eq "12.0"
|
109
|
+
expect_formatted(12.3, min_decimals: false).to eq "12.3"
|
110
|
+
end
|
111
|
+
|
52
112
|
end
|
53
113
|
|
54
114
|
context "explicit sign" do
|
55
115
|
it "is not included by default" do
|
56
|
-
expect_formatted(1).to eq "1.
|
57
|
-
expect_formatted(0).to eq "0.
|
58
|
-
expect_formatted(-1).to eq "-1.
|
116
|
+
expect_formatted(1).to eq "1.00"
|
117
|
+
expect_formatted(0).to eq "0.00"
|
118
|
+
expect_formatted(-1).to eq "-1.00"
|
59
119
|
end
|
60
120
|
|
61
121
|
it "never shows 0 as negative" do
|
62
|
-
expect_formatted(-0.0).to eq "0.
|
122
|
+
expect_formatted(-0.0).to eq "0.00"
|
63
123
|
end
|
64
124
|
|
65
125
|
it "can be enforced" do
|
66
|
-
expect_formatted(1, explicit_sign: true).to eq "+1.
|
67
|
-
expect_formatted(0, explicit_sign: true).to eq "0.
|
68
|
-
expect_formatted(-1, explicit_sign: true).to eq "-1.
|
126
|
+
expect_formatted(1, explicit_sign: true).to eq "+1.00"
|
127
|
+
expect_formatted(0, explicit_sign: true).to eq "0.00"
|
128
|
+
expect_formatted(-1, explicit_sign: true).to eq "-1.00"
|
69
129
|
end
|
70
130
|
end
|
71
131
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formatting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: i18n
|
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
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|