everyday_natsort 1.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f757236f459322bdb735b3520b1d58b62407f3c8
4
+ data.tar.gz: 9327a7f85e11b51d6de87c42cab96fd32469e9b0
5
+ SHA512:
6
+ metadata.gz: 8d9e9b371cf9413d29f6c892822c3dfda9111289cee75e2d9cefd24cce1877d8a2a683efbe48b4d9e163e53f78efcc3e7c00d4ba1d9784390eed60dc600f6f69
7
+ data.tar.gz: 709a9d76b33d16322a9741d3197c1a32608a2bb46d4d3f8f6af2681bc0721bbf708ae5b59d6596c0d0451025fb35062813c2cc4d5696a0e5488f9131beaa9e35
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+
24
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+ -rspec_helper
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ language: ruby
2
+ gemfile: Gemfile.ci
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in everyday_natsort.gemspec
4
+ gemspec
data/Gemfile.ci ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'bundler', '~> 1.6'
4
+ gem 'rake', '~> 10.0'
5
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Eric Henderson
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # EverydayNatsort
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'everyday_natsort'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install everyday_natsort
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/everyday_natsort/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new('spec')
7
+
8
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'everyday_natsort/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'everyday_natsort'
8
+ spec.version = EverydayNatsort::VERSION
9
+ spec.authors = ['Eric Henderson']
10
+ spec.email = ['henderea@gmail.com']
11
+ spec.summary = %q{A gem for natural sorting and accent stripping. Based on the naturalsort gem.}
12
+ spec.description = %q{A gem for natural sorting and accent stripping. Based on the naturalsort gem, but with enhancements to handle accented characters and hyphen versus underscore. Accents handling is also available as a separate module.}
13
+ spec.homepage = 'https://github.com/henderea/everyday_natsort'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.4'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,59 @@
1
+ module EverydayNatsort
2
+ class Accents
3
+ MAPPINGS = [[%r{([\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u0100\u0102\u0104])}, 'A'],
4
+ [%r{([\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u0101\u0103\u0105])}, 'a'],
5
+ [%r{([\u00C6])}, 'AE'],
6
+ [%r{([\u00E6])}, 'ae'],
7
+ [%r{([\u00C7\u0106\u0108\u010A\u010C])}, 'C'],
8
+ [%r{([\u00E7\u0107\u0109\u010B\u010D])}, 'c'],
9
+ [%r{([\u010E\u0110])}, 'D'],
10
+ [%r{([\u010F\u0111])}, 'd'],
11
+ [%r{([\u00C8\u00C9\u00CA\u00CB\u0112\u0114\u0116\u0118\u011A])}, 'E'],
12
+ [%r{([\u00E8\u00E9\u00EA\u00EB\u0113\u0115\u0117\u0119\u011B])}, 'e'],
13
+ [%r{([\u011C\u011E\u0120\u0122])}, 'G'],
14
+ [%r{([\u011D\u011F\u0121\u0123])}, 'g'],
15
+ [%r{([\u0124\u0126])}, 'H'],
16
+ [%r{([\u0125\u0127])}, 'h'],
17
+ [%r{([\u00CC\u00CD\u00CE\u00CF\u0128\u012A\u012C\u012E\u0130])}, 'I'],
18
+ [%r{([\u00EC\u00ED\u00EE\u00EF\u0129\u012B\u012D\u012F\u0131])}, 'i'],
19
+ [%r{([\u0132])}, 'IJ'],
20
+ [%r{([\u0133])}, 'ij'],
21
+ [%r{([\u0134])}, 'J'],
22
+ [%r{([\u0135])}, 'j'],
23
+ [%r{([\u0136])}, 'K'],
24
+ [%r{([\u0137])}, 'k'],
25
+ [%r{([\u0139\u013B\u013D\u013F\u0141])}, 'L'],
26
+ [%r{([\u013A\u013C\u013E\u0140\u0142])}, 'l'],
27
+ [%r{([\u00D1\u0143\u0145\u0147\u014A])}, 'N'],
28
+ [%r{([\u00F1\u0144\u0146\u0148\u0149\u014B])}, 'n'],
29
+ [%r{([\u00D2\u00D3\u00D4\u00D5\u00D6\u00D8\u014C\u014E\u0150])}, 'O'],
30
+ [%r{([\u00F2\u00F3\u00F4\u00F5\u00F6\u00F8\u014D\u014F\u0151])}, 'o'],
31
+ [%r{([\u0152])}, 'OE'],
32
+ [%r{([\u0153])}, 'oe'],
33
+ [%r{([\u0154\u0156\u0158])}, 'R'],
34
+ [%r{([\u0155\u0157\u0159])}, 'r'],
35
+ [%r{([\u015A\u015C\u015E\u0160])}, 'S'],
36
+ [%r{([\u015B\u015D\u015F\u0161])}, 's'],
37
+ [%r{([\u0162\u0164\u0166])}, 'T'],
38
+ [%r{([\u0163\u0165\u0167])}, 't'],
39
+ [%r{([\u00D9\u00DA\u00DB\u00DC\u0168\u016A\u016C\u016E\u0170\u0172])}, 'U'],
40
+ [%r{([\u00F9\u00FA\u00FB\u00FC\u0169\u016B\u016D\u016F\u0171\u0173])}, 'u'],
41
+ [%r{([\u0174])}, 'W'],
42
+ [%r{([\u0175])}, 'w'],
43
+ [%r{([\u00D7])}, 'x'],
44
+ [%r{([\u00DD\u0176\u0178])}, 'Y'],
45
+ [%r{([\u00FD\u00FF\u0177])}, 'y'],
46
+ [%r{([\u0179\u017B\u017D])}, 'Z'],
47
+ [%r{([\u017A\u017C\u017E])}, 'z'],
48
+ [%r{([\u201C\u201D])}, '"'],
49
+ [%r{([\u00B4\u2018\u2019])}, "'"],
50
+ [%r{([\u2010\u2011\u2012\u2013\u2014\u2015])}, '-']]
51
+ class << self
52
+ def sanitize(str)
53
+ rval = str
54
+ MAPPINGS.each { |v| rval = rval.gsub(v[0], v[1]) }
55
+ rval
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'accents'
2
+ require_relative 'natural'
3
+
4
+ module EverydayNatsort
5
+ class << self
6
+ def strip_accents(str)
7
+ EverydayNatsort::Accents.sanitize(str)
8
+ end
9
+
10
+ def sort(object)
11
+ EverydayNatsort::Natural.sort(object)
12
+ end
13
+
14
+ def compare(a, b)
15
+ EverydayNatsort::Natural.compare(a, b)
16
+ end
17
+ end
18
+
19
+ module NaturalSort
20
+ def natural_sort
21
+ EverydayNatsort.sort(self)
22
+ end
23
+ end
24
+
25
+ module StripAccents
26
+ def strip_accents
27
+ EverydayNatsort.strip_accents(self)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,60 @@
1
+ require_relative 'base'
2
+
3
+ module EverydayNatsort::Kernel
4
+ end
5
+
6
+ module Enumerable
7
+ # Add #natural_sort method to Enumerable module.
8
+ #
9
+ # Examples
10
+ #
11
+ # require 'everyday_natsort_kernel'
12
+ # ['a1', 'a12', 'a2'].natural_sort #=> ['a1', 'a2', 'a12']
13
+ # ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
14
+ include EverydayNatsort::NaturalSort
15
+ end
16
+
17
+ class Array
18
+ # Add #natural_sort method to Array class.
19
+ #
20
+ # Examples
21
+ #
22
+ # require 'everyday_natsort_kernel'
23
+ # ['a1', 'a12', 'a2'].natural_sort #=> ['a1', 'a2', 'a12']
24
+ # ['a', 'b', 'A', 'B'].natural_sort #=> ['A', 'a', 'B', 'b']
25
+ include EverydayNatsort::NaturalSort
26
+ end
27
+
28
+ class Range
29
+ # Add #natural_sort method to Range class, which aliases #to_a
30
+ #
31
+ # Examples
32
+ #
33
+ # require 'everyday_natsort_kernel'
34
+ # (1..11).natural_sort #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
35
+ alias :natural_sort :to_a
36
+ end
37
+
38
+ if defined?(Set)
39
+ class Set
40
+ # Add #natural_sort method to Set class, if it has been initialized.
41
+ #
42
+ # Examples
43
+ #
44
+ # require 'set'
45
+ # require 'everyday_natsort_kernel'
46
+ # Set.new(['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']).natural_sort #=> ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd']
47
+ include EverydayNatsort::NaturalSort
48
+ end
49
+ end
50
+
51
+ class Hash
52
+ # Add #natural_sort method to Hash class.
53
+ #
54
+ # Examples
55
+ #
56
+ # require 'everyday_natsort_kernel'
57
+ # { "a" => "value", "b" => "value", "A" => "value", "B" => "value" }.natural_sort #=>
58
+ # [["A", "value"], ["a", "value"], ["B", "value"], ["b", "value"]]
59
+ include EverydayNatsort::NaturalSort
60
+ end
@@ -0,0 +1,99 @@
1
+ require_relative 'accents'
2
+
3
+ #Note: uses a modified version of the comparing logic from https://github.com/johnnyshields/naturalsort
4
+
5
+ module EverydayNatsort
6
+ class Natural
7
+ REGEXP = /(^|\D+)(\d+|(\D$))/
8
+ NUMERIC = /(\d+)/
9
+
10
+ class << self
11
+ def sort(object)
12
+ Array(object).sort do |a, b|
13
+ self.compare(a, b)
14
+ end
15
+ end
16
+
17
+ def compare(a, b)
18
+ sa, sb = a.to_s, b.to_s
19
+ if (sa.downcase <=> sb.downcase) == 0
20
+ sa <=> sb
21
+ else
22
+ na, nb = nat_sanatize(sa, sb)
23
+ na <=> nb
24
+ end
25
+ end
26
+
27
+ def nat_sanatize(sa, sb)
28
+ sa = EverydayNatsort::Accents.sanitize(sa).gsub('-', '_')
29
+ sb = EverydayNatsort::Accents.sanitize(sb).gsub('-', '_')
30
+ ma, mb = multireg(REGEXP, sa), multireg(REGEXP, sb)
31
+ ret = sanatize_loop(ma, mb)
32
+ return ret[0], ret[1]
33
+ end
34
+
35
+ def sanatize_loop(ma, mb)
36
+ ret = ['', '']
37
+ (0...[ma.size, mb.size].min).each { |it|
38
+ ret = process_match(it, ma, mb)
39
+ break unless check_equal(ret)
40
+ }
41
+ ret
42
+ end
43
+
44
+ def process_match(it, ma, mb)
45
+ match_num?(it, ma, mb) ? process_numeric_match(it, ma, mb) : process_alpha_match(it, ma, mb)
46
+ end
47
+
48
+ def match_num?(it, ma, mb)
49
+ both_exist?(it, ma, mb) && both_have_group_1?(it, ma, mb) && both_num?(it, ma, mb)
50
+ end
51
+
52
+ def both_exist?(it, ma, mb)
53
+ ma[it] && mb[it]
54
+ end
55
+
56
+ def both_have_group_1?(it, ma, mb)
57
+ ma[it][1] && mb[it][1]
58
+ end
59
+
60
+ def both_num?(it, ma, mb)
61
+ num?(ma[it][0]) && num?(mb[it][0])
62
+ end
63
+
64
+ def process_numeric_match(it, ma, mb)
65
+ l = [ma[it][2].size, mb[it][2].size].max
66
+ [format(ma[it], l), format(mb[it], l)]
67
+ end
68
+
69
+ def process_alpha_match(it, ma, mb)
70
+ [ma[it][0].downcase, mb[it][0].downcase]
71
+ end
72
+
73
+ def num?(v)
74
+ NUMERIC.match(v)
75
+ end
76
+
77
+ def check_equal(ret)
78
+ (ret[0] <=> ret[1]) == 0
79
+ end
80
+
81
+ # format([a, 1], 3) => a001
82
+ # add leading zero
83
+ def format(match_data, length)
84
+ match_data[1].gsub(/-|_/, '').downcase + ("%0#{length}d" % match_data[2].to_i)
85
+ end
86
+
87
+ # return an array with
88
+ # regexp matchdata on str
89
+ def multireg(regpexp, str)
90
+ result = []
91
+ while regpexp.match(str)
92
+ result.push regpexp.match(str)
93
+ str = regpexp.match(str).post_match
94
+ end
95
+ result
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module EverydayNatsort
2
+ VERSION = '1.0.3'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'everyday_natsort/version'
2
+ require 'everyday_natsort/accents'
3
+ require 'everyday_natsort/natural'
4
+ require 'everyday_natsort/base'
@@ -0,0 +1,5 @@
1
+ require 'everyday_natsort/version'
2
+ require 'everyday_natsort/accents'
3
+ require 'everyday_natsort/natural'
4
+ require 'everyday_natsort/base'
5
+ require 'everyday_natsort/kernel'
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ require 'rspec'
3
+ require_relative '../../lib/everyday_natsort'
4
+
5
+ def test_fixes_char(str, char, count)
6
+ str2 = EverydayNatsort.strip_accents(str)
7
+ expect(str2).to eq (char * count)
8
+ end
9
+
10
+ describe 'accents' do
11
+ it 'fixes A' do
12
+ str = 'ÀÁÂÃÄÅĀĂĄ'
13
+ test_fixes_char(str, 'A', 9)
14
+ end
15
+ it 'fixes a' do
16
+ str = 'àáâãäåāăą'
17
+ test_fixes_char(str, 'a', 9)
18
+ end
19
+ it 'fixes AE' do
20
+ str = 'Æ'
21
+ test_fixes_char(str, 'AE', 1)
22
+ end
23
+ it 'fixes ae' do
24
+ str = 'æ'
25
+ test_fixes_char(str, 'ae', 1)
26
+ end
27
+ it 'fixes C' do
28
+ str = 'ÇĆĈĊČ'
29
+ test_fixes_char(str, 'C', 5)
30
+ end
31
+ it 'fixes c' do
32
+ str = 'çćĉċč'
33
+ test_fixes_char(str, 'c', 5)
34
+ end
35
+ end
File without changes
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: everyday_natsort
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Eric Henderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A gem for natural sorting and accent stripping. Based on the naturalsort
56
+ gem, but with enhancements to handle accented characters and hyphen versus underscore. Accents
57
+ handling is also available as a separate module.
58
+ email:
59
+ - henderea@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - Gemfile.ci
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - everyday_natsort.gemspec
73
+ - lib/everyday_natsort.rb
74
+ - lib/everyday_natsort/accents.rb
75
+ - lib/everyday_natsort/base.rb
76
+ - lib/everyday_natsort/kernel.rb
77
+ - lib/everyday_natsort/natural.rb
78
+ - lib/everyday_natsort/version.rb
79
+ - lib/everyday_natsort_kernel.rb
80
+ - spec/everyday_natsort/accents_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/henderea/everyday_natsort
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.5
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A gem for natural sorting and accent stripping. Based on the naturalsort
106
+ gem.
107
+ test_files:
108
+ - spec/everyday_natsort/accents_spec.rb
109
+ - spec/spec_helper.rb