Skip to content
This repository was archived by the owner on Sep 4, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ That means:
### Parsing a string
```ruby
Filesize.from("1 GiB")
# => #<Filesize:0x93c06c8 @bytes=1073741824, @type={:regexp=>/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}>
# => #<Filesize:0x00007fd71f103328 @bytes=1073741824, @type={:regexp=>/^(-?)([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i, :multiplier=>1024, :prefixes=>["Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"], :presuffix=>"i"}>
```

### Converting filesizes
Expand Down
17 changes: 9 additions & 8 deletions lib/filesize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class Filesize

# Set of rules describing file sizes according to SI units.
SI = {
:regexp => /^([\d,.]+)?[[:space:]]?([kmgtpezy]?)b?$/i,
:regexp => /^(-?)([\d,.]+)?[[:space:]]?([kmgtpezy]?)b?$/i,
:multiplier => 1000,
:prefixes => TYPE_PREFIXES[:SI],
:presuffix => '' # deprecated
}
# Set of rules describing file sizes according to binary units.
BINARY = {
:regexp => /^([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i,
:regexp => /^(-?)([\d,.]+)?[[:space:]]?(?:([kmgtpezy])i)?b?$/i,
:multiplier => 1024,
:prefixes => TYPE_PREFIXES[:BINARY],
:presuffix => 'i' # deprecated
Expand Down Expand Up @@ -71,14 +71,14 @@ def to_s(unit = 'B')
# @return [String]
# @see #to_s
def pretty
size = @bytes
size = @bytes.abs
if size < @type[:multiplier]
unit = "B"
unit = 'B'
else
pos = (Math.log(size) / Math.log(@type[:multiplier])).floor
pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1

unit = @type[:prefixes][pos-1] + "B"
unit = @type[:prefixes][pos-1] + 'B'
end

to_s(unit)
Expand Down Expand Up @@ -151,10 +151,11 @@ def parse(string)
end
}

prefix = $2 || ''
size = ($1 || 0).to_f
sign = $1 == '' ? 1 : -1
prefix = $3 || ''
size = ($2 || 0).to_f * sign

return { :prefix => prefix, :size => size, :type => type}
return { :prefix => prefix, :size => size, :type => type }
end
end

Expand Down
44 changes: 40 additions & 4 deletions spec/lib/filesize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
it 'parses megabytes without b suffix' do
expect(Filesize.from('1 M').to).to eq 1000 * 1000
end

it 'parses negative kilobytes' do
expect(Filesize.from('-1 kB').to).to eq -1000
end

it 'parses negative megabytes' do
expect(Filesize.from('-1 MB').to).to eq -1000 * 1000
end

it 'parses negative megabytes without b suffix' do
expect(Filesize.from('-1 M').to).to eq -1000 * 1000
end
end

context 'BINARY units' do
Expand All @@ -56,6 +68,18 @@
it 'parses mebibytes without b suffix' do
expect(Filesize.from('1 Mi').to).to eq 1024 * 1024
end

it 'parses negative kilobytes' do
expect(Filesize.from('-1 KiB').to).to eq -1024
end

it 'parses negative megabytes' do
expect(Filesize.from('-1 MiB').to).to eq -1024 * 1024
end

it 'parses negative mebibytes without b suffix' do
expect(Filesize.from('-1 Mi').to).to eq -1024 * 1024
end
end
end

Expand Down Expand Up @@ -105,12 +129,24 @@
end

describe '#pretty' do
it 'returns the number of the most matching prefix with its unit (BINARY default)' do
expect(Filesize.new(1024).pretty).to eq '1.00 KiB'
context 'BINARY units (default)' do
it 'returns the number of the most matching prefix with its unit' do
expect(Filesize.new(1024).pretty).to eq '1.00 KiB'
end

it 'returns the negative number of the most matching prefix with its unit' do
expect(Filesize.new(-1024).pretty).to eq '-1.00 KiB'
end
end

it 'returns the number of the most matching prefix with its unit (SI)' do
expect(Filesize.new(1000, Filesize::SI).pretty).to eq '1.00 kB'
context 'SI units' do
it 'returns the number of the most matching prefix with its unit' do
expect(Filesize.new(1000, Filesize::SI).pretty).to eq '1.00 kB'
end

it 'returns the negative number of the most matching prefix with its unit' do
expect(Filesize.new(-1000, Filesize::SI).pretty).to eq '-1.00 kB'
end
end
end

Expand Down