normalizy 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d91babea23ed3c9c18a9aa9629da1139d1f12c55
4
- data.tar.gz: 7ab05fe0149743bf72ea4c50b8095e029a11158d
3
+ metadata.gz: 1803f9ca8bad714b7d44f99c1d685b3cf79bea7d
4
+ data.tar.gz: c5c50b280313e3903e1c05e441d9d4f628571083
5
5
  SHA512:
6
- metadata.gz: cb83724af9af4b195de644457cf7928ea71b218a77ed15d2ae5f822fffa5065531f8cb5a6d65b28a38048d1d238a43bd89894639634729c2dbff19d01f9c93a7
7
- data.tar.gz: 5852ecb8103d2fa5849a61919216c297a493d500d7f41d00059c20abb62996c8c398788ccf1ce637af299b0878775846a970e346471a4c04adcf6b215ed104c4
6
+ metadata.gz: 6142ea4c78fc22dc2fafa71be697768f60d39a0abc93c43f931d6bd9794db0139851151d55cc53076c32779b54d12275eb27b99f6b29862bdd4f9859dfdc6ef9
7
+ data.tar.gz: 6f308e9cfaafceda8495e7baecefe2bce8af421938bd4205dc53433b7d2fa2dffce33c924c5170c30dfe171c1d440e5dc1844efde90a19e441165fadccfd3de4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v1.0.1
2
+
3
+ - Fixes:
4
+ - When `type` options was `cents` and had no decimal on number, extra decimal were added.
5
+
1
6
  ## v1.0.0
2
7
 
3
8
  - Changes:
data/README.md CHANGED
@@ -32,11 +32,11 @@ On your model, just add `normalizy` callback with the attribute you want to norm
32
32
 
33
33
  ```ruby
34
34
  class User < ApplicationRecord
35
- normalizy :name, with: :strip
35
+ normalizy :name, with: :downcase
36
36
  end
37
37
  ```
38
38
 
39
- Now some email like ` myemail@example.com ` will be saved as `email@example.com`.
39
+ Now some email like `MyEmail@Example.com` will be saved as `myemail@example.com`.
40
40
 
41
41
  ## Filters
42
42
 
@@ -11,8 +11,13 @@ module Normalizy
11
11
 
12
12
  return nil if value.blank?
13
13
 
14
- value = "%0.#{precision(options)}f" % [value.sub(separator(options), '.')]
15
- value = value.delete('.') if cents?(options)
14
+ if cents?(options)
15
+ if value.include? separator(options)
16
+ value = precisioned(value, options).delete('.')
17
+ end
18
+ else
19
+ value = precisioned(value, options)
20
+ end
16
21
 
17
22
  return value.send(options[:cast]) if options[:cast]
18
23
 
@@ -29,6 +34,10 @@ module Normalizy
29
34
  options.fetch :precision, I18n.t('currency.format.precision', default: 2)
30
35
  end
31
36
 
37
+ def precisioned(value, options)
38
+ "%0.#{precision(options)}f" % [value.sub(separator(options), '.')]
39
+ end
40
+
32
41
  def separator(options)
33
42
  options.fetch :separator, I18n.t('currency.format.separator', default: '.')
34
43
  end
@@ -11,8 +11,13 @@ module Normalizy
11
11
 
12
12
  return nil if value.blank?
13
13
 
14
- value = "%0.#{precision(options)}f" % [value.sub(separator(options), '.')]
15
- value = value.delete('.') if cents?(options)
14
+ if cents?(options)
15
+ if value.include? separator(options)
16
+ value = precisioned(value, options).delete('.')
17
+ end
18
+ else
19
+ value = precisioned(value, options)
20
+ end
16
21
 
17
22
  return value.send(options[:cast]) if options[:cast]
18
23
 
@@ -29,6 +34,10 @@ module Normalizy
29
34
  options.fetch :precision, I18n.t('percentage.format.precision', default: 2)
30
35
  end
31
36
 
37
+ def precisioned(value, options)
38
+ "%0.#{precision(options)}f" % [value.sub(separator(options), '.')]
39
+ end
40
+
32
41
  def separator(options)
33
42
  options.fetch :separator, I18n.t('percentage.format.separator', default: '.')
