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 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
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in si.gemspec
4
4
  gemspec
5
+
6
+ gem 'test-unit', '~> 3.6', '>= 3.6.1'
7
+ gem 'rake', '~> 13.0', '>= 13.0.6'
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
- - `:length` Number of digits. (default: 3)
49
- - `:base` For [binary prefix](http://en.wikipedia.org/wiki/Binary_prefix), set this to 1024 instead of default 1000.
50
- - `:min_exp` Default: -8, down to <strong>y</strong>octo
51
- - `:max_exp` Default: 8, up to <strong>Y</strong>otta
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 = Hash[ -8.upto(8).zip(%[yzafpnμm kMGTPEZY].chars.map(&:strip)) ]
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?(Fixnum)
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?(Fixnum) ? '0' : "0.#{'0' * (length - 1)}" if num == 0
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 = val.round [length - val.to_i.to_s.length, 0].max
21
- val = val.to_i if exp == 0 && num.is_a?(Fixnum)
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 == str[-1] }
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
- class Float
2
- include SI
3
- end
1
+ original_verbosity = $VERBOSE
4
2
 
5
- class Fixnum
6
- include SI
7
- end
3
+ $VERBOSE = nil
8
4
 
9
- class Bignum
10
- include SI
11
- end
5
+ ['Float', 'Fixnum', 'Bignum', 'Rational', 'Integer'].each do |const|
6
+ next unless Object.const_defined?(const)
12
7
 
13
- class Rational
14
- include SI
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
@@ -1,3 +1,3 @@
1
1
  module SI
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
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.0098765, SI.revert('9.8765m')
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.1.3
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: 2012-09-14 00:00:00.000000000 Z
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
- - .gitignore
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
- post_install_message:
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
- rubyforge_project:
54
- rubygems_version: 1.8.24
55
- signing_key:
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