naturally 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +11 -5
- data/lib/naturally.rb +16 -13
- data/lib/naturally/version.rb +1 -1
- data/spec/naturally_spec.rb +44 -1
- metadata +2 -3
- data/Gemfile.lock +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7f13718d03ab6f2f6e7d7fc6f3aa44683aa14ac
|
4
|
+
data.tar.gz: d0576fae7b11c7a543fb9a78d06a74805ee171ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8dd2bf1648705cf27085b9e67900def932a3d470577dcaeee866c2a0e8461cc5aef5e26d57d8f43b1e262ac125603d6a32c1f135f26bca22d708a303d9262b3
|
7
|
+
data.tar.gz: ac2984cf0e59d68ec9c27a3b73a29e066c8f90e48749c5b67661b201a878758904f96087a21a8ae7987560184fb78793b2ea568c34e8c6ce7eb44e9132637d42
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
[![Gem Version](https://badge.fury.io/rb/naturally.png)](http://badge.fury.io/rb/naturally) [![Build Status](https://travis-ci.org/dogweather/naturally.png)](https://travis-ci.org/dogweather/naturally) [![Code Climate](https://codeclimate.com/github/dogweather/naturally.png)](https://codeclimate.com/github/dogweather/naturally)
|
3
3
|
|
4
4
|
Natural (version number) sorting with added support for legal document numbering.
|
5
|
-
See [Sorting for Humans
|
6
|
-
for the motivations to make this library.
|
5
|
+
See Jeff Atwood's [Sorting for Humans: Natural Sort Order](http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html) and the Weblaws.org post [Counting to 10 in Californian](http://www.weblaws.org/blog/2012/08/counting-from-1-to-10-in-californian/)
|
6
|
+
for the motivations to make this library.
|
7
7
|
|
8
8
|
The core of the search is [from here](https://github.com/ahoward/version_sorter). I then made
|
9
9
|
several changes to handle the particular types of numbers that come up in statutes, such
|
@@ -37,14 +37,16 @@ require 'naturally'
|
|
37
37
|
Naturally.sort(["1.1", "1.10", "1.2"]) # => ["1.1", "1.2", "1.10"]
|
38
38
|
```
|
39
39
|
|
40
|
-
Usually
|
41
|
-
object:
|
40
|
+
Usually the library is used to sort an array of objects:
|
42
41
|
|
43
42
|
|
44
43
|
```Ruby
|
45
44
|
describe '#sort_naturally_by' do
|
46
45
|
it 'sorts by an attribute' do
|
46
|
+
# Define a new simple object for storing Ubuntu versions
|
47
47
|
UbuntuVersion = Struct.new(:name, :version)
|
48
|
+
|
49
|
+
# Create an array
|
48
50
|
releases = [
|
49
51
|
UbuntuVersion.new('Saucy Salamander', '13.10'),
|
50
52
|
UbuntuVersion.new('Raring Ringtail', '13.04'),
|
@@ -53,7 +55,11 @@ object:
|
|
53
55
|
UbuntuVersion.new('Quantal Quetzal', '12.10'),
|
54
56
|
UbuntuVersion.new('Lucid Lynx', '10.04.4')
|
55
57
|
]
|
58
|
+
|
59
|
+
# Sort by version number
|
56
60
|
sorted = Naturally.sort_by(releases, :version)
|
61
|
+
|
62
|
+
# Check what we have
|
57
63
|
expect(sorted.map(&:name)).to eq [
|
58
64
|
'Lucid Lynx',
|
59
65
|
'Maverick Meerkat',
|
@@ -66,7 +72,7 @@ object:
|
|
66
72
|
end
|
67
73
|
```
|
68
74
|
|
69
|
-
See [the spec for more examples](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb).
|
75
|
+
See [the spec for more examples](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb) of what Naturally can sort.
|
70
76
|
|
71
77
|
|
72
78
|
## Contributing
|
data/lib/naturally.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'naturally/version'
|
2
4
|
|
3
5
|
module Naturally
|
@@ -7,7 +9,7 @@ module Naturally
|
|
7
9
|
# @return [Array<String>] the numbers sorted naturally.
|
8
10
|
def self.sort(an_array)
|
9
11
|
return an_array.sort_by { |x| normalize(x) }
|
10
|
-
end
|
12
|
+
end
|
11
13
|
|
12
14
|
def self.sort_by(an_array, an_attribute)
|
13
15
|
an_array.sort_by{|i| Naturally.normalize(i.send(an_attribute))}
|
@@ -20,7 +22,7 @@ module Naturally
|
|
20
22
|
# @return [Array<NumberElement>] an array of NumberElements which is
|
21
23
|
# able to be sorted naturally via a normal 'sort'.
|
22
24
|
def self.normalize(number)
|
23
|
-
number.to_s.scan(%r
|
25
|
+
number.to_s.scan(%r/\p{Word}+/o).map { |i| NumberElement.new(i) }
|
24
26
|
end
|
25
27
|
|
26
28
|
private
|
@@ -49,7 +51,7 @@ module Naturally
|
|
49
51
|
return reverse_simple_normalize(@val) <=> reverse_simple_normalize(other.val)
|
50
52
|
end
|
51
53
|
|
52
|
-
|
54
|
+
@val <=> other.val
|
53
55
|
end
|
54
56
|
|
55
57
|
def either_is_letters_followed_by_numbers(other)
|
@@ -67,29 +69,30 @@ module Naturally
|
|
67
69
|
def pure_integer?
|
68
70
|
@val =~ /^\d+$/
|
69
71
|
end
|
70
|
-
|
72
|
+
|
71
73
|
def numbers_with_letters?
|
72
|
-
val =~ /^\d
|
74
|
+
val =~ /^\d+\p{Alpha}+$/
|
73
75
|
end
|
74
76
|
|
75
77
|
def letters_with_numbers?
|
76
|
-
val =~
|
78
|
+
val =~ /^\p{Alpha}+\d+$/
|
77
79
|
end
|
78
|
-
|
80
|
+
|
79
81
|
def simple_normalize(n)
|
80
|
-
if n =~ /^(\d+)(
|
81
|
-
|
82
|
+
if n =~ /^(\d+)(\p{Alpha}+)$/
|
83
|
+
[$1.to_i, $2]
|
82
84
|
else
|
83
|
-
|
85
|
+
[n.to_i]
|
84
86
|
end
|
85
87
|
end
|
86
88
|
|
87
89
|
def reverse_simple_normalize(n)
|
88
|
-
if n =~ /^(
|
89
|
-
|
90
|
+
if n =~ /^(\p{Alpha}+)(\d+)$/
|
91
|
+
[$1, $2.to_i]
|
90
92
|
else
|
91
|
-
|
93
|
+
[n.to_s]
|
92
94
|
end
|
93
95
|
end
|
96
|
+
|
94
97
|
end
|
95
98
|
end
|
data/lib/naturally/version.rb
CHANGED
data/spec/naturally_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'naturally'
|
2
4
|
|
3
5
|
describe Naturally do
|
@@ -20,17 +22,35 @@ describe Naturally do
|
|
20
22
|
Naturally.sort(a).should == b
|
21
23
|
end
|
22
24
|
|
25
|
+
it 'sorts when numbers have unicode letters in them' do
|
26
|
+
a = %w[335 335.1 336a 336 337 337я 337.1 337.15 337.2]
|
27
|
+
b = %w[335 335.1 336 336a 337 337.1 337.2 337.15 337я]
|
28
|
+
Naturally.sort(a).should == b
|
29
|
+
end
|
30
|
+
|
23
31
|
it 'sorts when letters have numbers in them' do
|
24
32
|
a = %w[PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, PBLI, SBP1, SBP3]
|
25
33
|
b = %w[PBLI, PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, SBP1, SBP3]
|
26
34
|
Naturally.sort(a).should == b
|
27
35
|
end
|
28
|
-
|
36
|
+
|
37
|
+
it 'sorts when letters have numbers and unicode characters in them' do
|
38
|
+
a = %w[АБ4, АБ2, АБ10, АБ12, АБ1, АБ3, АД8, АД5, АЩФ12, АЩФ8, ЫВА1]
|
39
|
+
b = %w[АБ1, АБ2, АБ3, АБ4, АБ10, АБ12, АД5, АД8, АЩФ8, АЩФ12, ЫВА1]
|
40
|
+
Naturally.sort(a).should == b
|
41
|
+
end
|
42
|
+
|
29
43
|
it 'sorts double digits with letters correctly' do
|
30
44
|
a = %w[12a 12b 12c 13a 13b 2 3 4 5 10 11 12]
|
31
45
|
b = %w[2 3 4 5 10 11 12 12a 12b 12c 13a 13b]
|
32
46
|
Naturally.sort(a).should == b
|
33
47
|
end
|
48
|
+
|
49
|
+
it 'sorts double digits with unicode letters correctly' do
|
50
|
+
a = %w[12а 12б 12в 13а 13б 2 3 4 5 10 11 12]
|
51
|
+
b = %w[2 3 4 5 10 11 12 12а 12б 12в 13а 13б]
|
52
|
+
Naturally.sort(a).should == b
|
53
|
+
end
|
34
54
|
end
|
35
55
|
|
36
56
|
describe '#sort_naturally_by' do
|
@@ -54,5 +74,28 @@ describe Naturally do
|
|
54
74
|
'Saucy Salamander'
|
55
75
|
]
|
56
76
|
end
|
77
|
+
|
78
|
+
it 'sorts by an attribute which contains unicode characters' do
|
79
|
+
Thing = Struct.new(:number, :name)
|
80
|
+
objects = [
|
81
|
+
Thing.new('1.1', 'Москва'),
|
82
|
+
Thing.new('1.2', 'Киев'),
|
83
|
+
Thing.new('1.1.1', 'Париж'),
|
84
|
+
Thing.new('1.1.2', 'Будапешт'),
|
85
|
+
Thing.new('1.10', 'Брест'),
|
86
|
+
Thing.new('2.1', 'Калуга'),
|
87
|
+
Thing.new('1.3', 'Васюки')
|
88
|
+
]
|
89
|
+
sorted = objects.sort_by{ |o| Naturally.normalize(o.name) }
|
90
|
+
sorted.map{|o| o.name}.should == %w[
|
91
|
+
Брест
|
92
|
+
Будапешт
|
93
|
+
Васюки
|
94
|
+
Калуга
|
95
|
+
Киев
|
96
|
+
Москва
|
97
|
+
Париж
|
98
|
+
]
|
99
|
+
end
|
57
100
|
end
|
58
101
|
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.3.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-03-
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Natural Sorting with support for legal numbering
|
14
14
|
email:
|
@@ -20,7 +20,6 @@ files:
|
|
20
20
|
- ".gitignore"
|
21
21
|
- ".travis.yml"
|
22
22
|
- Gemfile
|
23
|
-
- Gemfile.lock
|
24
23
|
- LICENSE.txt
|
25
24
|
- README.md
|
26
25
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
naturally (1.2.1)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
diff-lcs (1.2.5)
|
10
|
-
rake (10.2.1)
|
11
|
-
rspec (2.14.1)
|
12
|
-
rspec-core (~> 2.14.0)
|
13
|
-
rspec-expectations (~> 2.14.0)
|
14
|
-
rspec-mocks (~> 2.14.0)
|
15
|
-
rspec-core (2.14.8)
|
16
|
-
rspec-expectations (2.14.5)
|
17
|
-
diff-lcs (>= 1.1.3, < 2.0)
|
18
|
-
rspec-mocks (2.14.6)
|
19
|
-
|
20
|
-
PLATFORMS
|
21
|
-
ruby
|
22
|
-
|
23
|
-
DEPENDENCIES
|
24
|
-
naturally!
|
25
|
-
rake (~> 10.0)
|
26
|
-
rspec (~> 2.0)
|