friendly_numbers 0.1.0 → 0.2.0

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: d0ffa8e616e2a56a0eb4a48975b9cc33fd038ea9
4
- data.tar.gz: 53f0f0beb3ed999a9b68b7609c549a53e4448b5c
3
+ metadata.gz: 083f9bc78bce7405ff3be9b4c98d54f513d51656
4
+ data.tar.gz: d8ae616f668ef23ca2cda1e03747a698c5e9acf0
5
5
  SHA512:
6
- metadata.gz: eb9ff0ec92c89fcf2c9ce0db77108ede76abec1cb33ea2a971d9c200e2b982a216953bc2d74d767a3857e08a50055c89e3b54fa7389229ebb5bfda170c721f08
7
- data.tar.gz: 5757ad5cfb357191bfa5e416f6a682d0e2f727940898930533bcca507404b095deff04f24e11d48a173d39579b83e7eb8679d9d429b506b3d060691c6984d525
6
+ metadata.gz: fce62b13582d6a10918e2678dbdc261ec1ebb084f6254b21a37ebc8c038475902b63729ae9ad01f8db323b16c441f80e89d17036a9d0bb768aa8a147a293d4f8
7
+ data.tar.gz: ff0a37dbcf10e3252d09259fb95db65f98f51ea18f0b34d9e6844ac76d78200ad61bc4617c995ac7feaf9cef09eec22afbc8e4d3f40e6c1e4140a7d759b7be92
@@ -1,17 +1,37 @@
1
1
  require "friendly_numbers/version"
2
2
  require "friendly_numbers/number_to_currency"
3
3
  require "friendly_numbers/number_to_human_size"
4
+ require "friendly_numbers/number_to_percentage"
4
5
 
5
6
  module FriendlyNumbers
7
+ module_function
8
+
9
+ # Convert a Number to a currency-formatted String
10
+ #
11
+ # Options:
12
+ # precision:: number of decimal places
13
+ # unit:: symbol to prefix value
14
+ # separator:: symbol to separate thousands
6
15
  def number_to_currency(value, options = {})
7
16
  NumberToCurrency.convert(value, options)
8
17
  end
9
- module_function :number_to_currency
10
18
 
19
+ # Convert a Number to a human-readable size of data
20
+ #
21
+ # Options:
22
+ # precision:: number of decimal places
23
+ # scales:: Array of abbreviations in order
11
24
  def number_to_human_size(bytes, options = {})
12
25
  NumberToHumanSize.convert(bytes, options)
13
26
  end
14
- module_function :number_to_human_size
27
+
28
+ # Convert a Number to a percentage
29
+ #
30
+ # Options:
31
+ # precision:: number of decimal places
32
+ def number_to_percentage(value, options = {})
33
+ NumberToPercentage.convert(value, options)
34
+ end
15
35
  end
16
36
 
17
37
  Friendly = FriendlyNumbers
@@ -1,14 +1,31 @@
1
1
  module FriendlyNumbers
2
2
  module NumberToCurrency
3
- extend self
3
+ DEFAULTS = {
4
+ precision: 2,
5
+ unit: "$",
6
+ separator: ","
7
+ }
4
8
 
5
- def convert(value, options)
6
- options = {
7
- precision: 2,
8
- unit: "$"
9
- }.merge(options)
9
+ module_function
10
10
 
11
- format("%s%.#{options[:precision]}f", options[:unit], value)
11
+ def convert(value, options) # :nodoc:
12
+ options = DEFAULTS.merge(options)
13
+
14
+ whole, part = value.divmod(1)
15
+
16
+ parted = partition(whole, options[:separator])
17
+
18
+ options[:unit] +
19
+ parted +
20
+ format("%.#{options[:precision]}f", part)[1..-1] # Strip leading 0
21
+ end
22
+
23
+ def partition(whole, separator)
24
+ whole.to_s.chars.reverse
25
+ .each_slice(3)
26
+ .map(&:join)
27
+ .join(separator)
28
+ .reverse
12
29
  end
13
30
  end
14
31
  end
@@ -1,12 +1,14 @@
1
1
  module FriendlyNumbers
2
2
  module NumberToHumanSize
3
- extend self
3
+ DEFAULTS = {
4
+ precision: 3,
5
+ scales: %w(Bytes KB MB GB TB PB EB ZB YB)
6
+ }
4
7
 
5
- def convert(bytes, options)
6
- options = {
7
- precision: 3,
8
- scales: %w(Bytes KB MB GB TB PB)
9
- }.merge(options)
8
+ module_function
9
+
10
+ def convert(bytes, options) # :nodoc:
11
+ options = DEFAULTS.merge(options)
10
12
 
11
13
  value, scale = calculate_smallest_value_with_scale(bytes)
12
14
  precision = zero_precision_for_zero_remainder(value, options[:precision])
@@ -14,7 +16,7 @@ module FriendlyNumbers
14
16
  format("%.#{precision}f %s", value, options[:scales][scale])
15
17
  end
16
18
 
17
- def calculate_smallest_value_with_scale(value)
19
+ def calculate_smallest_value_with_scale(value) # :nodoc:
18
20
  scale = 0
19
21
  while value >= 1024
20
22
  value /= 1024.0
@@ -23,7 +25,7 @@ module FriendlyNumbers
23
25
  [value, scale]
24
26
  end
25
27
 
26
- def zero_precision_for_zero_remainder(value, default_precision = 0)
28
+ def zero_precision_for_zero_remainder(value, default_precision = 0) # :nodoc:
27
29
  if (value % 1).zero?
28
30
  0
29
31
  else
@@ -0,0 +1,15 @@
1
+ module FriendlyNumbers
2
+ module NumberToPercentage
3
+ DEFAULTS = {
4
+ precision: 2
5
+ }
6
+
7
+ module_function
8
+
9
+ def convert(value, options) # :nodoc:
10
+ options = DEFAULTS.merge(options)
11
+
12
+ format("%.#{options[:precision]}f\%", value * 100)
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module FriendlyNumbers
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_numbers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Daniels
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-03 00:00:00.000000000 Z
11
+ date: 2017-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ files:
75
75
  - lib/friendly_numbers.rb
76
76
  - lib/friendly_numbers/number_to_currency.rb
77
77
  - lib/friendly_numbers/number_to_human_size.rb
78
+ - lib/friendly_numbers/number_to_percentage.rb
78
79
  - lib/friendly_numbers/version.rb
79
80
  homepage: https://github.com/adam12/friendly_numbers
80
81
  licenses: