sort_bytes 0.1.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
+ SHA1:
3
+ metadata.gz: 3d0239c984a968ea98711f2077a61c91dbe2cad7
4
+ data.tar.gz: 7a261add9588cf295facea1c16bae5b1b95229dc
5
+ SHA512:
6
+ metadata.gz: c3cfb533a49957464ae228217dac8ca420658fdd899cf6adf68ece7066760daba220263d10578bb5c4e0e62e766cfe21c22c12bf3b65a0a7dd1284a58cee744d
7
+ data.tar.gz: dd3b96948fb8cd220e27906841bae1347b028654006a06b0ba112d2a0600ceb4eceed42db07e9fac380963db025e3ac569a3c832eb2737bdc52e8918955ecfd7
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sort_bytes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jon Riddle
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,23 @@
1
+ # SortBytes
2
+
3
+ Sorts lines by human-readable byte sizes (what you get from `du -h`)
4
+
5
+ ## Installation
6
+
7
+ $ gem install sort_bytes
8
+
9
+ ## Usage
10
+
11
+ sort_bytes <filename>
12
+
13
+ or
14
+
15
+ some_command | sort_bytes
16
+
17
+ ## Contributing
18
+
19
+ 1. Fork it
20
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
21
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
22
+ 4. Push to the branch (`git push origin my-new-feature`)
23
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
data/bin/sort_bytes ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sort_bytes'
4
+
5
+ puts SortBytes::SizeSorter.new(ARGF.readlines).sorted
data/lib/sort_bytes.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "sort_bytes/version"
2
+
3
+ module SortBytes
4
+ class SizeSorter
5
+ attr_accessor :lines
6
+
7
+ def initialize(lines)
8
+ @lines = lines
9
+ end
10
+
11
+ def sorted
12
+ lines.map do |line|
13
+ [ size_in_bytes(size_token(line)), line]
14
+ end.sort.map(&:last)
15
+ end
16
+
17
+ def size_in_bytes(size)
18
+ num, suffix = size.split(/([^0-9.]+)/)
19
+ suffix ||= 'B'
20
+ power = %w(B K M G T P).index(suffix[0,1]) * 3
21
+ multiplier = 10 ** power
22
+ num.to_f * multiplier
23
+ end
24
+
25
+ def size_token(line)
26
+ line[/^\s*(\S+)/, 1]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module SortBytes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sort_bytes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sort_bytes"
8
+ spec.version = SortBytes::VERSION
9
+ spec.authors = ["Jon Riddle"]
10
+ spec.email = ["jon@jdrnetworking.com"]
11
+ spec.description = %q{Sorts lines by human-readable byte size (like the output of `du -h`)}
12
+ spec.summary = %q{Sorts lines by human-readable byte size}
13
+ spec.homepage = "https://github.com/jdrnetworking/sort_bytes"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.4"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'sort_bytes'
3
+
4
+ require 'minitest/autorun'
@@ -0,0 +1,52 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestSortBytes < MiniTest::Unit::TestCase
4
+ def setup
5
+ @sorter = SortBytes::SizeSorter.new([])
6
+ end
7
+
8
+ def test_should_accept_decimals_in_byte_size
9
+ assert_in_delta 2.5, @sorter.size_in_bytes('2.5B')
10
+ end
11
+
12
+ def test_should_return_size_with_no_suffix
13
+ assert_in_delta 5.0, @sorter.size_in_bytes('5')
14
+ end
15
+
16
+ def test_should_return_size_with_b_suffix
17
+ assert_in_delta 5.0, @sorter.size_in_bytes('5B')
18
+ end
19
+
20
+ def test_should_return_size_with_k_suffix
21
+ assert_in_delta 5_000, @sorter.size_in_bytes('5K')
22
+ end
23
+
24
+ def test_should_return_size_with_m_suffix
25
+ assert_in_delta 5_000_000, @sorter.size_in_bytes('5M')
26
+ end
27
+
28
+ def test_should_return_size_with_g_suffix
29
+ assert_in_delta 5_000_000_000, @sorter.size_in_bytes('5G')
30
+ end
31
+
32
+ def test_should_return_size_with_t_suffix
33
+ assert_in_delta 5_000_000_000_000, @sorter.size_in_bytes('5T')
34
+ end
35
+
36
+ def test_should_extract_size_token_from_line
37
+ assert_equal '54K', @sorter.size_token(' 54K foo/bar/3')
38
+ end
39
+
40
+ def test_should_sort_by_byte_size
41
+ lines = [
42
+ '5T in terabytes',
43
+ '6G in gigabytes',
44
+ '7M in megabytes',
45
+ '8K in kilobytes',
46
+ '9B in bytes',
47
+ '9 in bytes'
48
+ ]
49
+ @sorter.lines = lines
50
+ assert_equal lines.reverse, @sorter.sorted
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sort_bytes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Riddle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Sorts lines by human-readable byte size (like the output of `du -h`)
56
+ email:
57
+ - jon@jdrnetworking.com
58
+ executables:
59
+ - sort_bytes
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/sort_bytes
69
+ - lib/sort_bytes.rb
70
+ - lib/sort_bytes/version.rb
71
+ - sort_bytes.gemspec
72
+ - test/minitest_helper.rb
73
+ - test/test_sort_bytes.rb
74
+ homepage: https://github.com/jdrnetworking/sort_bytes
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.1.4
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Sorts lines by human-readable byte size
98
+ test_files:
99
+ - test/minitest_helper.rb
100
+ - test/test_sort_bytes.rb