naturally 1.0.3 → 1.0.4
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 +7 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/README.md +32 -18
- data/Rakefile +6 -0
- data/lib/naturally.rb +4 -4
- data/lib/naturally/version.rb +1 -1
- metadata +8 -9
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 779c19dc2b8a8008edc6044aea5dd581d04fd231
|
4
|
+
data.tar.gz: 51fc1ee3a6591b169e3823d38c26df50bb58c7ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49b59c6d73db65805dc0e3d86fe7d8508e889d4634d0c9604e7fba9422e552c7dca242cdf34b0f8a534d912a3d1406a1d30d2d0ad6c7275c224871efc0087d85
|
7
|
+
data.tar.gz: 5d996f19d784d877090477bc82e340035506f2f9dbebf4acc03156f43e84740459e9c2b8cc7602f9ea8e866191d229a3acee3b887a1b4456b54560df207ad3f3
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# Naturally
|
2
|
+
[](https://travis-ci.org/dogweather/naturally)
|
2
3
|
|
3
|
-
Natural sorting with support for legal document numbering.
|
4
|
-
See [Counting to 10 in Californian](http://www.weblaws.org/blog/2012/08/counting-from-1-to-10-in-californian/)
|
5
|
-
for the motivations to make this library.
|
4
|
+
Natural sorting with added support for legal document numbering.
|
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
|
+
for the motivations to make this library. This is also the kind of ordering you want if you're sorting version numbers.
|
6
7
|
|
7
8
|
The core of the search is [from here](https://github.com/ahoward/version_sorter). I then made
|
8
9
|
several changes to handle the particular types of numbers that come up in statutes, such
|
@@ -19,7 +20,7 @@ And then execute:
|
|
19
20
|
|
20
21
|
$ bundle
|
21
22
|
|
22
|
-
Or install it
|
23
|
+
Or install it outside of bundler with:
|
23
24
|
|
24
25
|
$ gem install naturally
|
25
26
|
|
@@ -27,28 +28,41 @@ Or install it yourself as:
|
|
27
28
|
## Usage
|
28
29
|
|
29
30
|
```Ruby
|
31
|
+
require 'naturally'
|
32
|
+
|
30
33
|
# Sort a simple array of strings
|
31
34
|
Naturally.sort(["1.1", "1.10", "1.2"]) # => ["1.1", "1.2", "1.10"]
|
35
|
+
```
|
36
|
+
|
37
|
+
Usually, however, the library is used to sort an array of some kind of
|
38
|
+
object:
|
32
39
|
|
40
|
+
|
41
|
+
```Ruby
|
33
42
|
# Sort an array of objects by one attribute
|
34
43
|
Thing = Struct.new(:number, :name)
|
35
44
|
objects = [
|
36
|
-
Thing.new('1
|
37
|
-
Thing.new('
|
38
|
-
Thing.new('1.1
|
39
|
-
Thing.new('1.
|
40
|
-
Thing.new('1.
|
41
|
-
Thing.new('
|
42
|
-
Thing.new('1
|
45
|
+
Thing.new('1', 'USA'),
|
46
|
+
Thing.new('2', 'Canada'),
|
47
|
+
Thing.new('1.1', 'Oregon'),
|
48
|
+
Thing.new('1.2', 'Washington'),
|
49
|
+
Thing.new('1.1.1', 'Portland'),
|
50
|
+
Thing.new('1.10', 'Texas'),
|
51
|
+
Thing.new('2.1', 'British Columbia'),
|
52
|
+
Thing.new('1.3', 'California'),
|
53
|
+
Thing.new('1.1.2', 'Eugene')
|
43
54
|
]
|
44
55
|
objects.sort_by{ |o| Naturally.normalize(o.number) }
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
56
|
+
|
57
|
+
# Results in:
|
58
|
+
[<struct Thing number="1.1", name="Oregon">,
|
59
|
+
<struct Thing number="1.1.1", name="Portland">,
|
60
|
+
<struct Thing number="1.1.2", name="Eugene">,
|
61
|
+
<struct Thing number="1.2", name="Washington">,
|
62
|
+
<struct Thing number="1.3", name="California">,
|
63
|
+
<struct Thing number="1.10", name="Texas">,
|
64
|
+
<struct Thing number="2", name="Canada">,
|
65
|
+
<struct Thing number="2.1", name="British Columbia">]
|
52
66
|
```
|
53
67
|
|
54
68
|
See [the spec for more examples](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb).
|
data/Rakefile
CHANGED
data/lib/naturally.rb
CHANGED
@@ -9,10 +9,10 @@ module Naturally
|
|
9
9
|
return an_array.sort_by{ |x| normalize(x) }
|
10
10
|
end
|
11
11
|
|
12
|
-
# Convert the given number into
|
12
|
+
# Convert the given number into an object that can be sorted
|
13
13
|
# naturally.
|
14
14
|
# @param [String] number the number in complex form such as 1.2a.3.
|
15
|
-
# @return [Array<NumberElement>] an array of NumberElements which
|
15
|
+
# @return [Array<NumberElement>] an array of NumberElements which is
|
16
16
|
# able to be sorted naturally via a normal 'sort'.
|
17
17
|
def self.normalize(number)
|
18
18
|
number.to_s.scan(%r/[0-9a-zA-Z]+/o).map{|i| NumberElement.new(i)}
|
@@ -32,7 +32,7 @@ module Naturally
|
|
32
32
|
def <=>(other)
|
33
33
|
if pure_integer? && other.pure_integer?
|
34
34
|
return @val.to_i <=> other.val.to_i
|
35
|
-
elsif
|
35
|
+
elsif numbers_with_letters? || other.numbers_with_letters?
|
36
36
|
return simple_normalize(@val) <=> simple_normalize(other.val)
|
37
37
|
else
|
38
38
|
return @val <=> other.val
|
@@ -43,7 +43,7 @@ module Naturally
|
|
43
43
|
@val =~ /^\d+$/
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
46
|
+
def numbers_with_letters?
|
47
47
|
val =~ /^\d+[a-zA-Z]+$/
|
48
48
|
end
|
49
49
|
|
data/lib/naturally/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: naturally
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Robb Shecter
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-09-19 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: Natural Sorting with support for legal numbering
|
15
14
|
email:
|
@@ -19,6 +18,7 @@ extensions: []
|
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
21
20
|
- .gitignore
|
21
|
+
- .travis.yml
|
22
22
|
- Gemfile
|
23
23
|
- LICENSE.txt
|
24
24
|
- README.md
|
@@ -29,27 +29,26 @@ files:
|
|
29
29
|
- spec/naturally_spec.rb
|
30
30
|
homepage: http://github.com/dogweather/naturally
|
31
31
|
licenses: []
|
32
|
+
metadata: {}
|
32
33
|
post_install_message:
|
33
34
|
rdoc_options: []
|
34
35
|
require_paths:
|
35
36
|
- lib
|
36
37
|
required_ruby_version: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
|
-
- -
|
39
|
+
- - '>='
|
39
40
|
- !ruby/object:Gem::Version
|
40
41
|
version: '0'
|
41
|
-
none: false
|
42
42
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - '>='
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
|
-
none: false
|
48
47
|
requirements: []
|
49
48
|
rubyforge_project:
|
50
|
-
rubygems_version: 1.
|
49
|
+
rubygems_version: 2.1.2
|
51
50
|
signing_key:
|
52
|
-
specification_version:
|
51
|
+
specification_version: 4
|
53
52
|
summary: Sorts numbers according to the way people are used to seeing them.
|
54
53
|
test_files:
|
55
54
|
- spec/naturally_spec.rb
|