naturally 2.1.0 → 2.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
- SHA1:
3
- metadata.gz: 3c7fe0629f54a4f0418f2ef7bd0b698bac3ed282
4
- data.tar.gz: 87063a64049efd21df9051ebf72b18cc4142e0b1
2
+ SHA256:
3
+ metadata.gz: f7b3bdea2bec4624b2f0725d0e52e05d93cf0638fe8ddc42e8dd8c7b20696920
4
+ data.tar.gz: ddc8c8f12e62e604deb0d8345df5f29464051ca859834747d2a6db1d8f51c263
5
5
  SHA512:
6
- metadata.gz: 6b5485915f455c893d8fc1fd0afd1116c602447e863a54cf8704fe42fb21269f71834d80369ed701389483d063293592b2b342226a608cea0804eda3298b6090
7
- data.tar.gz: ff836d19c5b40dc8a79ae0f6e638dd3272ae51026b2ee3fe1dd7ee8a07e1e1d9ef9a4880a3b724ed193a92aa36d9816f12963c56b2c3c5cff26c0cbcf311d9c5
6
+ metadata.gz: 602b9798ae11958dd850f1f992a7217cf4b981ccdb7446899b410154c5e9d009318cbf8106232e4dc83c3ba7d06b37df148f3c96770f0165d259d92032e9872f
7
+ data.tar.gz: ee6d95f697088d3a6196f64b863dbac96cb2cfc5bf83357af029ed6aa00d0c49cd24345356d12c616ef2780d30785d2bd40a60c42c4321e07047f990094c9b60
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0
4
- - 2.1
5
- - 2.2
3
+ - 2.4
4
+ - 2.5
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # 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)
2
+ [![Gem Version](https://badge.fury.io/rb/naturally.png)](http://badge.fury.io/rb/naturally) [![Build Status](https://travis-ci.org/public-law/naturally.png)](https://travis-ci.org/public-law/naturally)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0ebf4ef97723f2622105/maintainability)](https://codeclimate.com/github/dogweather/naturally/maintainability)
3
4
 
4
5
  Natural (version number) sorting with support for **legal document numbering**, **college course codes**, and **Unicode**.
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
+ 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 Public.Law post [Counting to 10 in Californian](https://blog.public.law/2012/08/07/counting-from-1-to-10-in-californian/).
6
7
 
7
- ##Installation
8
+ ## Installation
8
9
 
9
10
  ```Shell
10
11
  $ gem install naturally
@@ -37,7 +38,7 @@ releases = [
37
38
  ]
38
39
 
39
40
  # Sort by version number
40
- sorted = Naturally.sort_by(releases, :version)
41
+ sorted = Naturally.sort releases, by: :version
41
42
 
42
43
  # Check what we have
43
44
  expect(sorted.map(&:name)).to eq [
@@ -3,12 +3,20 @@ require 'naturally/segment'
3
3
  # A module which performs natural sorting on a variety of number
4
4
  # formats. (See the specs for examples.)
5
5
  module Naturally
6
- # Perform a natural sort.
6
+ # Perform a natural sort. Supports two syntaxes:
7
+ #
8
+ # 1. sort(objects) # Simple arrays
9
+ # 2. sort(objects, by: some_attribute) # Complex objects
7
10
  #
8
11
  # @param [Array<String>] an_array the list of numbers to sort.
12
+ # @param [by] (optional) an attribute of the array by which to sort.
9
13
  # @return [Array<String>] the numbers sorted naturally.
10
- def self.sort(an_array)
11
- an_array.sort_by { |x| normalize(x) }
14
+ def self.sort(an_array, by:nil)
15
+ if by.nil?
16
+ an_array.sort_by { |x| normalize(x) }
17
+ else
18
+ self.sort_by(an_array, by)
19
+ end
12
20
  end
13
21
 
14
22
  # Sort an array of objects "naturally" by a given attribute.
@@ -25,9 +33,9 @@ module Naturally
25
33
  an_array.sort_by { |obj| normalize(obj.send(an_attribute)) }
26
34
  end
27
35
 
28
- # Convert the given number an array of {Segment}s.
36
+ # Convert the given number to an array of {Segment}s.
29
37
  # This enables it to be sorted against other arrays
30
- # by the standard #sort method.
38
+ # by the built-in #sort method.
31
39
  #
32
40
  # For example, '1.2a.3' becomes
33
41
  # [Segment<'1'>, Segment<'2a'>, Segment<'3'>]
@@ -1,7 +1,8 @@
1
1
  module Naturally
2
2
  # An entity which can be compared to other like elements for
3
3
  # sorting. It's an object representing
4
- # a value which implements the {Comparable} interface.
4
+ # a value which implements the {Comparable} interface which can
5
+ # convert itself to an array.
5
6
  class Segment
6
7
  include Comparable
7
8
 
@@ -25,7 +26,7 @@ module Naturally
25
26
  # @example a college course code
26
27
  # Segment.new('MATH101').to_array #=> [:str, "MATH", 101]
27
28
  #
28
- # @example Section 633a of the ADEA
29
+ # @example Section 633a of the U.S. Age Discrimination in Employment Act
29
30
  # Segment.new('633a').to_array #=> [:int, 633, "a"]
30
31
  def to_array
31
32
  # TODO: Refactor, probably via polymorphism
@@ -1,4 +1,4 @@
1
1
  module Naturally
2
2
  # Gem version
3
- VERSION = '2.1.0'
3
+ VERSION = '2.2.0'
4
4
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.version = Naturally::VERSION
8
8
  gem.license = 'MIT'
9
9
  gem.authors = ["Robb Shecter"]
10
- gem.email = ["robb@weblaws.org"]
10
+ gem.email = ["robb@public.law"]
11
11
  gem.summary = %q{Sorts numbers according to the way people are used to seeing them.}
12
12
  gem.description = %q{Natural Sorting with support for legal numbering, course numbers, and other number/letter mixes.}
13
13
  gem.homepage = "http://github.com/dogweather/naturally"
@@ -2,6 +2,7 @@
2
2
  require 'naturally'
3
3
 
4
4
  describe Naturally do
5
+ # Just a helper for these tests
5
6
  def it_sorts(opts = {})
6
7
  this = opts[:this]
7
8
  to_this = opts[:to_this]
@@ -9,59 +10,84 @@ describe Naturally do
9
10
  expect(actual).to eq(to_this)
10
11
  end
11
12
 
13
+
12
14
  describe '#sort' do
15
+ it 'supports a nicer by: syntax' do
16
+ UbuntuVersion ||= Struct.new(:name, :version)
17
+ releases = [
18
+ UbuntuVersion.new('Saucy Salamander', '13.10'),
19
+ UbuntuVersion.new('Raring Ringtail', '13.04'),
20
+ UbuntuVersion.new('Precise Pangolin', '12.04.4'),
21
+ UbuntuVersion.new('Maverick Meerkat', '10.10'),
22
+ UbuntuVersion.new('Quantal Quetzal', '12.10'),
23
+ UbuntuVersion.new('Lucid Lynx', '10.04.4')
24
+ ]
25
+
26
+ actual = Naturally.sort releases, by: :version
27
+
28
+ expect(actual.map(&:name)).to eq [
29
+ 'Lucid Lynx',
30
+ 'Maverick Meerkat',
31
+ 'Precise Pangolin',
32
+ 'Quantal Quetzal',
33
+ 'Raring Ringtail',
34
+ 'Saucy Salamander'
35
+ ]
36
+ end
37
+
38
+
13
39
  it 'sorts an array of strings nicely as if they were legal numbers' do
14
40
  it_sorts(
15
- this: %w(676 676.1 676.11 676.12 676.2 676.3 676.9 676.10),
41
+ this: %w(676 676.1 676.11 676.12 676.2 676.3 676.9 676.10),
16
42
  to_this: %w(676 676.1 676.2 676.3 676.9 676.10 676.11 676.12)
17
43
  )
18
44
  end
19
45
 
20
46
  it 'sorts a more complex list of strings' do
21
47
  it_sorts(
22
- this: %w(350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.45 354.5),
48
+ this: %w(350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.45 354.5),
23
49
  to_this: %w(350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.5 354.45)
24
50
  )
25
51
  end
26
52
 
27
53
  it 'sorts when numbers have letters in them' do
28
54
  it_sorts(
29
- this: %w(335 335.1 336a 336 337 337a 337.1 337.15 337.2),
55
+ this: %w(335 335.1 336a 336 337 337a 337.1 337.15 337.2),
30
56
  to_this: %w(335 335.1 336 336a 337 337.1 337.2 337.15 337a)
31
57
  )
32
58
  end
33
59
 
34
60
  it 'sorts when numbers have unicode letters in them' do
35
61
  it_sorts(
36
- this: %w(335 335.1 336a 336 337 337я 337.1 337.15 337.2),
62
+ this: %w(335 335.1 336a 336 337 337я 337.1 337.15 337.2),
37
63
  to_this: %w(335 335.1 336 336a 337 337.1 337.2 337.15 337я)
38
64
  )
39
65
  end
40
66
 
41
67
  it 'sorts when letters have numbers in them' do
42
68
  it_sorts(
43
- this: %w(PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, PBLI, SBP1, SBP3),
69
+ this: %w(PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, PBLI, SBP1, SBP3),
44
70
  to_this: %w(PBLI, PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, SBP1, SBP3)
45
71
  )
46
72
  end
47
73
 
48
74
  it 'sorts when letters have numbers and unicode characters in them' do
49
75
  it_sorts(
50
- this: %w(АБ4, АБ2, АБ10, АБ12, АБ1, АБ3, АД8, АД5, АЩФ12, АЩФ8, ЫВА1),
76
+ this: %w(АБ4, АБ2, АБ10, АБ12, АБ1, АБ3, АД8, АД5, АЩФ12, АЩФ8, ЫВА1),
51
77
  to_this: %w(АБ1, АБ2, АБ3, АБ4, АБ10, АБ12, АД5, АД8, АЩФ8, АЩФ12, ЫВА1)
52
78
  )
53
79
  end
54
80
 
55
81
  it 'sorts double digits with letters correctly' do
56
82
  it_sorts(
57
- this: %w(12a 12b 12c 13a 13b 2 3 4 5 10 11 12),
83
+ this: %w(12a 12b 12c 13a 13b 2 3 4 5 10 11 12),
58
84
  to_this: %w(2 3 4 5 10 11 12 12a 12b 12c 13a 13b)
59
85
  )
60
86
  end
61
87
 
62
88
  it 'sorts double digits with unicode letters correctly' do
63
89
  it_sorts(
64
- this: %w(12а 12б 12в 13а 13б 2 3 4 5 10 11 12),
90
+ this: %w(12а 12б 12в 13а 13б 2 3 4 5 10 11 12),
65
91
  to_this: %w(2 3 4 5 10 11 12 12а 12б 12в 13а 13б)
66
92
  )
67
93
  end
@@ -75,28 +101,28 @@ describe Naturally do
75
101
 
76
102
  it 'sorts letters with digits correctly' do
77
103
  it_sorts(
78
- this: %w(1 a 2 b 3 c),
104
+ this: %w(1 a 2 b 3 c),
79
105
  to_this: %w(1 2 3 a b c)
80
106
  )
81
107
  end
82
108
 
83
109
  it 'sorts complex numbers with digits correctly' do
84
110
  it_sorts(
85
- this: %w(1 a 2 b 3 c 1.1 a.1 1.2 a.2 1.3 a.3 b.1 ),
111
+ this: %w(1 a 2 b 3 c 1.1 a.1 1.2 a.2 1.3 a.3 b.1 ),
86
112
  to_this: %w(1 1.1 1.2 1.3 2 3 a a.1 a.2 a.3 b b.1 c)
87
113
  )
88
114
  end
89
115
 
90
116
  it 'sorts complex mixes of numbers and digits correctly' do
91
117
  it_sorts(
92
- this: %w( 1.a.1 1.1 ),
118
+ this: %w( 1.a.1 1.1 ),
93
119
  to_this: %w( 1.1 1.a.1 )
94
120
  )
95
121
  end
96
122
 
97
123
  it 'sorts complex mixes of numbers and digits correctly' do
98
124
  it_sorts(
99
- this: %w( 1a1 1aa aaa ),
125
+ this: %w( 1a1 1aa aaa ),
100
126
  to_this: %w( 1aa 1a1 aaa )
101
127
  )
102
128
  end
@@ -104,7 +130,7 @@ describe Naturally do
104
130
 
105
131
  describe '#sort_naturally_by' do
106
132
  it 'sorts by an attribute' do
107
- UbuntuVersion = Struct.new(:name, :version)
133
+ UbuntuVersion ||= Struct.new(:name, :version)
108
134
  releases = [
109
135
  UbuntuVersion.new('Saucy Salamander', '13.10'),
110
136
  UbuntuVersion.new('Raring Ringtail', '13.04'),
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naturally
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.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: 2015-10-30 00:00:00.000000000 Z
11
+ date: 2018-06-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Natural Sorting with support for legal numbering, course numbers, and
14
14
  other number/letter mixes.
15
15
  email:
16
- - robb@weblaws.org
16
+ - robb@public.law
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  version: '0'
50
50
  requirements: []
51
51
  rubyforge_project:
52
- rubygems_version: 2.4.8
52
+ rubygems_version: 2.7.4
53
53
  signing_key:
54
54
  specification_version: 4
55
55
  summary: Sorts numbers according to the way people are used to seeing them.