naturalsort 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzVkY2U1YzA2YjJkMDNjNGNmNGZlMjM4NTBkZGU0MjY0OWMxMzE5OA==
5
+ data.tar.gz: !binary |-
6
+ NWQzNzM1ODExZmQ5MDkzYzVkODZlZWQ5N2FiNjhmNDU5N2I2Yjc3Yg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDNhMTkyNWRiMGY0MGUwMzA2MDc5Yjg0ZjRmZTUzZWRiMzEzMjcyYmNmYzQ3
10
+ YWI0MGU1ODYzNDhmOTIzMDc0N2E3YWFlNDZiNDIxMmYyYjI0MTc2YmNjMWU0
11
+ ZWQzMDMzMjA5YzkxOGI1NmI1YjY4ZTExZDQ3MDg1Mzg4MjU3NzI=
12
+ data.tar.gz: !binary |-
13
+ MWFmMmNhZmIxZjY2ZGNhODVkOTZiMzU2Y2I4NGM4NmVmNjM4ZGM5OTQ0ZDY3
14
+ Zjg1YTZlZmIxMWJjNjM3YTBhMTkxYWUwMzMxOGIzZTg3ZjJjNTdjNzAxYjUy
15
+ ZWY1MWM4OTExNzFiOTJlMzJhOWQyZDQ3ZTIzYmMwNTgzZTQzZjY=
@@ -0,0 +1,18 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby-head
8
+ - rbx-19mode
@@ -0,0 +1,49 @@
1
+ # Changelog
2
+
3
+ ## 1.2.0 - 2013-10-10
4
+
5
+ * DEPRECATION: deprecate `NaturalSort.naturalsort` and replace with `NaturalSort.sort`
6
+ * Use `Array(object)` instead of `object.to_a` for implicit Array conversion
7
+ * Remove unused private instance methods which were aliased to class methods
8
+ * Make regexp objects as module constants
9
+ * Reorganize directory/require structure to match conventions used in other gems. Core sorting functions are moved to new `NaturalSort::Engine` module.
10
+ * Kernel: Only include into `Set` if Set is defined
11
+ * Kernel: `Range#natural_sort` is now an alias to `Range#to_a`
12
+ * Test: Upgrade to use Minitest gem
13
+ * Test: Improve readability by using `%w()` syntax for arrays
14
+ * Documentation: Convert all code documentation to TomDoc format
15
+ * Admin: Remove rdoc generator which was not working
16
+ * Admin: Add files .gitignore, Gemfile, gemspec, and cleanup Rakefile
17
+ * Admin: Add Travis and CodeClimate
18
+ * Admin: Migrate to Github
19
+
20
+ ## 1.1.1 - 2010-07-21
21
+
22
+ * Fix typo error in some unit test
23
+ * Remove copy/paste between naturalsort and natural_sort methods
24
+ * Added samples in rdoc
25
+ * Adding contribution from Sobe http://pastie.caboo.se/139803 (thanks mate)
26
+ * Sort complex text list (see test_natural_sort_kernel.rb:107))
27
+ * Fix more complex [string][number] pattern like:
28
+ ```ruby
29
+ ["x2-y08", "x2-g8", "x2-y7", "x8-y8"].natural_sort => ["x2-g8", "x2-y7", "x2-y08", "x8-y8"]
30
+ ["x02-y08", "x02-g8", "x2-y7", "x8-y8"].natural_sort => ["x02-g8", "x2-y7", "x02-y08", "x8-y8"]
31
+ ```
32
+ * Fix bug in Range ordering:
33
+ ```ruby
34
+ (1..21).natural_sort => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
35
+ ```
36
+
37
+ ## 1.1.0 - 2008-01-08
38
+
39
+ * Improve documentation to get better google results
40
+ * Remove alias methods: `sort_natural`, `sort_alpha`, `alpha_sort`, `sort_alphabetical`, `alphabetical_sort`, `sort_alphanum`, `alphanum_sort`
41
+
42
+ ## 1.0.1 - 2007-12-18
43
+
44
+ * Fix problem using as a static method
45
+ * Refactoring code
46
+
47
+ ## 1.0.0 - 2007-12-14
48
+
49
+ * Project creation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2007 Benjamin Francisoud
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,130 @@
1
+ # NaturalSort
2
+
3
+ [![Build Status](https://secure.travis-ci.org/johnnyshields/naturalsort.png?branch=master)](http://travis-ci.org/johnnyshields/naturalsort)
4
+ [![Code Climate](https://codeclimate.com/github/johnnyshields/naturalsort.png)](https://codeclimate.com/github/johnnyshields/naturalsort)
5
+
6
+ NaturalSort is a simple library which implements a natural, human-friendly alphanumeric sort in Ruby.
7
+
8
+ #### Fork from RubyForge-hosted NaturalSort gem
9
+
10
+ This Github repo `johnyshields/naturalsort` is an update of the Benjamin Francisoud's original NaturalSort gem,
11
+ hosted on RubyForge at http://rubyforge.org/projects/naturalsort. It has been forked from version 1.1.1
12
+ which was released on 2010-07-21. The first new release from this Github repo is version 1.2.0.
13
+
14
+ ## Examples
15
+
16
+ ```ruby
17
+ %w(a1 a11 a12 a2 a21).natural_sort #=> %w(a1 a2 a11 a12 a21)
18
+ %w(a b c A B C).natural_sort #=> %w(A a B b C c)
19
+ %w(x__2 x_1).natural_sort #=> %w(x_1 x__2)
20
+ %w(x2-y08 x2-g8 x2-y7 x8-y8).natural_sort #=> %w(x2-g8 x2-y7 x2-y08 x8-y8)
21
+ %w(x02-y08 x02-g8 x2-y7 x8-y8).natural_sort #=> %w(x02-g8 x2-y7 x02-y08 x8-y8)
22
+ ```
23
+
24
+ ## Features
25
+
26
+ * Sort case insensitive
27
+ * Sort underscore insensitive
28
+ * Sort filename matching patterns
29
+ * Sort mixed alpha and numeric "abc1", "abc12", "abc2", "a1b2" in correct order
30
+
31
+ ## Install
32
+
33
+ #### With Bundler
34
+
35
+ In your `Gemfile`:
36
+
37
+ ```ruby
38
+ gem 'naturalsort'
39
+ ```
40
+
41
+ or to optionally extend Ruby native objects:
42
+
43
+ ```ruby
44
+ gem 'naturalsort', :require => 'natural_sort_kernel'
45
+ ```
46
+
47
+ #### From Command Line
48
+
49
+ ```cmd
50
+ $ gem install naturalsort
51
+ ```
52
+
53
+ ## Usage
54
+
55
+ #### Extend Ruby native enumerable objects
56
+
57
+ `require 'natural_sort_kernel'` adds `natural_sort` methods to all native Ruby enumerable objects (Array, Hash, etc...)
58
+
59
+ ```ruby
60
+ require 'natural_sort_kernel'
61
+
62
+ sorted = %w(a b c A B C).natural_sort
63
+ ```
64
+
65
+ #### Use as a module function
66
+
67
+ ```ruby
68
+ require 'natural_sort' # unless using Bundler
69
+
70
+ sorted = NaturalSort.sort %w(a b c d A B C D)
71
+ ```
72
+
73
+ #### Use comparator function as a standalone
74
+
75
+ Adds `natural_sort` methods to Ruby native enumerable objects (Array, Hash, etc...)
76
+
77
+ ```ruby
78
+ person_1 = Person.new('Moe')
79
+ person_2 = Person.new('Larry')
80
+ person_3 = Person.new('Curly')
81
+
82
+ [person_1, person_2, person_3].sort{|a,b| NaturalSort.comparator(a.name, b.name)} #=> [person_3, person_2, person_1]
83
+
84
+ sorted = %w(a b c A B C).natural_sort
85
+ ```
86
+
87
+ #### Include into your own objects
88
+
89
+ Can be used to add `#natural_sort` method to on any enumerable object or any object which implements `#to_a`
90
+
91
+ ```ruby
92
+ class TodoList < Array
93
+ include NaturalSort
94
+ end
95
+
96
+ todo_list = TodoList.new
97
+ todo_list << 'Wash car'
98
+ todo_list << 'Water plants'
99
+ todo_list << 'Feed dog'
100
+
101
+ todo_list.natural_sort #=> ['Feed dog', 'Wash car', 'Water plants']
102
+ ```
103
+
104
+ ## Authors
105
+
106
+ * [Benjamin Francisoud](http://www.google.com/profiles/benjamin.francisoud)
107
+ * [Johnny Shields](http://github.com/johnnyshields) (version 1.2.0+)
108
+
109
+ ## Contributing
110
+
111
+ Fork -> Patch -> Spec -> Push -> Pull Request
112
+
113
+ ## Related Links
114
+
115
+ Links related to the natural sorting problem:
116
+
117
+ * http://www.codinghorror.com/blog/archives/001018.html
118
+ * http://sourcefrog.net/projects/natsort/
119
+ * http://be2.php.net/natsort
120
+ * http://www.wellho.net/forum/Programming-in-Python-and-Ruby/Python-Sort-Order.html
121
+ * http://www.davekoelle.com/alphanum.html
122
+ * http://rikkus.info/arch/sensible_sort.rb
123
+ * http://weblog.masukomi.org/2007/12/10/alphabetical-asciibetical
124
+ * http://nedbatchelder.com/blog/200712.html#e20071211T054956
125
+
126
+ ## License
127
+
128
+ Copyright (c) 2007 Benjamin Francisoud
129
+
130
+ Licensed under the MIT License. Refer to LICENSE for details.
data/Rakefile CHANGED
@@ -1,30 +1,13 @@
1
- # -*- ruby -*-
1
+ #!/usr/bin/env rake
2
+ # -*- mode: ruby -*-
2
3
 
3
4
  require 'rubygems'
4
- require 'hoe'
5
5
  require './lib/natural_sort.rb'
6
+ require 'rake/testtask'
6
7
 
7
- Hoe.new('naturalsort', NaturalSort::VERSION) do |p|
8
- p.rubyforge_name = 'naturalsort'
9
- p.author = 'Benjamin Francisoud'
10
- p.email = 'pub.cog@gmail.com'
11
- p.summary = 'NaturalSort is a small and simple library to implements a natural or human friendly alphabetical sort in ruby.'
12
- p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
- p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
- p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
- p.remote_rdoc_dir = '' # Release to root
16
- p.need_zip = true
17
- end
18
-
19
- SVN = 'svn+ssh://cogito@rubyforge.org/var/svn/naturalsort'
8
+ task :default => :test
20
9
 
21
- task :svn_branch => [:clean] do
22
- `svn delete #{SVN}/branches/1.X -m "cleanup branch"`
23
- `svn copy #{SVN}/trunk #{SVN}/branches/1.X -m "create branch"`
10
+ Rake::TestTask.new do |t|
11
+ t.libs << 'test'
12
+ t.test_files = FileList['test/**/test_*.rb']
24
13
  end
25
-
26
- task :svn_tag do
27
- `svn copy #{SVN}/trunk #{SVN}/tags/#{NaturalSort::VERSION} -m "create tag"`
28
- end
29
-
30
- # vim: syntax=Ruby
@@ -1,102 +1,4 @@
1
- # Sort elements in a natural / alphabetical / human friendly order.
2
- # require 'natural_sort'
3
- #
4
- # NaturalSort::naturalsort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
5
- # or
6
- # require 'natural_sort_kernel'
7
- #
8
- # ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
9
- module NaturalSort
10
- VERSION = '1.1.1'
11
-
12
- # call-seq:
13
- # NaturalSort::naturalsort(object) => array
14
- #
15
- # Static method to sort.
16
- #
17
- # *Usage*
18
- # NaturalSort::naturalsort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
19
- #
20
- # <tt>object</tt> can by any object that has to_a method.
21
- def self.naturalsort(object)
22
- sorted = object.to_a.sort do |a,b|
23
- sa, sb = a.to_s, b.to_s
24
- if ((sa.downcase <=> sb.downcase) == 0) then sa <=> sb
25
- else
26
- na, nb = check_regexp(sa, sb)
27
- na <=> nb
28
- end
29
- end
30
- end
31
-
32
- # call-seq:
33
- # object.natural_sort => array
34
- #
35
- # Main method to sort (other are just aliases).
36
- #
37
- # *Usage*
38
- # require 'natural_sort'
39
- # include NaturalSort
40
- #
41
- # object.natural_sort #=> ['a1', 'a2', 'a12']
42
- #
43
- # <tt>object</tt> can by any object that has a method <tt>to_a</tt>
44
- #
45
- # See <tt>natural_sort_kernel.rb</tt> to add natural sort methods to default ruby objects.
46
- # Enumerable , Array, Range, Set, Hash
47
- def natural_sort
48
- NaturalSort::naturalsort(to_a)
49
- end
50
-
51
- private
52
-
53
- def self.check_regexp(sa, sb)
54
- regexp = /(^|\D+)(\d+|(\D$))/
55
- ma, mb = multireg(regexp,sa), multireg(regexp,sb)
56
- it = 0
57
- equal = 0
58
- ret = ["",""]
59
- numeric = /(\d+)/
60
- while (it < [ma.size,mb.size].min) and (equal==0)
61
- if (ma[it] and mb[it]) and (ma[it][1] and mb[it][1]) \
62
- and (numeric.match ma[it][0] and numeric.match mb[it][0])
63
- l = [ma[it][2].size,mb[it][2].size].max
64
- ret = [format(ma[it], l), format(mb[it], l)]
65
- else
66
- ret = [ma[it][0].downcase, mb[it][0].downcase]
67
- end
68
- equal = ret[0] <=> ret[1]
69
- it+=1
70
- end
71
- return ret[0], ret[1]
72
- end
73
-
74
- def check_regexp(sa, sb)
75
- NaturalSort::check_regexp(sa, sb)
76
- end
77
-
78
- # format([a, 1], 3) => a001
79
- # add leading zero
80
- def self.format(match_data, length)
81
- match_data[1].gsub("_", "").downcase + ("%0#{length}d" % match_data[2].to_i)
82
- end
83
-
84
- def format(match_data, length)
85
- NaturalSort::format(match_data, length)
86
- end
87
-
88
- # return an array with
89
- # rgpx matchdata on str
90
- def self.multireg rgpx, str
91
- result = []
92
- while rgpx.match str
93
- result.push rgpx.match(str)
94
- str = rgpx.match(str).post_match
95
- end
96
- result
97
- end
98
-
99
- def multireg rgpx, str
100
- NaturalSort::multireg(rgpx, str)
101
- end
102
- end
1
+
2
+ require 'natural_sort/version'
3
+ require 'natural_sort/engine'
4
+ require 'natural_sort/base'
@@ -0,0 +1,68 @@
1
+ # Public: The public interface for NaturalSort module
2
+ # For method descriptions refer to NaturalSort::Engine
3
+ #
4
+ # Examples
5
+ #
6
+ # require 'natural_sort'
7
+ # NaturalSort.sort ['a1', 'a12', 'a2'] #=> ['a1', 'a2', 'a12']
8
+ #
9
+ # # or
10
+ #
11
+ # require 'natural_sort_kernel'
12
+ # ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
13
+ module NaturalSort
14
+
15
+ class << self
16
+ extend Forwardable
17
+
18
+ # Public: sort
19
+ #
20
+ # Signature
21
+ #
22
+ # NaturalSort.sort(object)
23
+ #
24
+ # Refer to NaturalSort::Engine.sort
25
+ def_delegator :'NaturalSort::Engine', :sort
26
+
27
+ # Public: comparator
28
+ #
29
+ # Signature
30
+ #
31
+ # NaturalSort.comparator(a, b)
32
+ #
33
+ # Refer to NaturalSort::Engine.comparator
34
+ def_delegator :'NaturalSort::Engine', :comparator
35
+
36
+ # Deprecated: Alias to NaturalSort.sort. Was deprecated in version 1.2.0.
37
+ # To be removed in version 2.0.0 (according to semver)
38
+ #
39
+ # object - the object to sort, which must either be an Enumerable or implement #to_a to be meaningful.
40
+ #
41
+ # Returns a sorted version of the object.
42
+ def naturalsort(object)
43
+ warn('NaturalSort.naturalsort is deprecated and will be removed. Use NaturalSort.sort instead')
44
+ NaturalSort::Engine.sort(object)
45
+ end
46
+ end
47
+
48
+ # Public: Main method to sort (other are just aliases). To be used when including
49
+ # NaturalSort into another object. Delegates to NaturalSort::Engine.sort
50
+ #
51
+ # Note that the object must either be an Enumerable or implement #to_a to be meaningful.
52
+ #
53
+ # Returns a sorted version of the object.
54
+ #
55
+ # Examples
56
+ #
57
+ # require 'natural_sort'
58
+ #
59
+ # class MyObject
60
+ # include NaturalSort
61
+ # def to_a
62
+ # ['a3, a2, a1']
63
+ # end
64
+ # end
65
+ def natural_sort
66
+ NaturalSort::Engine.sort(self)
67
+ end
68
+ end