34
43
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Normalizy
4
- VERSION = '1.0.0'.freeze
4
+ VERSION = '1.0.1'.freeze
5
5
  end
@@ -3,7 +3,7 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Normalizy::Filters::Money do
6
- context 'with default options' do
6
+ describe 'default options' do
7
7
  it { expect(subject.call('')).to eq nil }
8
8
 
9
9
  it { expect(subject.call(1)).to be 1 }
@@ -13,6 +13,8 @@ RSpec.describe Normalizy::Filters::Money do
13
13
  it { expect(subject.call(10_300.70)).to be 10_300.70 }
14
14
 
15
15
  it { expect(subject.call('0')).to eq '0.00' }
16
+ it { expect(subject.call('100')).to eq '100.00' }
17
+ it { expect(subject.call('100.0')).to eq '100.00' }
16
18
  it { expect(subject.call('1.70')).to eq '1.70' }
17
19
  it { expect(subject.call('103.70')).to eq '103.70' }
18
20
  it { expect(subject.call('1030.70')).to eq '1030.70' }
@@ -26,18 +28,104 @@ RSpec.describe Normalizy::Filters::Money do
26
28
  it { expect(subject.call('$ 10300.70')).to eq '10300.70' }
27
29
  end
28
30
 
29
- context 'with :cast' do
30
- context 'when value has two decimal precision' do
31
- it { expect(subject.call('1.70' , cast: :to_f)).to be 1.7 }
32
- it { expect(subject.call('103.70' , cast: :to_f)).to be 103.7 }
33
- it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
34
- it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
31
+ describe 'type' do
32
+ it { expect(subject.call('', type: :cents)).to eq nil }
35
33
 
