anthroposi 0.0.1
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.
- checksums.yaml +7 -0
- data/lib/anthroposi.rb +62 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c5e2b2e1ded0d6a09c10ba2c612bf5ff959d80b1
|
4
|
+
data.tar.gz: 6bd849dec61e7a3de70946cf93e97e1892c8a15c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de7de9b15bbc33248e8efb690ce081601a308f91b30e2a0b49fcbc040312b5315dafe89ed70c046c29af48212e76002c1ff6eadc402beda46bae7ac0a87fad46
|
7
|
+
data.tar.gz: 02abcb2a2a355ffb265bc537b2d5b77807a60b319b01d6c660b75c225a57ad493a288a9f2d661e0231d82e78de636c2607942863693dfe5275f356bbca6c0d73
|
data/lib/anthroposi.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
class Anthroposi
|
2
|
+
SI_PREFIXES = %w(K M G T P E Z Y).freeze
|
3
|
+
BINARY_UNITS = ['B'] + SI_PREFIXES.map { |p| "#{p}iB" }
|
4
|
+
DECIMAL_PREFIXES = [''] + SI_PREFIXES
|
5
|
+
KILO = 1024
|
6
|
+
SIGNIFICANT_DIGITS = 3
|
7
|
+
LARGEST_THRESHOLD = 9999
|
8
|
+
|
9
|
+
def initialize(bytes, decimal = false)
|
10
|
+
raise(ArgumentError, 'no string or fractional number') if bytes > 0 && bytes < 1
|
11
|
+
@bytes = bytes
|
12
|
+
@bytes.zero? and return handle_zero(decimal)
|
13
|
+
prepare(decimal)
|
14
|
+
@unit = compute
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
format @mantissa > LARGEST_THRESHOLD ? '%.2e%s' : '%s%s', @mantissa, @unit
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def prepare(decimal)
|
24
|
+
if decimal
|
25
|
+
@thousand = 1000
|
26
|
+
@units = DECIMAL_PREFIXES
|
27
|
+
@ordinal = Math.log10(@bytes).to_i / 3
|
28
|
+
else
|
29
|
+
@thousand = KILO
|
30
|
+
@units = BINARY_UNITS
|
31
|
+
@ordinal = Math.log2(@bytes).to_i / 10
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def handle_zero(decimal)
|
36
|
+
@mantissa = 0
|
37
|
+
@units = decimal ? DECIMAL_PREFIXES : BINARY_UNITS
|
38
|
+
@unit = @units[0]
|
39
|
+
end
|
40
|
+
|
41
|
+
def compute
|
42
|
+
if @ordinal < BINARY_UNITS.size
|
43
|
+
@mantissa = significant_digits(@bytes.to_f / @thousand**@ordinal, SIGNIFICANT_DIGITS)
|
44
|
+
return @units[@ordinal]
|
45
|
+
end
|
46
|
+
@mantissa = significant_digits(@bytes.to_f / @thousand**SI_PREFIXES.size, SIGNIFICANT_DIGITS)
|
47
|
+
@units[-1]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def significant_digits(number, digits)
|
52
|
+
return 0 if number.zero? || digits.zero?
|
53
|
+
number.to_f.round(digits - 1 - Math.log10(number.abs).floor)
|
54
|
+
end
|
55
|
+
|
56
|
+
def human_size(b)
|
57
|
+
Anthroposi.new(b).to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
def human_decimal_size(b)
|
61
|
+
Anthroposi.new(b, true).to_s
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anthroposi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- k z win
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email: rubygems@happyw.info
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/anthroposi.rb
|
20
|
+
homepage: https://github.com/kzw/anthroposi
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.6.11
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Turn number in binary units such as MiB, KiB or decimal prefixes G, T
|
44
|
+
test_files: []
|