formatting 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/formatting/number.rb +1 -1
- data/lib/formatting/percent.rb +17 -3
- data/lib/formatting/version.rb +1 -1
- data/spec/percent_spec.rb +16 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31a50e279bf40d7bd3e6a284ab518d258d0f1120
|
4
|
+
data.tar.gz: 66448a804ff7bc12a5dfaba0dbcfe9643221b66a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f19832f4814387e8397459f2d7c038684832235987d7c2d05c8ddc00a66f76358adfc9d2052f605a5b280796f1adb4907e328d09deb5f671be7ca3fdea529862
|
7
|
+
data.tar.gz: e95ab8b878a6f1b081ee28fd261385aa13e7229019467ce9ea92bde2ae0b02ef3ac7d71bc1b0c8a9a53c3aa3abc7f7e843cce167fd47b931a840874b48baa0de
|
data/README.md
CHANGED
@@ -99,7 +99,11 @@ Formatting.format_percent(12.3) # => "12.3%"
|
|
99
99
|
|
100
100
|
#### Options
|
101
101
|
|
102
|
-
Passes on all the number options
|
102
|
+
Passes on all the number options and also takes these:
|
103
|
+
|
104
|
+
name | default | explanation
|
105
|
+
----------------|---------------------------------------------------|------------
|
106
|
+
format | `"<number>%"` or `<number> %` depending on locale | A format string.
|
103
107
|
|
104
108
|
|
105
109
|
## Installation
|
data/lib/formatting/number.rb
CHANGED
data/lib/formatting/percent.rb
CHANGED
@@ -1,12 +1,26 @@
|
|
1
|
-
# http://en.wikipedia.org/wiki/Percent_sign#Spacing
|
2
|
-
|
3
1
|
require "attr_extras"
|
4
2
|
|
5
3
|
module Formatting
|
6
4
|
module Percent
|
7
5
|
def format_percent(number, opts = {})
|
6
|
+
format_string = opts.fetch(:format) { default_percent_format_string }
|
8
7
|
formatted_number = Formatting.format_number(number, opts)
|
9
|
-
"
|
8
|
+
format_string.gsub("<number>", formatted_number)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# http://en.wikipedia.org/wiki/Percent_sign#Spacing
|
14
|
+
# Rails i18n doesn't have a conventional format string for this.
|
15
|
+
def default_percent_format_string
|
16
|
+
locale = defined?(I18n) && I18n.locale
|
17
|
+
|
18
|
+
case locale
|
19
|
+
when :sv, :fi, :fr, :de
|
20
|
+
"<number>#{Formatting::NON_BREAKING_SPACE}%"
|
21
|
+
else
|
22
|
+
"<number>%"
|
23
|
+
end
|
10
24
|
end
|
11
25
|
end
|
12
26
|
end
|
data/lib/formatting/version.rb
CHANGED
data/spec/percent_spec.rb
CHANGED
@@ -14,7 +14,22 @@ describe Formatting, ".format_percent" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "passes on number formatting options" do
|
17
|
-
expect_formatted(1.567, round:
|
17
|
+
expect_formatted(1.567, round: 1).to include "1.6"
|
18
|
+
end
|
19
|
+
|
20
|
+
context "format string option" do
|
21
|
+
it "is used if provided" do
|
22
|
+
expect_formatted(1, format: "%<number>%").to eq "%1.00%"
|
23
|
+
end
|
24
|
+
|
25
|
+
context "if I18n.locale is available" do
|
26
|
+
let(:i18n) { stub_const("I18n", double) }
|
27
|
+
|
28
|
+
it "defaults sensibly for some locales that require spacing the sign" do
|
29
|
+
i18n.stub(locale: :sv)
|
30
|
+
expect_formatted(1).to eq space_to_nbsp("1.00 %")
|
31
|
+
end
|
32
|
+
end
|
18
33
|
end
|
19
34
|
|
20
35
|
def expect_formatted(number, opts = {})
|