naturally 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e088049af46b6400067b7fee29e5fcf173f867c5
4
- data.tar.gz: 0f2c8c9786b402bf168826b416229c725805744b
3
+ metadata.gz: b2fe9301244de764ea34a2f403745a4bbc11815d
4
+ data.tar.gz: 8b13bf9eb6958c150019c5d09e0d778bb953ddf6
5
5
  SHA512:
6
- metadata.gz: d0e08aedc2dcaadc4af03ed0ebe01b2ae60bfa1d0f87d950aec091d0a4c52fe02c69ec0b0d398b34f147b5b85b2f587d55429a710e4cdbab500a0dbe2fd7a788
7
- data.tar.gz: ff7350582be9a879f06069d96fca4a9e0429fd610fa212eea2390d4d8815c527eb9132d87590b4aa2bb2844f256433624b116dd6fcbbbedeb55954405ec35bf4
6
+ metadata.gz: b016f2539c1f2de5f3e47753d9122f1fec032e187e54f9723be8225c471f89f65b2bad005385acfe6cdbd554a924d778e29c1356120099d4da4c8a91ac9abc7a
7
+ data.tar.gz: 39bd27cd6fa8c287559cf906b210d6b6d33c731338a6b0a8431fc85a08ab3b8530fcdf0be1e5b5c7cd64823251eb0a093c9049c60de2c3016c0a85b66d7b292c
data/.travis.yml CHANGED
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - "1.9.3"
4
- - "2.0.0"
4
+ - "2.0.0"
5
+ - "2.1.0"
6
+ - "2.1.1"
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Naturally
2
- [![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)
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
- 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
- # Sort an array of objects by one attribute
46
- Thing = Struct.new(:number, :name)
47
- objects = [
48
- Thing.new('1', 'USA'),
49
- Thing.new('2', 'Canada'),
50
- Thing.new('1.1', 'Oregon'),
51
- Thing.new('1.2', 'Washington'),
52
- Thing.new('1.1.1', 'Portland'),
53
- Thing.new('1.10', 'Texas'),
54
- Thing.new('2.1', 'British Columbia'),
55
- Thing.new('1.3', 'California'),
56
- Thing.new('1.1.2', 'Eugene')
57
- ]
58
- objects.sort_by{ |o| Naturally.normalize(o.number) }
59
-
60
- # Results in:
61
- [<struct Thing number="1.1", name="Oregon">,
62
- <struct Thing number="1.1.1", name="Portland">,
63
- <struct Thing number="1.1.2", name="Eugene">,
64
- <struct Thing number="1.2", name="Washington">,
65
- <struct Thing number="1.3", name="California">,
66
- <struct Thing number="1.10", name="Texas">,
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 pure_integer? && other.pure_integer?
40
+ if both_are_integers_without_letters(other)
37
41
  return @val.to_i <=> other.val.to_i
38
- elsif numbers_with_letters? || other.numbers_with_letters?
42
+ end
43
+
44
+ if either_is_numbers_followed_by_letters(other)
39
45
  return simple_normalize(@val) <=> simple_normalize(other.val)
40
- elsif letters_with_numbers? || other.letters_with_numbers?
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
@@ -1,3 +1,3 @@
1
1
  module Naturally
2
- VERSION = "1.1.0"
2
+ VERSION = '1.2.0'
3
3
  end
@@ -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 '#normalize' do
43
- it 'enables sorting objects by one particular attribute' do
44
- Thing = Struct.new(:number, :name)
45
- objects = [
46
- Thing.new('1.1', 'color'),
47
- Thing.new('1.2', 'size'),
48
- Thing.new('1.1.1', 'opacity'),
49
- Thing.new('1.1.2', 'lightness'),
50
- Thing.new('1.10', 'hardness'),
51
- Thing.new('2.1', 'weight'),
52
- Thing.new('1.3', 'shape')
53
- ]
54
- sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }
55
- sorted.map{|o| o.name}.should == %w[
56
- color
57
- opacity
58
- lightness
59
- size
60
- shape
61
- hardness
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.1.0
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-01-18 00:00:00.000000000 Z
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.1
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.