si 0.1.0
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +71 -0
- data/Rakefile +8 -0
- data/lib/si.rb +54 -0
- data/lib/si/version.rb +3 -0
- data/si.gemspec +17 -0
- data/test/test_si.rb +180 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Junegunn Choi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# SI
|
2
|
+
|
3
|
+
Formats a number with [SI prefix (Metric prefix)](http://en.wikipedia.org/wiki/SI_prefix).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'si'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install si
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### `si`
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
0.9.si # '900m'
|
25
|
+
9.si # '9'
|
26
|
+
98.si # '98'
|
27
|
+
987.si # '987'
|
28
|
+
9876.si # '9.88k'
|
29
|
+
98765.si # '98.8k'
|
30
|
+
987654.si # '988k'
|
31
|
+
9876543.si # '9.88M'
|
32
|
+
98765432.si # '98.8M'
|
33
|
+
987654321.si # '988M'
|
34
|
+
9876543210.si # '9.88G'
|
35
|
+
98765432100.si # '98.8G'
|
36
|
+
987654321000.si # '988G'
|
37
|
+
9876543210000.si # '9.88T'
|
38
|
+
# ...
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
#### Options
|
43
|
+
|
44
|
+
- `:length` Number of digits. (default: 3)
|
45
|
+
- `:base` For [binary prefix](http://en.wikipedia.org/wiki/Binary_prefix), set this to 1024 instead of default 1000.
|
46
|
+
- `:min_exp` Default: -8, down to <strong>y</strong>octo
|
47
|
+
- `:max_exp` Default: 8, up to <strong>Y</strong>otta
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
9876543210000.si(:length => 5) # '9.8765T'
|
51
|
+
```
|
52
|
+
|
53
|
+
### `si_byte`
|
54
|
+
|
55
|
+
`si_byte` is simply a shorcut for
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
number.si(:length => length, :base => 1024, :min_exp => 0) + 'B'
|
59
|
+
```
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
13255342817.si_byte(3) # '12.3GB'
|
63
|
+
```
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
1. Fork it
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
69
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
70
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
71
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/si.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "si/version"
|
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 = DEFAULT.merge(options)
|
16
|
+
length,
|
17
|
+
min_exp,
|
18
|
+
max_exp = options.values_at(:length, :min_exp, :max_exp)
|
19
|
+
base = options[:base].to_f
|
20
|
+
minus = self < 0 ? '-' : ''
|
21
|
+
selfp = self.abs
|
22
|
+
|
23
|
+
PREFIXES.keys.sort.reverse.select { |exp| (min_exp..max_exp).include? exp }.each do |exp|
|
24
|
+
denom = base ** exp
|
25
|
+
if selfp >= denom || exp == min_exp
|
26
|
+
val = selfp / denom
|
27
|
+
val = val.round [length - val.to_i.to_s.length, 0].max
|
28
|
+
val = val.to_i if exp == 0 && self.is_a?(Fixnum)
|
29
|
+
val = val.to_s.ljust(length + 1, '0') if val.is_a?(Float)
|
30
|
+
|
31
|
+
return "#{minus}#{val}#{PREFIXES[exp]}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def si_byte length = 3
|
39
|
+
self.si(:length => length, :base => 1024, :min_exp => 0) + 'B'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Float
|
44
|
+
include SI
|
45
|
+
end
|
46
|
+
|
47
|
+
class Fixnum
|
48
|
+
include SI
|
49
|
+
end
|
50
|
+
|
51
|
+
class Bignum
|
52
|
+
include SI
|
53
|
+
end
|
54
|
+
|
data/lib/si/version.rb
ADDED
data/si.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/si/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Junegunn Choi"]
|
6
|
+
gem.email = ["junegunn.c@gmail.com"]
|
7
|
+
gem.description = %q{Formats a number with SI prefix}
|
8
|
+
gem.summary = %q{Formats a number with SI prefix (metric prefix)}
|
9
|
+
gem.homepage = "https://github.com/junegunn/si"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "si"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = SI::VERSION
|
17
|
+
end
|
data/test/test_si.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
require 'test/unit'
|
6
|
+
require 'si'
|
7
|
+
|
8
|
+
class TestSI < Test::Unit::TestCase
|
9
|
+
def test_si
|
10
|
+
val = 0.000_000_000_000_000_000_000_000_0001234567
|
11
|
+
|
12
|
+
%w[
|
13
|
+
0.00012y
|
14
|
+
0.00123y
|
15
|
+
0.01235y
|
16
|
+
0.12346y
|
17
|
+
|
18
|
+
1.23457y
|
19
|
+
12.3457y
|
20
|
+
123.457y
|
21
|
+
|
22
|
+
1.23457z
|
23
|
+
12.3457z
|
24
|
+
123.457z
|
25
|
+
|
26
|
+
1.23457a
|
27
|
+
12.3457a
|
28
|
+
123.457a
|
29
|
+
|
30
|
+
1.23457f
|
31
|
+
12.3457f
|
32
|
+
123.457f
|
33
|
+
|
34
|
+
1.23457p
|
35
|
+
12.3457p
|
36
|
+
123.457p
|
37
|
+
|
38
|
+
1.23457n
|
39
|
+
12.3457n
|
40
|
+
123.457n
|
41
|
+
|
42
|
+
1.23457μ
|
43
|
+
12.3457μ
|
44
|
+
123.457μ
|
45
|
+
|
46
|
+
1.23457m
|
47
|
+
12.3457m
|
48
|
+
123.457m
|
49
|
+
|
50
|
+
1.23457
|
51
|
+
12.3457
|
52
|
+
123.457
|
53
|
+
|
54
|
+
1.23457k
|
55
|
+
12.3457k
|
56
|
+
123.457k
|
57
|
+
|
58
|
+
1.23457M
|
59
|
+
12.3457M
|
60
|
+
123.457M
|
61
|
+
|
62
|
+
1.23457G
|
63
|
+
12.3457G
|
64
|
+
123.457G
|
65
|
+
|
66
|
+
1.23457T
|
67
|
+
12.3457T
|
68
|
+
123.457T
|
69
|
+
|
70
|
+
1.23457P
|
71
|
+
12.3457P
|
72
|
+
123.457P
|
73
|
+
|
74
|
+
1.23457E
|
75
|
+
12.3457E
|
76
|
+
123.457E
|
77
|
+
|
78
|
+
1.23457Z
|
79
|
+
12.3457Z
|
80
|
+
123.457Z
|
81
|
+
|
82
|
+
1.23457Y
|
83
|
+
12.3457Y
|
84
|
+
123.457Y
|
85
|
+
|
86
|
+
1234.57Y
|
87
|
+
12345.7Y
|
88
|
+
123457Y
|
89
|
+
1234567Y
|
90
|
+
12345670Y
|
91
|
+
123456700Y
|
92
|
+
1234567000Y
|
93
|
+
].each do |ret|
|
94
|
+
assert_equal ret, val.si(:length => 6)
|
95
|
+
assert_equal '-' + ret, (-val).si(:length => 6)
|
96
|
+
val *= 10
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_si_base
|
101
|
+
val = 807936
|
102
|
+
%w[
|
103
|
+
789k
|
104
|
+
789M
|
105
|
+
789G
|
106
|
+
789T
|
107
|
+
807936T
|
108
|
+
827326464T
|
109
|
+
].each do |ret|
|
110
|
+
assert_equal ret, val.si(:base => 1024, :max_exp => 4)
|
111
|
+
val *= 1024
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_si_byte
|
116
|
+
{
|
117
|
+
1234132 => '1.18MB',
|
118
|
+
123004132 => '117MB',
|
119
|
+
123004999132 => '115GB',
|
120
|
+
555123004999132 => '505TB',
|
121
|
+
5555123004999132 => '4.93PB',
|
122
|
+
3335555123004999132 => '2.89EB',
|
123
|
+
0.001 => '0.00B',
|
124
|
+
0.005 => '0.01B',
|
125
|
+
0.01 => '0.01B',
|
126
|
+
0.1 => '0.10B',
|
127
|
+
0 => '0B',
|
128
|
+
1 => '1B',
|
129
|
+
1.0 => '1.00B',
|
130
|
+
1.01 => '1.01B',
|
131
|
+
10 => '10B',
|
132
|
+
10.0 => '10.0B',
|
133
|
+
100 => '100B',
|
134
|
+
100.0 => '100B',
|
135
|
+
0.0 => '0.00B',
|
136
|
+
0.123412 => '0.12B',
|
137
|
+
0.0123412 => '0.01B',
|
138
|
+
0.00123412 => '0.00B',
|
139
|
+
}.each do |val, ret|
|
140
|
+
assert_equal ret, val.si_byte
|
141
|
+
|
142
|
+
if val == 0
|
143
|
+
assert_equal ret, (-val).si_byte
|
144
|
+
else
|
145
|
+
assert_equal '-' + ret, (-val).si_byte unless val == 0
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_readme
|
151
|
+
{
|
152
|
+
0.009 => '9.00m',
|
153
|
+
0.09 => '90.0m',
|
154
|
+
0.9 => '900m',
|
155
|
+
9 => '9',
|
156
|
+
98 => '98',
|
157
|
+
987 => '987',
|
158
|
+
9876 => '9.88k',
|
159
|
+
98765 => '98.8k',
|
160
|
+
987654 => '988k',
|
161
|
+
9876543 => '9.88M',
|
162
|
+
98765432 => '98.8M',
|
163
|
+
987654321 => '988M',
|
164
|
+
9876543210 => '9.88G',
|
165
|
+
98765432100 => '98.8G',
|
166
|
+
987654321000 => '988G',
|
167
|
+
9876543210000 => '9.88T',
|
168
|
+
}.each do |val, ret|
|
169
|
+
assert_equal ret, val.si
|
170
|
+
end
|
171
|
+
|
172
|
+
assert_equal '9.8765T', 9876543210000.si(:length => 5)
|
173
|
+
assert_equal '12.3GB', 13255342817.si_byte
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_edge_cases
|
177
|
+
assert_equal '9.0000T', 9000000000001.si(:length => 5)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: si
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Junegunn Choi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Formats a number with SI prefix
|
15
|
+
email:
|
16
|
+
- junegunn.c@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/si.rb
|
27
|
+
- lib/si/version.rb
|
28
|
+
- si.gemspec
|
29
|
+
- test/test_si.rb
|
30
|
+
homepage: https://github.com/junegunn/si
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Formats a number with SI prefix (metric prefix)
|
54
|
+
test_files:
|
55
|
+
- test/test_si.rb
|