36
- it { expect(subject.call('$ 1' , cast: :to_f)).to be 1.0 }
37
- it { expect(subject.call('$ 1.70' , cast: :to_f)).to be 1.7 }
38
- it { expect(subject.call('$ 103.70' , cast: :to_f)).to be 103.7 }
39
- it { expect(subject.call('$ 1030.70' , cast: :to_f)).to be 1030.7 }
40
- it { expect(subject.call('$ 10300.70', cast: :to_f)).to be 10_300.7 }
34
+ it { expect(subject.call(1 , type: :cents)).to be 1 }
35
+ it { expect(subject.call(1.70 , type: :cents)).to be 1.70 }
36
+ it { expect(subject.call(103.70 , type: :cents)).to be 103.70 }
37
+ it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
38
+ it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
39
+
40
+ context 'with different separator' do
41
+ it { expect(subject.call('1,7', separator: ',', type: :cents)).to eq '170' }
42
+ end
43
+
44
+ context 'with no decimal precision' do
45
+ it { expect(subject.call('1' , type: :cents)).to eq '1' }
46
+ it { expect(subject.call('10' , type: :cents)).to eq '10' }
47
+ it { expect(subject.call('100', type: :cents)).to eq '100' }
48
+
49
+ context 'with float :cast' do
50
+ it { expect(subject.call('1' , cast: :to_f, type: :cents)).to eq 1.0 }
51
+ it { expect(subject.call('10' , cast: :to_f, type: :cents)).to eq 10.0 }
52
+ it { expect(subject.call('100', cast: :to_f, type: :cents)).to eq 100.0 }
53
+ end
54
+ end
55
+
56
+ context 'with one decimal precision' do
57
+ it { expect(subject.call('1.7' , type: :cents)).to eq '170' }
58
+ it { expect(subject.call('103.7' , type: :cents)).to eq '10370' }
59
+ it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
60
+ it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
61
+
62
+ it { expect(subject.call('$ 1.7' , type: :cents)).to eq '170' }
63
+ it { expect(subject.call('$ 103.7' , type: :cents)).to eq '10370' }
64
+ it { expect(subject.call('$ 1030.7' , type: :cents)).to eq '103070' }
65
+ it { expect(subject.call('$ 10300.7', type: :cents)).to eq '1030070' }
66
+
67
+ context 'with float :cast' do
68
+ it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
69
+ it { expect(subject.call('103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
70
+ it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
71
+ it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
72
+
73
+ it { expect(subject.call('$ 1.7' , cast: :to_f, type: :cents)).to be 170.0 }
74
+ it { expect(subject.call('$ 103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
75
+ it { expect(subject.call('$ 1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
76
+ it { expect(subject.call('$ 10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
77
+ end
78
+ end
79
+
80
+ context 'with two decimal precision' do
81
+ it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
82
+ it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
83
+ it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
84
+ it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
85
+
86
+ it { expect(subject.call('$ 1.70' , type: :cents)).to eq '170' }
87
+ it { expect(subject.call('$ 103.70' , type: :cents)).to eq '10370' }
88
+ it { expect(subject.call('$ 1030.70' , type: :cents)).to eq '103070' }
89
+ it { expect(subject.call('$ 10300.70', type: :cents)).to eq '1030070' }
90
+
91
+ context 'with float :cast' do
92
+ it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
93
+ it { expect(subject.call('103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
94
+ it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
95
+ it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
96
+
97
+ it { expect(subject.call('$ 1.70' , cast: :to_f, type: :cents)).to be 170.0 }
98
+ it { expect(subject.call('$ 103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
99
+ it { expect(subject.call('$ 1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
100
+ it { expect(subject.call('$ 10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
101
+ end
102
+ end
103
+ end
104
+
105
+ describe 'separator' do
106
+ context 'provided inline' do
107
+ it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
108
+ end
109
+
110
+ context 'provided I18n' do
111
+ before do
112
+ allow(I18n).to receive(:t).with('currency.format.separator', default: '.') { 'x' }
113
+ allow(I18n).to receive(:t).with('currency.format.precision', default: 2) { 2 }
114
+ end
115
+
116
+ it { expect(subject.call('1x2')).to eq '1.20' }
117
+ end
118
+
119
+ context 'when not provided' do
120
+ it { expect(subject.call('1.2')).to eq '1.20' }
121
+ end
122
+ end
123
+
124
+ describe 'cast' do
125
+ context 'with no decimal precision' do
126
+ it { expect(subject.call('1' , cast: :to_f)).to eq 1.0 }
127
+ it { expect(subject.call('10' , cast: :to_f)).to eq 10.0 }
128
+ it { expect(subject.call('100', cast: :to_f)).to eq 100.0 }
41
129
  end
42
130
 
43
131
  context 'when value has one decimal precision' do
@@ -64,97 +152,17 @@ RSpec.describe Normalizy::Filters::Money do
64
152
  end
65
153
  end
66
154
 
67
- context 'with no decimal precision' do
68
- it { expect(subject.call('1', type: :cents)).to eq '100' }
69
- end
70
- end
71
-
72
- context 'with :type options' do
73
- context 'as :cents' do
74
- it { expect(subject.call('', type: :cents)).to eq nil }
75
-
76
- it { expect(subject.call(1 , type: :cents)).to be 1 }
77
- it { expect(subject.call(1.70 , type: :cents)).to be 1.70 }
78
- it { expect(subject.call(103.70 , type: :cents)).to be 103.70 }
79
- it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
80
- it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
81
-
82
- context 'with different separator' do
83
- it { expect(subject.call('1,7', separator: ',', type: :cents)).to eq '170' }
84
- end
85
-
86
- context 'with two decimal precision' do
87
- it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
88
- it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
89
- it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
90
- it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
91
-
92
- it { expect(subject.call('$ 1' , type: :cents)).to eq '100' }
93
- it { expect(subject.call('$ 1.70' , type: :cents)).to eq '170' }
94
- it { expect(subject.call('$ 103.70' , type: :cents)).to eq '10370' }
95
- it { expect(subject.call('$ 1030.70' , type: :cents)).to eq '103070' }
96
- it { expect(subject.call('$ 10300.70', type: :cents)).to eq '1030070' }
97
-
98
- context 'with :cast' do
99
- it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
100
- it { expect(subject.call('103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
101
- it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
102
- it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
103
-
104
- it { expect(subject.call('$ 1' , cast: :to_f, type: :cents)).to be 100.0 }
105
- it { expect(subject.call('$ 1.70' , cast: :to_f, type: :cents)).to be 170.0 }
106
- it { expect(subject.call('$ 103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
107
- it { expect(subject.call('$ 1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
108
- it { expect(subject.call('$ 10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
109
- end
110
- end
111
-
112
- context 'with one decimal precision' do
113
- it { expect(subject.call('1.7' , type: :cents)).to eq '170' }
114
- it { expect(subject.call('103.7' , type: :cents)).to eq '10370' }
115
- it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
116
- it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
117
-
118
- it { expect(subject.call('$ 1.7' , type: :cents)).to eq '170' }
119
- it { expect(subject.call('$ 103.7' , type: :cents)).to eq '10370' }
120
- it { expect(subject.call('$ 1030.7' , type: :cents)).to eq '103070' }
121
- it { expect(subject.call('$ 10300.7', type: :cents)).to eq '1030070' }
122
-
123
- context 'with :cast' do
124
- it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
125
- it { expect(subject.call('103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
126
- it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
127
- it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
128
-
129
- it { expect(subject.call('$ 1.7' , cast: :to_f, type: :cents)).to be 170.0 }
130
- it { expect(subject.call('$ 103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
131
- it { expect(subject.call('$ 1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
132
- it { expect(subject.call('$ 10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
133
- end
134
- end
135
-
136
- context 'with no decimal precision' do
137
- it { expect(subject.call('1', type: :cents)).to eq '100' }
138
- end
139
- end
140
- end
141
-
142
- context 'with :separator options' do
143
- context 'provided via options rule' do
144
- it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
145
- end
146
-
147
- context 'provided I18n' do
148
- before do
149
- allow(I18n).to receive(:t).with('currency.format.separator', default: '.') { 'x' }
150
- allow(I18n).to receive(:t).with('currency.format.precision', default: 2) { 2 }
151
- end
152
-
153
- it { expect(subject.call('1x2')).to eq '1.20' }
154
- end
155
+ context 'when value has two decimal precision' do
156
+ it { expect(subject.call('1.70' , cast: :to_f)).to be 1.7 }
157
+ it { expect(subject.call('103.70' , cast: :to_f)).to be 103.7 }
158
+ it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
159
+ it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
155
160
 
156
- context 'when not provided' do
157
- it { expect(subject.call('1.2')).to eq '1.20' }
161
+ it { expect(subject.call('$ 1' , cast: :to_f)).to be 1.0 }
162
+ it { expect(subject.call('$ 1.70' , cast: :to_f)).to be 1.7 }
163
+ it { expect(subject.call('$ 103.70' , cast: :to_f)).to be 103.7 }
164
+ it { expect(subject.call('$ 1030.70' , cast: :to_f)).to be 1030.7 }
165
+ it { expect(subject.call('$ 10300.70', cast: :to_f)).to be 10_300.7 }
158
166
  end
159
167
  end
160
168
  end
@@ -3,7 +3,7 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Normalizy::Filters::Percent do
6
- context 'with default options' do
6
+ describe 'default options' do
7
7
  it { expect(subject.call('')).to eq nil }
8
8
 
9
9
  it { expect(subject.call(1)).to be 1 }
@@ -13,130 +13,98 @@ RSpec.describe Normalizy::Filters::Percent do
13
13
  it { expect(subject.call(10_300.70)).to be 10_300.70 }
14
14
 
15
15
  it { expect(subject.call('0')).to eq '0.00' }
16
+ it { expect(subject.call('100')).to eq '100.00' }
17
+ it { expect(subject.call('100.0')).to eq '100.00' }
16
18
  it { expect(subject.call('1.70')).to eq '1.70' }
17
19
  it { expect(subject.call('103.70')).to eq '103.70' }
18
20
  it { expect(subject.call('1030.70')).to eq '1030.70' }
19
21
  it { expect(subject.call('10300.70')).to eq '10300.70' }
20
22
 
21
- it { expect(subject.call('1 %')).to eq '1.00' }
22
- it { expect(subject.call('1.70 %')).to eq '1.70' }
23
- it { expect(subject.call('103.70 %')).to eq '103.70' }
24
- it { expect(subject.call('1030.70 %')).to eq '1030.70' }
25
- it { expect(subject.call('10300.70 %')).to eq '10300.70' }
23
+ it { expect(subject.call('$ 0.01')).to eq '0.01' }
24
+ it { expect(subject.call('$ 1')).to eq '1.00' }
25
+ it { expect(subject.call('$ 1.70')).to eq '1.70' }
26
+ it { expect(subject.call('$ 103.70')).to eq '103.70' }
27
+ it { expect(subject.call('$ 1030.70')).to eq '1030.70' }
28
+ it { expect(subject.call('$ 10300.70')).to eq '10300.70' }
26
29
  end
27
30
 
28
- context 'with :cast' do
29
- context 'when value has two decimal precision' do
30
- it { expect(subject.call('1.70' , cast: :to_f)).to be 1.7 }
31
- it { expect(subject.call('103.70' , cast: :to_f)).to be 103.7 }
32
- it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
33
- it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
34
-
35
- it { expect(subject.call('1 %' , cast: :to_f)).to be 1.0 }
36
- it { expect(subject.call('1.70 %' , cast: :to_f)).to be 1.7 }
37
- it { expect(subject.call('103.70 %' , cast: :to_f)).to be 103.7 }
38
- it { expect(subject.call('1030.70 %' , cast: :to_f)).to be 1030.7 }
39
- it { expect(subject.call('10300.70 %', cast: :to_f)).to be 10_300.7 }
40
- end
31
+ describe 'type' do
32
+ it { expect(subject.call('', type: :cents)).to eq nil }
41
33
 
42
- context 'when value has one decimal precision' do
43
- it { expect(subject.call('1.7' , cast: :to_f)).to eq 1.7 }
44
- it { expect(subject.call('103.7' , cast: :to_f)).to eq 103.7 }
45
- it { expect(subject.call('1030.7' , cast: :to_f)).to eq 1030.7 }
46
- it { expect(subject.call('10300.7', cast: :to_f)).to eq 10_300.7 }
47
-
48
- it { expect(subject.call('1.7 %' , cast: :to_f)).to eq 1.7 }
49
- it { expect(subject.call('103.7 %' , cast: :to_f)).to eq 103.7 }
50
- it { expect(subject.call('1030.7 %' , cast: :to_f)).to eq 1030.7 }
51
- it { expect(subject.call('10300.7 %', cast: :to_f)).to eq 10_300.7 }
34
+ it { expect(subject.call(1 , type: :cents)).to be 1 }
35
+ it { expect(subject.call(1.70 , type: :cents)).to be 1.70 }
36
+ it { expect(subject.call(103.70 , type: :cents)).to be 103.70 }
37
+ it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
38
+ it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
52
39
 
53
- context 'and calls cast' do
54
- it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
55
- it { expect(subject.call('103.7' , cast: :to_f)).to be 103.7 }
56
- it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
57
- it { expect(subject.call('10300.7', cast: :to_f)).to be 10_300.7 }
58
-
59
- it { expect(subject.call('1.7 %' , cast: :to_f)).to be 1.7 }
60
- it { expect(subject.call('103.7 %' , cast: :to_f)).to be 103.7 }
61
- it { expect(subject.call('1030.7 %' , cast: :to_f)).to be 1030.7 }
62
- it { expect(subject.call('10300.7 %', cast: :to_f)).to be 10_300.7 }
63
- end
40
+ context 'with different separator' do
41
+ it { expect(subject.call('1,7', separator: ',', type: :cents)).to eq '170' }
64
42
  end
65
43
 
66
44
  context 'with no decimal precision' do
67
- it { expect(subject.call('1', type: :cents)).to eq '100' }
68
- end
69
- end
70
-
71
- context 'with :type options' do
72
- context 'as :cents' do
73
- it { expect(subject.call('', type: :cents)).to eq nil }
74
-
75
- it { expect(subject.call(1 , type: :cents)).to be 1 }
76
- it { expect(subject.call(1.70 , type: :cents)).to be 1.70 }
77
- it { expect(subject.call(103.70 , type: :cents)).to be 103.70 }
78
- it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
79
- it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
80
-
81
- context 'with two decimal precision' do
82
- it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
83
- it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
84
- it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
85
- it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
86
-
87
- it { expect(subject.call('1 %' , type: :cents)).to eq '100' }
88
- it { expect(subject.call('1.70 %' , type: :cents)).to eq '170' }
89
- it { expect(subject.call('103.70 %' , type: :cents)).to eq '10370' }
90
- it { expect(subject.call('1030.70 %' , type: :cents)).to eq '103070' }
91
- it { expect(subject.call('10300.70 %', type: :cents)).to eq '1030070' }
92
-
93
- context 'with :cast' do
94
- it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
95
- it { expect(subject.call('103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
96
- it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
97
- it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
98
-
99
- it { expect(subject.call('1 %' , cast: :to_f, type: :cents)).to be 100.0 }
100
- it { expect(subject.call('1.70 %' , cast: :to_f, type: :cents)).to be 170.0 }
101
- it { expect(subject.call('103.70 %' , cast: :to_f, type: :cents)).to be 10_370.0 }
102
- it { expect(subject.call('1030.70 %' , cast: :to_f, type: :cents)).to be 103_070.0 }
103
- it { expect(subject.call('10300.70 %', cast: :to_f, type: :cents)).to be 1_030_070.0 }
104
- end
45
+ it { expect(subject.call('1' , type: :cents)).to eq '1' }
46
+ it { expect(subject.call('10' , type: :cents)).to eq '10' }
47
+ it { expect(subject.call('100', type: :cents)).to eq '100' }
48
+
49
+ context 'with float :cast' do
50
+ it { expect(subject.call('1' , cast: :to_f, type: :cents)).to eq 1.0 }
51
+ it { expect(subject.call('10' , cast: :to_f, type: :cents)).to eq 10.0 }
52
+ it { expect(subject.call('100', cast: :to_f, type: :cents)).to eq 100.0 }
105
53
  end
54
+ end
106
55
 
107
- context 'with one decimal precision' do
108
- it { expect(subject.call('1.7' , type: :cents)).to eq '170' }
109
- it { expect(subject.call('103.7' , type: :cents)).to eq '10370' }
110
- it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
111
- it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
112
-
113
- it { expect(subject.call('1.7 %' , type: :cents)).to eq '170' }
114
- it { expect(subject.call('103.7 %' , type: :cents)).to eq '10370' }
115
- it { expect(subject.call('1030.7 %' , type: :cents)).to eq '103070' }
116
- it { expect(subject.call('10300.7 %', type: :cents)).to eq '1030070' }
117
-
118
- context 'with :cast' do
119
- it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
120
- it { expect(subject.call('103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
121
- it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
122
- it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
123
-
124
- it { expect(subject.call('1.7 %' , cast: :to_f, type: :cents)).to be 170.0 }
125
- it { expect(subject.call('103.7 %' , cast: :to_f, type: :cents)).to be 10_370.0 }
126
- it { expect(subject.call('1030.7 %' , cast: :to_f, type: :cents)).to be 103_070.0 }
127
- it { expect(subject.call('10300.7 %', cast: :to_f, type: :cents)).to be 1_030_070.0 }
128
- end
56
+ context 'with one decimal precision' do
57
+ it { expect(subject.call('1.7' , type: :cents)).to eq '170' }
58
+ it { expect(subject.call('103.7' , type: :cents)).to eq '10370' }
59
+ it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
60
+ it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
61
+
62
+ it { expect(subject.call('$ 1.7' , type: :cents)).to eq '170' }
63
+ it { expect(subject.call('$ 103.7' , type: :cents)).to eq '10370' }
64
+ it { expect(subject.call('$ 1030.7' , type: :cents)).to eq '103070' }
65
+ it { expect(subject.call('$ 10300.7', type: :cents)).to eq '1030070' }
66
+
67
+ context 'with float :cast' do
68
+ it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
69
+ it { expect(subject.call('103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
70
+ it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
71
+ it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
72
+
73
+ it { expect(subject.call('$ 1.7' , cast: :to_f, type: :cents)).to be 170.0 }
74
+ it { expect(subject.call('$ 103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
75
+ it { expect(subject.call('$ 1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
76
+ it { expect(subject.call('$ 10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
129
77
  end
78
+ end
130
79
 
131
- context 'with no decimal precision' do
132
- it { expect(subject.call('1', type: :cents)).to eq '100' }
80
+ context 'with two decimal precision' do
81
+ it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
82
+ it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
83
+ it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
84
+ it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
85
+
86
+ it { expect(subject.call('$ 1.70' , type: :cents)).to eq '170' }
87
+ it { expect(subject.call('$ 103.70' , type: :cents)).to eq '10370' }
88
+ it { expect(subject.call('$ 1030.70' , type: :cents)).to eq '103070' }
89
+ it { expect(subject.call('$ 10300.70', type: :cents)).to eq '1030070' }
90
+
91
+ context 'with float :cast' do
92
+ it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
93
+ it { expect(subject.call('103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
94
+ it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
95
+ it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
96
+
97
+ it { expect(subject.call('$ 1.70' , cast: :to_f, type: :cents)).to be 170.0 }
98
+ it { expect(subject.call('$ 103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
99
+ it { expect(subject.call('$ 1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
100
+ it { expect(subject.call('$ 10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
133
101
  end
134
102
  end
135
103
  end
136
104
 
137
- context 'with :separator options' do
138
- context 'provided via options rule' do
139
- it { expect(subject.call('1-2', separator: '-')).to eq '1.20' }
105
+ describe 'separator' do
106
+ context 'provided inline' do
107
+ it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
140
108
  end
141
109
 
142
110
  context 'provided I18n' do
@@ -152,4 +120,49 @@ RSpec.describe Normalizy::Filters::Percent do
152
120
  it { expect(subject.call('1.2')).to eq '1.20' }
153
121
  end
154
122
  end
123
+
124
+ describe 'cast' do
125
+ context 'with no decimal precision' do
126
+ it { expect(subject.call('1' , cast: :to_f)).to eq 1.0 }
127
+ it { expect(subject.call('10' , cast: :to_f)).to eq 10.0 }
128
+ it { expect(subject.call('100', cast: :to_f)).to eq 100.0 }
129
+ end
130
+
131
+ context 'when value has one decimal precision' do
132
+ it { expect(subject.call('1.7' , cast: :to_f)).to eq 1.7 }
133
+ it { expect(subject.call('103.7' , cast: :to_f)).to eq 103.7 }
134
+ it { expect(subject.call('1030.7' , cast: :to_f)).to eq 1030.7 }
135
+ it { expect(subject.call('10300.7', cast: :to_f)).to eq 10_300.7 }
136
+
137
+ it { expect(subject.call('$ 1.7' , cast: :to_f)).to eq 1.7 }
138
+ it { expect(subject.call('$ 103.7' , cast: :to_f)).to eq 103.7 }
139
+ it { expect(subject.call('$ 1030.7' , cast: :to_f)).to eq 1030.7 }
140
+ it { expect(subject.call('$ 10300.7', cast: :to_f)).to eq 10_300.7 }
141
+
142
+ context 'and calls cast' do
143
+ it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
144
+ it { expect(subject.call('103.7' , cast: :to_f)).to be 103.7 }
145
+ it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
146
+ it { expect(subject.call('10300.7', cast: :to_f)).to be 10_300.7 }
147
+
148
+ it { expect(subject.call('$ 1.7' , cast: :to_f)).to be 1.7 }
149
+ it { expect(subject.call('$ 103.7' , cast: :to_f)).to be 103.7 }
150
+ it { expect(subject.call('$ 1030.7' , cast: :to_f)).to be 1030.7 }
151
+ it { expect(subject.call('$ 10300.7', cast: :to_f)).to be 10_300.7 }
152
+ end
153
+ end
154
+
155
+ context 'when value has two decimal precision' do
156
+ it { expect(subject.call('1.70' , cast: :to_f)).to be 1.7 }
157
+ it { expect(subject.call('103.70' , cast: :to_f)).to be 103.7 }
158
+ it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
159
+ it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
160
+
161
+ it { expect(subject.call('$ 1' , cast: :to_f)).to be 1.0 }
162
+ it { expect(subject.call('$ 1.70' , cast: :to_f)).to be 1.7 }
163
+ it { expect(subject.call('$ 103.70' , cast: :to_f)).to be 103.7 }
164
+ it { expect(subject.call('$ 1030.70' , cast: :to_f)).to be 1030.7 }
165
+ it { expect(subject.call('$ 10300.70', cast: :to_f)).to be 10_300.7 }
166
+ end
167
+ end
155
168
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: normalizy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Washington Botelho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-24 00:00:00.000000000 Z
11
+ date: 2017-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails