naturally 1.1.0 → 1.2.0
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 +4 -4
- data/.travis.yml +3 -1
- data/README.md +24 -26
- data/lib/naturally.rb +27 -7
- data/lib/naturally/version.rb +1 -1
- data/spec/naturally_spec.rb +21 -29
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2fe9301244de764ea34a2f403745a4bbc11815d
|
4
|
+
data.tar.gz: 8b13bf9eb6958c150019c5d09e0d778bb953ddf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b016f2539c1f2de5f3e47753d9122f1fec032e187e54f9723be8225c471f89f65b2bad005385acfe6cdbd554a924d778e29c1356120099d4da4c8a91ac9abc7a
|
7
|
+
data.tar.gz: 39bd27cd6fa8c287559cf906b210d6b6d33c731338a6b0a8431fc85a08ab3b8530fcdf0be1e5b5c7cd64823251eb0a093c9049c60de2c3016c0a85b66d7b292c
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Naturally
|
2
|
-
[](https://travis-ci.org/dogweather/naturally) [](https://codeclimate.com/github/dogweather/naturally)
|
2
|
+
[](http://badge.fury.io/rb/naturally) [](https://travis-ci.org/dogweather/naturally) [](https://codeclimate.com/github/dogweather/naturally)
|
3
3
|
|
4
|
-
Natural sorting with added support for legal document numbering.
|
4
|
+
Natural (version number) sorting with added support for legal document numbering.
|
5
5
|
See [Sorting for Humans : Natural Sort Order](http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html) and [Counting to 10 in Californian](http://www.weblaws.org/blog/2012/08/counting-from-1-to-10-in-californian/)
|
6
6
|
for the motivations to make this library. This is also the kind of ordering you want if you're sorting version numbers.
|
7
7
|
|
@@ -42,30 +42,28 @@ object:
|
|
42
42
|
|
43
43
|
|
44
44
|
```Ruby
|
45
|
-
#
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
<struct Thing number="2", name="Canada">,
|
68
|
-
<struct Thing number="2.1", name="British Columbia">]
|
45
|
+
describe '#sort_naturally_by' do
|
46
|
+
it 'sorts by an attribute' do
|
47
|
+
UbuntuVersion = Struct.new(:name, :version)
|
48
|
+
releases = [
|
49
|
+
UbuntuVersion.new('Saucy Salamander', '13.10'),
|
50
|
+
UbuntuVersion.new('Raring Ringtail', '13.04'),
|
51
|
+
UbuntuVersion.new('Precise Pangolin', '12.04.4'),
|
52
|
+
UbuntuVersion.new('Maverick Meerkat', '10.10'),
|
53
|
+
UbuntuVersion.new('Quantal Quetzal', '12.10'),
|
54
|
+
UbuntuVersion.new('Lucid Lynx', '10.04.4')
|
55
|
+
]
|
56
|
+
sorted = Naturally.sort_by(releases, :version)
|
57
|
+
expect(sorted.map(&:name)).to eq [
|
58
|
+
'Lucid Lynx',
|
59
|
+
'Maverick Meerkat',
|
60
|
+
'Precise Pangolin',
|
61
|
+
'Quantal Quetzal',
|
62
|
+
'Raring Ringtail',
|
63
|
+
'Saucy Salamander'
|
64
|
+
]
|
65
|
+
end
|
66
|
+
end
|
69
67
|
```
|
70
68
|
|
71
69
|
See [the spec for more examples](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb).
|
data/lib/naturally.rb
CHANGED
@@ -9,6 +9,10 @@ module Naturally
|
|
9
9
|
return an_array.sort_by { |x| normalize(x) }
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.sort_by(an_array, an_attribute)
|
13
|
+
an_array.sort_by{|i| Naturally.normalize(i.send(an_attribute))}
|
14
|
+
end
|
15
|
+
|
12
16
|
# Convert the given number into an object that can be sorted
|
13
17
|
# naturally. This object is an array of {NumberElement} instances.
|
14
18
|
#
|
@@ -33,17 +37,33 @@ module Naturally
|
|
33
37
|
end
|
34
38
|
|
35
39
|
def <=>(other)
|
36
|
-
if
|
40
|
+
if both_are_integers_without_letters(other)
|
37
41
|
return @val.to_i <=> other.val.to_i
|
38
|
-
|
42
|
+
end
|
43
|
+
|
44
|
+
if either_is_numbers_followed_by_letters(other)
|
39
45
|
return simple_normalize(@val) <=> simple_normalize(other.val)
|
40
|
-
|
46
|
+
end
|
47
|
+
|
48
|
+
if either_is_letters_followed_by_numbers(other)
|
41
49
|
return reverse_simple_normalize(@val) <=> reverse_simple_normalize(other.val)
|
42
|
-
else
|
43
|
-
return @val <=> other.val
|
44
50
|
end
|
51
|
+
|
52
|
+
return @val <=> other.val
|
45
53
|
end
|
46
|
-
|
54
|
+
|
55
|
+
def either_is_letters_followed_by_numbers(other)
|
56
|
+
letters_with_numbers? || other.letters_with_numbers?
|
57
|
+
end
|
58
|
+
|
59
|
+
def either_is_numbers_followed_by_letters(other)
|
60
|
+
numbers_with_letters? || other.numbers_with_letters?
|
61
|
+
end
|
62
|
+
|
63
|
+
def both_are_integers_without_letters(other)
|
64
|
+
pure_integer? && other.pure_integer?
|
65
|
+
end
|
66
|
+
|
47
67
|
def pure_integer?
|
48
68
|
@val =~ /^\d+$/
|
49
69
|
end
|
@@ -72,4 +92,4 @@ module Naturally
|
|
72
92
|
end
|
73
93
|
end
|
74
94
|
end
|
75
|
-
end
|
95
|
+
end
|
data/lib/naturally/version.rb
CHANGED
data/spec/naturally_spec.rb
CHANGED
@@ -8,12 +8,6 @@ describe Naturally do
|
|
8
8
|
Naturally.sort(a).should == b
|
9
9
|
end
|
10
10
|
|
11
|
-
it 'sorts a smaller array of strings nicely as if they were legal numbers' do
|
12
|
-
a = %w[676 676.1 676.11 676.12 676.2 676.3 676.9]
|
13
|
-
b = %w[676 676.1 676.2 676.3 676.9 676.11 676.12]
|
14
|
-
Naturally.sort(a).should == b
|
15
|
-
end
|
16
|
-
|
17
11
|
it 'sorts a more complex list of strings' do
|
18
12
|
a = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.45 354.5]
|
19
13
|
b = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.5 354.45]
|
@@ -39,28 +33,26 @@ describe Naturally do
|
|
39
33
|
end
|
40
34
|
end
|
41
35
|
|
42
|
-
describe '#
|
43
|
-
it '
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
sorted
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
weight
|
63
|
-
]
|
36
|
+
describe '#sort_naturally_by' do
|
37
|
+
it 'sorts by an attribute' do
|
38
|
+
UbuntuVersion = Struct.new(:name, :version)
|
39
|
+
releases = [
|
40
|
+
UbuntuVersion.new('Saucy Salamander', '13.10'),
|
41
|
+
UbuntuVersion.new('Raring Ringtail', '13.04'),
|
42
|
+
UbuntuVersion.new('Precise Pangolin', '12.04.4'),
|
43
|
+
UbuntuVersion.new('Maverick Meerkat', '10.10'),
|
44
|
+
UbuntuVersion.new('Quantal Quetzal', '12.10'),
|
45
|
+
UbuntuVersion.new('Lucid Lynx', '10.04.4')
|
46
|
+
]
|
47
|
+
sorted = Naturally.sort_by(releases, :version)
|
48
|
+
expect(sorted.map(&:name)).to eq [
|
49
|
+
'Lucid Lynx',
|
50
|
+
'Maverick Meerkat',
|
51
|
+
'Precise Pangolin',
|
52
|
+
'Quantal Quetzal',
|
53
|
+
'Raring Ringtail',
|
54
|
+
'Saucy Salamander'
|
55
|
+
]
|
64
56
|
end
|
65
57
|
end
|
66
|
-
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: naturally
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Shecter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Natural Sorting with support for legal numbering
|
14
14
|
email:
|
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
47
|
version: '0'
|
48
48
|
requirements: []
|
49
49
|
rubyforge_project:
|
50
|
-
rubygems_version: 2.2.
|
50
|
+
rubygems_version: 2.2.2
|
51
51
|
signing_key:
|
52
52
|
specification_version: 4
|
53
53
|
summary: Sorts numbers according to the way people are used to seeing them.
|