natsort 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +11 -0
- data/lib/natsort.rb +24 -0
- data/lib/natsort/version.rb +3 -0
- data/natsort.gemspec +25 -0
- data/test/array_test.rb +71 -0
- data/test/enumerable_test.rb +67 -0
- data/test/test_helper.rb +5 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a3b696276dd498d31dea76c7d022db1958adde30
|
4
|
+
data.tar.gz: ad37996668e96049f8b642a28aa07594dacd259c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bdaf1bc55971aed4d0c55ccbb2da7733bcb532128beb987638783b4d773f7f869d045624d281c2206ec3329ff018f3ef11b8e5cb4b410743c668a00b003c141c
|
7
|
+
data.tar.gz: 7e0df7a02b9db4d0cda68beb449b5fd8e0db600dfa5c791591a3d14381969339963103675ec5394fb89775cb189d970d618aa8283739979ad2e5add59a5720bd
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Masato Ikeda
|
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,72 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/a2ikm/natsort.svg)](https://travis-ci.org/a2ikm/natsort)
|
2
|
+
|
3
|
+
# natsort
|
4
|
+
|
5
|
+
This gem provides natural sorting methods to `Enumerable`.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'natsort'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install natsort
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
%w(1 10 2).natsort
|
25
|
+
#=> ["1", "2", "10"]
|
26
|
+
```
|
27
|
+
|
28
|
+
or
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
[
|
32
|
+
{ "n" => "1" },
|
33
|
+
{ "n" => "10" },
|
34
|
+
{ "n" => "2" }
|
35
|
+
].natsort_by { |x| x["n"] }
|
36
|
+
#=> [
|
37
|
+
{ "n" => "1" },
|
38
|
+
{ "n" => "2" },
|
39
|
+
{ "n" => "10" }
|
40
|
+
]
|
41
|
+
```
|
42
|
+
|
43
|
+
These sorts are case sensitive by default.
|
44
|
+
If you want to sort objects in case insensitive, pass `false` to the methods.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
%w(B a c).natsort(false)
|
48
|
+
#=> ["a", "B", "c"]
|
49
|
+
```
|
50
|
+
|
51
|
+
or
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
[
|
55
|
+
{ "n" => "B" },
|
56
|
+
{ "n" => "a" },
|
57
|
+
{ "n" => "c" }
|
58
|
+
].natsort_by(false) { |x| x["n"] }
|
59
|
+
#=> [
|
60
|
+
{ "n" => "a" },
|
61
|
+
{ "n" => "B" },
|
62
|
+
{ "n" => "c" }
|
63
|
+
]
|
64
|
+
```
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it ( https://github.com/a2ikm/natsort/fork )
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/natsort.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "natsort/version"
|
2
|
+
require "natcmp"
|
3
|
+
|
4
|
+
module Enumerable
|
5
|
+
def natsort(case_sensitive = true)
|
6
|
+
ignore_case = !case_sensitive
|
7
|
+
sort { |a, b| Natcmp.natcmp(a.to_s, b.to_s, ignore_case) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def natsort_by(case_sensitive = true, &block)
|
11
|
+
ignore_case = !case_sensitive
|
12
|
+
sort { |a, b| Natcmp.natcmp(block.call(a).to_s, block.call(b).to_s, ignore_case) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Array
|
17
|
+
def natsort!(case_sensitive = true)
|
18
|
+
replace(natsort(case_sensitive))
|
19
|
+
end
|
20
|
+
|
21
|
+
def natsort_by!(case_sensitive = true, &block)
|
22
|
+
replace(natsort_by(case_sensitive, &block))
|
23
|
+
end
|
24
|
+
end
|
data/natsort.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'natsort/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "natsort"
|
8
|
+
spec.version = Natsort::VERSION
|
9
|
+
spec.authors = ["Masato Ikeda"]
|
10
|
+
spec.email = ["masato.ikeda@gmail.com"]
|
11
|
+
spec.summary = %q{Provides natural sorting methods to Enumerable.}
|
12
|
+
spec.description = %q{Provides natural sorting methods to Enumerable.}
|
13
|
+
spec.homepage = "https://github.com/a2ikm/natsort"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_dependency "natcmp"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
data/test/array_test.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ArrayTest < MiniTest::Test
|
4
|
+
def test_destructive_natsort
|
5
|
+
original = ["1", "10", "2"]
|
6
|
+
original.natsort!
|
7
|
+
|
8
|
+
assert_equal ["1", "2", "10"], original
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_destructive_natsort_case_sensitive
|
12
|
+
original = ["a", "B"]
|
13
|
+
original.natsort!
|
14
|
+
|
15
|
+
assert_equal ["B", "a"], original
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_destructive_natsort_case_insensitive
|
19
|
+
original = ["B", "a"]
|
20
|
+
original.natsort!(false)
|
21
|
+
|
22
|
+
assert_equal ["a", "B"], original
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_destructive_natsort_by
|
26
|
+
original = [
|
27
|
+
{ "m" => "1", "n" => "1" },
|
28
|
+
{ "m" => "2", "n" => "10" },
|
29
|
+
{ "m" => "3", "n" => "2" }
|
30
|
+
]
|
31
|
+
original.natsort_by! { |x| x["n"] }
|
32
|
+
|
33
|
+
expected = [
|
34
|
+
{ "m" => "1", "n" => "1" },
|
35
|
+
{ "m" => "3", "n" => "2" },
|
36
|
+
{ "m" => "2", "n" => "10" }
|
37
|
+
]
|
38
|
+
|
39
|
+
assert_equal expected, original
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_destructive_natsort_by_case_sensitive
|
43
|
+
original = [
|
44
|
+
{ "m" => "1", "n" => "a" },
|
45
|
+
{ "m" => "2", "n" => "B" }
|
46
|
+
]
|
47
|
+
original.natsort_by! { |x| x["n"] }
|
48
|
+
|
49
|
+
expected = [
|
50
|
+
{ "m" => "2", "n" => "B" },
|
51
|
+
{ "m" => "1", "n" => "a" }
|
52
|
+
]
|
53
|
+
|
54
|
+
assert_equal expected, original
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_destructive_natsort_by_case_insensitive
|
58
|
+
original = [
|
59
|
+
{ "m" => "1", "n" => "B" },
|
60
|
+
{ "m" => "2", "n" => "a" }
|
61
|
+
]
|
62
|
+
original.natsort_by!(false) { |x| x["n"] }
|
63
|
+
|
64
|
+
expected = [
|
65
|
+
{ "m" => "2", "n" => "a" },
|
66
|
+
{ "m" => "1", "n" => "B" }
|
67
|
+
]
|
68
|
+
|
69
|
+
assert_equal expected, original
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class EnumerableTest < MiniTest::Test
|
4
|
+
def test_natsort_strings
|
5
|
+
actual = ["1", "10", "2"].natsort
|
6
|
+
assert_equal ["1", "2", "10"], actual
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_natsort_numbers
|
10
|
+
actual = [1, 10, 2].natsort
|
11
|
+
assert_equal [1, 2, 10], actual
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_natsort_case_sensitive
|
15
|
+
actual = ["a", "B"].natsort
|
16
|
+
assert_equal ["B", "a"], actual
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_natsort_case_insensitive
|
20
|
+
actual = ["B", "a"].natsort(false)
|
21
|
+
assert_equal ["a", "B"], actual
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_natsort_by
|
25
|
+
actual = [
|
26
|
+
{ "m" => "1", "n" => "1" },
|
27
|
+
{ "m" => "2", "n" => "10" },
|
28
|
+
{ "m" => "3", "n" => "2" }
|
29
|
+
].natsort_by { |x| x["n"] }
|
30
|
+
|
31
|
+
expected = [
|
32
|
+
{ "m" => "1", "n" => "1" },
|
33
|
+
{ "m" => "3", "n" => "2" },
|
34
|
+
{ "m" => "2", "n" => "10" }
|
35
|
+
]
|
36
|
+
|
37
|
+
assert_equal expected, actual
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_natsort_by_case_sensitive
|
41
|
+
actual = [
|
42
|
+
{ "m" => "1", "n" => "a" },
|
43
|
+
{ "m" => "2", "n" => "B" }
|
44
|
+
].natsort_by { |x| x["n"] }
|
45
|
+
|
46
|
+
expected = [
|
47
|
+
{ "m" => "2", "n" => "B" },
|
48
|
+
{ "m" => "1", "n" => "a" }
|
49
|
+
]
|
50
|
+
|
51
|
+
assert_equal expected, actual
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_natsort_by_case_insensitive
|
55
|
+
actual = [
|
56
|
+
{ "m" => "1", "n" => "B" },
|
57
|
+
{ "m" => "2", "n" => "a" }
|
58
|
+
].natsort_by(false) { |x| x["n"] }
|
59
|
+
|
60
|
+
expected = [
|
61
|
+
{ "m" => "2", "n" => "a" },
|
62
|
+
{ "m" => "1", "n" => "B" }
|
63
|
+
]
|
64
|
+
|
65
|
+
assert_equal expected, actual
|
66
|
+
end
|
67
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: natsort
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masato Ikeda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: natcmp
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: Provides natural sorting methods to Enumerable.
|
70
|
+
email:
|
71
|
+
- masato.ikeda@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/natsort.rb
|
83
|
+
- lib/natsort/version.rb
|
84
|
+
- natsort.gemspec
|
85
|
+
- test/array_test.rb
|
86
|
+
- test/enumerable_test.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
homepage: https://github.com/a2ikm/natsort
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Provides natural sorting methods to Enumerable.
|
112
|
+
test_files:
|
113
|
+
- test/array_test.rb
|
114
|
+
- test/enumerable_test.rb
|
115
|
+
- test/test_helper.rb
|
116
|
+
has_rdoc:
|