si 0.1.1 → 0.1.2
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.
- data/README.md +19 -4
- data/lib/si.rb +2 -52
- data/lib/si/constants.rb +13 -0
- data/lib/si/minimal.rb +4 -0
- data/lib/si/module.rb +49 -0
- data/lib/si/patch.rb +15 -0
- data/lib/si/version.rb +1 -1
- data/test/test_si.rb +16 -0
- metadata +6 -2
data/README.md
CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
### `si`
|
22
22
|
|
23
|
+
Express a numeric value with SI prefix.
|
24
|
+
|
23
25
|
```ruby
|
24
26
|
0.9.si # '900m'
|
25
27
|
9.si # '9'
|
@@ -50,19 +52,32 @@ Or install it yourself as:
|
|
50
52
|
9876543210000.si(:length => 5) # '9.8765T'
|
51
53
|
|
52
54
|
# For convenience, a single Fixnum is recognized as :length value
|
53
|
-
9876543210000.si(5)
|
55
|
+
9876543210000.si(5) # '9.8765T'
|
54
56
|
```
|
55
57
|
|
56
58
|
### `si_byte`
|
57
59
|
|
58
|
-
`si_byte` is simply a shorcut for
|
60
|
+
`si_byte` is simply a shorcut for `number.si(:length => length, :base => 1024, :min_exp => 0) + 'B'`.
|
59
61
|
|
60
62
|
```ruby
|
61
|
-
|
63
|
+
13255342817.si_byte(3) # '12.3GB'
|
62
64
|
```
|
63
65
|
|
66
|
+
## SI module methods: convert / revert
|
67
|
+
|
64
68
|
```ruby
|
65
|
-
|
69
|
+
SI.convert(9876543210000, :length => 5) # '9.8765T'
|
70
|
+
SI.revert('100k', :base => 1024) # 102400.0
|
71
|
+
```
|
72
|
+
|
73
|
+
## Avoiding monkey-patching
|
74
|
+
|
75
|
+
Require 'si/minimal' instead to avoid monkey-patching numeric classes.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
require 'si/minimal'
|
79
|
+
|
80
|
+
SI.convert(987654321, 3) # 988M
|
66
81
|
```
|
67
82
|
|
68
83
|
## Contributing
|
data/lib/si.rb
CHANGED
@@ -1,55 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
|
5
|
-
module SI
|
6
|
-
PREFIXES = Hash[ -8.upto(8).zip(%[yzafpnμm kMGTPEZY].chars.map(&:strip)) ]
|
7
|
-
DEFAULT = {
|
8
|
-
:length => 3,
|
9
|
-
:base => 1000,
|
10
|
-
:min_exp => -8,
|
11
|
-
:max_exp => 8,
|
12
|
-
}
|
13
|
-
|
14
|
-
def si options = {}
|
15
|
-
options = { :length => options } if options.is_a?(Fixnum) && options >= 3
|
16
|
-
options = DEFAULT.merge(options)
|
17
|
-
length,
|
18
|
-
min_exp,
|
19
|
-
max_exp = options.values_at(:length, :min_exp, :max_exp)
|
20
|
-
base = options[:base].to_f
|
21
|
-
minus = self < 0 ? '-' : ''
|
22
|
-
selfp = self.abs
|
23
|
-
|
24
|
-
PREFIXES.keys.sort.reverse.select { |exp| (min_exp..max_exp).include? exp }.each do |exp|
|
25
|
-
denom = base ** exp
|
26
|
-
if selfp >= denom || exp == min_exp
|
27
|
-
val = selfp / denom
|
28
|
-
val = val.round [length - val.to_i.to_s.length, 0].max
|
29
|
-
val = val.to_i if exp == 0 && self.is_a?(Fixnum)
|
30
|
-
val = val.to_s.ljust(length + 1, '0') if val.is_a?(Float)
|
31
|
-
|
32
|
-
return "#{minus}#{val}#{PREFIXES[exp]}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
nil
|
37
|
-
end
|
38
|
-
|
39
|
-
def si_byte length = 3
|
40
|
-
self.si(:length => length, :base => 1024, :min_exp => 0) + 'B'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class Float
|
45
|
-
include SI
|
46
|
-
end
|
47
|
-
|
48
|
-
class Fixnum
|
49
|
-
include SI
|
50
|
-
end
|
51
|
-
|
52
|
-
class Bignum
|
53
|
-
include SI
|
54
|
-
end
|
3
|
+
require 'si/minimal'
|
4
|
+
require 'si/patch'
|
55
5
|
|
data/lib/si/constants.rb
ADDED
data/lib/si/minimal.rb
ADDED
data/lib/si/module.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module SI
|
2
|
+
class << self
|
3
|
+
def convert num, options = {}
|
4
|
+
options = { :length => options } if options.is_a?(Fixnum) && options >= 3
|
5
|
+
options = DEFAULT.merge(options)
|
6
|
+
length,
|
7
|
+
min_exp,
|
8
|
+
max_exp = options.values_at(:length, :min_exp, :max_exp)
|
9
|
+
base = options[:base].to_f
|
10
|
+
minus = num < 0 ? '-' : ''
|
11
|
+
nump = num.abs
|
12
|
+
|
13
|
+
PREFIXES.keys.sort.reverse.select { |exp| (min_exp..max_exp).include? exp }.each do |exp|
|
14
|
+
denom = base ** exp
|
15
|
+
if nump >= denom || exp == min_exp
|
16
|
+
val = nump / denom
|
17
|
+
val = val.round [length - val.to_i.to_s.length, 0].max
|
18
|
+
val = val.to_i if exp == 0 && num.is_a?(Fixnum)
|
19
|
+
val = val.to_s.ljust(length + 1, '0') if val.is_a?(Float)
|
20
|
+
|
21
|
+
return "#{minus}#{val}#{PREFIXES[exp]}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def revert str, options = {}
|
29
|
+
options = DEFAULT.select { |k, v| k == :base }.merge(options)
|
30
|
+
base = options[:base]
|
31
|
+
pair = PREFIXES.to_a.find { |k, v| v == str[-1] }
|
32
|
+
|
33
|
+
if pair
|
34
|
+
str[0...-1].to_f * (options[:base] ** pair.first)
|
35
|
+
else
|
36
|
+
str.to_f
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def si options = {}
|
42
|
+
SI.convert(self, options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def si_byte length = 3
|
46
|
+
SI.convert(self, :length => length, :base => 1024, :min_exp => 0) + 'B'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
data/lib/si/patch.rb
ADDED
data/lib/si/version.rb
CHANGED
data/test/test_si.rb
CHANGED
@@ -180,5 +180,21 @@ class TestSI < Test::Unit::TestCase
|
|
180
180
|
def test_shortcut
|
181
181
|
assert_equal '123.5M', 123450000.si(4)
|
182
182
|
end
|
183
|
+
|
184
|
+
def test_module_methods
|
185
|
+
assert_equal '9.8765T', SI.convert(9876543210000, :length => 5)
|
186
|
+
assert_equal 9876500000000, SI.revert('9.8765T')
|
187
|
+
assert_equal 9876500000, SI.revert('9.8765G')
|
188
|
+
assert_equal 9876500, SI.revert('9.8765M')
|
189
|
+
assert_equal 9.8765 * 1024 ** 2, SI.revert('9.8765M', :base => 1024)
|
190
|
+
assert_equal 0.0098765, SI.revert('9.8765m')
|
191
|
+
assert_equal 0.0098765, SI.revert('9.8765m')
|
192
|
+
assert_equal 9.8765, SI.revert('9.8765')
|
193
|
+
assert_equal 0.0, SI.revert('hello') # FIXME
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_rational
|
197
|
+
assert_equal '12.345n', (12345 * (10 ** -12)).si(5)
|
198
|
+
end
|
183
199
|
end
|
184
200
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: si
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Formats a number with SI prefix
|
15
15
|
email:
|
@@ -24,6 +24,10 @@ files:
|
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
26
|
- lib/si.rb
|
27
|
+
- lib/si/constants.rb
|
28
|
+
- lib/si/minimal.rb
|
29
|
+
- lib/si/module.rb
|
30
|
+
- lib/si/patch.rb
|
27
31
|
- lib/si/version.rb
|
28
32
|
- si.gemspec
|
29
33
|
- test/test_si.rb
|