si 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +33 -0
- data/.gitignore +0 -1
- data/Gemfile +3 -0
- data/Gemfile.lock +23 -0
- data/README.md +6 -4
- data/lib/si/constants.rb +19 -1
- data/lib/si/module.rb +18 -6
- data/lib/si/patch.rb +9 -11
- data/lib/si/version.rb +1 -1
- data/test/test_si.rb +2 -1
- metadata +13 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e3d6c6cb24a8ed364c310019d4ad39af6f6bdc0c9ffeba7915854ef72a3de125
|
4
|
+
data.tar.gz: e67d999cd37a2f6d2cfb0c05994bb43d447e18bbc0e3aa9f0ee953f026772007
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c379c44a26d41a4d9e80448f6e9ced1b24723b9bb7b9300b2a71ec244c97c7fe98ea428e607c395d5940ea7b5a70f6b96a81cc0e0f770aa743c46b7c78fbe94a
|
7
|
+
data.tar.gz: d447b4d285805e5947d2f03caec9764f6547aebaa1b7feeacbe2f416a954e32f30559a9d8a55867a9aa3559c6f0ff318561ec742ff6d24e60f6054d5d483f177
|
@@ -0,0 +1,33 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [master]
|
6
|
+
pull_request:
|
7
|
+
branches: [master]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2']
|
16
|
+
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v3
|
19
|
+
|
20
|
+
- name: Set up Ruby
|
21
|
+
uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby }}
|
24
|
+
bundler-cache: true
|
25
|
+
|
26
|
+
- name: Update the RubyGems system software
|
27
|
+
run: gem update --system
|
28
|
+
|
29
|
+
- name: Update Bundler
|
30
|
+
run: bundle update --bundler
|
31
|
+
|
32
|
+
- name: Install dependencies
|
33
|
+
run: bundle install
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
si (0.1.4)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
power_assert (2.0.3)
|
10
|
+
rake (13.0.6)
|
11
|
+
test-unit (3.6.1)
|
12
|
+
power_assert
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
rake (~> 13.0, >= 13.0.6)
|
19
|
+
si!
|
20
|
+
test-unit (~> 3.6, >= 3.6.1)
|
21
|
+
|
22
|
+
BUNDLED WITH
|
23
|
+
2.4.10
|
data/README.md
CHANGED
@@ -45,10 +45,12 @@ require 'si'
|
|
45
45
|
|
46
46
|
#### Options
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
| Option | Default | Description |
|
49
|
+
|------------|---------|-----------------------------------------------------------------------------------------------------------|
|
50
|
+
| `:length` | 3 | Number of digits |
|
51
|
+
| `:base` | 1000 | For [binary prefix](http://en.wikipedia.org/wiki/Binary_prefix), set this to 1024 instead of default 1000 |
|
52
|
+
| `:min_exp` | -8 | Down to <strong>y</strong>octo |
|
53
|
+
| `:max_exp` | 8 | Up to <strong>Y</strong>otta |
|
52
54
|
|
53
55
|
```ruby
|
54
56
|
9876543210000.si(:length => 5) # '9.8765T'
|
data/lib/si/constants.rb
CHANGED
@@ -1,7 +1,25 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module SI
|
4
|
-
PREFIXES =
|
4
|
+
PREFIXES = {
|
5
|
+
-8 => 'y',
|
6
|
+
-7 => 'z',
|
7
|
+
-6 => 'a',
|
8
|
+
-5 => 'f',
|
9
|
+
-4 => 'p',
|
10
|
+
-3 => 'n',
|
11
|
+
-2 => 'μ',
|
12
|
+
-1 => 'm',
|
13
|
+
0 => '',
|
14
|
+
1 => 'k',
|
15
|
+
2 => 'M',
|
16
|
+
3 => 'G',
|
17
|
+
4 => 'T',
|
18
|
+
5 => 'P',
|
19
|
+
6 => 'E',
|
20
|
+
7 => 'Z',
|
21
|
+
8 => 'Y'
|
22
|
+
}
|
5
23
|
|
6
24
|
DEFAULT = {
|
7
25
|
:length => 3,
|
data/lib/si/module.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
module SI
|
2
2
|
class << self
|
3
3
|
def convert num, options = {}
|
4
|
-
options = { :length => options } if options.is_a?(
|
4
|
+
options = { :length => options } if options.is_a?(Numeric)
|
5
5
|
options = DEFAULT.merge(options)
|
6
6
|
length,
|
7
7
|
min_exp,
|
8
8
|
max_exp = options.values_at(:length, :min_exp, :max_exp)
|
9
9
|
raise ArgumentError.new("Invalid length") if length < 2
|
10
|
-
return num.is_a?(
|
10
|
+
return num.is_a?(Float) ? "0.#{'0' * (length - 1)}" : '0' if num == 0
|
11
11
|
|
12
12
|
base = options[:base].to_f
|
13
13
|
minus = num < 0 ? '-' : ''
|
@@ -17,8 +17,8 @@ module SI
|
|
17
17
|
denom = base ** exp
|
18
18
|
if nump >= denom || exp == min_exp
|
19
19
|
val = nump / denom
|
20
|
-
val =
|
21
|
-
val = val.to_i if exp == 0 && num.is_a?(
|
20
|
+
val = SI.round val, [length - val.to_i.to_s.length, 0].max
|
21
|
+
val = val.to_i if exp == 0 && !num.is_a?(Float)
|
22
22
|
val = val.to_s.ljust(length + 1, '0') if val.is_a?(Float)
|
23
23
|
|
24
24
|
return "#{minus}#{val}#{PREFIXES[exp]}"
|
@@ -29,8 +29,8 @@ module SI
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def revert str, options = {}
|
32
|
-
options = DEFAULT.select { |k, v| k == :base }.merge(options)
|
33
|
-
pair = PREFIXES.to_a.find { |k, v| v
|
32
|
+
options = Hash[ DEFAULT.select { |k, v| k == :base } ].merge(options)
|
33
|
+
pair = PREFIXES.to_a.find { |k, v| !v.empty? && str =~ /[0-9]#{v}$/ }
|
34
34
|
|
35
35
|
if pair
|
36
36
|
str[0...-1].to_f * (options[:base] ** pair.first)
|
@@ -38,6 +38,18 @@ module SI
|
|
38
38
|
str.to_f
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
if (RUBY_VERSION.split('.')[0, 2].map(&:to_i) <=> [1, 8]) == 1
|
43
|
+
def round val, ndigits
|
44
|
+
val.round ndigits
|
45
|
+
end
|
46
|
+
else
|
47
|
+
def round val, ndigits
|
48
|
+
exp = (10 ** ndigits).to_f
|
49
|
+
val = ((val * exp).round / exp)
|
50
|
+
ndigits == 0 ? val.to_i : val
|
51
|
+
end
|
52
|
+
end
|
41
53
|
end
|
42
54
|
|
43
55
|
def si options = {}
|
data/lib/si/patch.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
-
|
2
|
-
include SI
|
3
|
-
end
|
1
|
+
original_verbosity = $VERBOSE
|
4
2
|
|
5
|
-
|
6
|
-
include SI
|
7
|
-
end
|
3
|
+
$VERBOSE = nil
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
5
|
+
['Float', 'Fixnum', 'Bignum', 'Rational', 'Integer'].each do |const|
|
6
|
+
next unless Object.const_defined?(const)
|
12
7
|
|
13
|
-
|
14
|
-
|
8
|
+
Object.const_get(const).class_eval do
|
9
|
+
include SI
|
10
|
+
end
|
15
11
|
end
|
12
|
+
|
13
|
+
$VERBOSE = original_verbosity
|
data/lib/si/version.rb
CHANGED
data/test/test_si.rb
CHANGED
@@ -187,9 +187,10 @@ class TestSI < Test::Unit::TestCase
|
|
187
187
|
assert_equal 9876500000, SI.revert('9.8765G')
|
188
188
|
assert_equal 9876500, SI.revert('9.8765M')
|
189
189
|
assert_equal 9.8765 * 1024 ** 2, SI.revert('9.8765M', :base => 1024)
|
190
|
-
assert_equal 0.
|
190
|
+
assert_equal 0.0000098765, SI.revert('9.8765μ')
|
191
191
|
assert_equal 0.0098765, SI.revert('9.8765m')
|
192
192
|
assert_equal 9.8765, SI.revert('9.8765')
|
193
|
+
assert_equal 9.8765, SI.revert('9.8765xxxyyyzzz')
|
193
194
|
assert_equal 0.0, SI.revert('hello') # FIXME
|
194
195
|
end
|
195
196
|
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: si
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Junegunn Choi
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2023-08-26 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: Formats a number with SI prefix
|
15
14
|
email:
|
@@ -18,8 +17,10 @@ executables: []
|
|
18
17
|
extensions: []
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
21
|
-
- .
|
20
|
+
- ".github/workflows/ruby.yml"
|
21
|
+
- ".gitignore"
|
22
22
|
- Gemfile
|
23
|
+
- Gemfile.lock
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
@@ -33,27 +34,25 @@ files:
|
|
33
34
|
- test/test_si.rb
|
34
35
|
homepage: https://github.com/junegunn/si
|
35
36
|
licenses: []
|
36
|
-
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
37
39
|
rdoc_options: []
|
38
40
|
require_paths:
|
39
41
|
- lib
|
40
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
43
|
requirements:
|
43
|
-
- -
|
44
|
+
- - ">="
|
44
45
|
- !ruby/object:Gem::Version
|
45
46
|
version: '0'
|
46
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
48
|
requirements:
|
49
|
-
- -
|
49
|
+
- - ">="
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
specification_version: 3
|
53
|
+
rubygems_version: 3.4.6
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
57
56
|
summary: Formats a number with SI prefix (metric prefix)
|
58
57
|
test_files:
|
59
58
|
- test/test_si.rb
|