naturally-unicode 1.1.0

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: ef97522f5ea4669b25bb4b97b4e7d6c6fe331f3b
4
+ data.tar.gz: 2cfd04486ff7f938d47af12b1f2d68db3ef8a2a8
5
+ SHA512:
6
+ metadata.gz: 6610d5d63b5a1fa0c812a7eb6beac2457a8adb2f35f1d6c8158b7c52b5a69851c1760907bccddc4918c158f0b994655828972888b1ee5fcac5e9a14908bd5470
7
+ data.tar.gz: e342109988f022df63d13eab3f2a1d5cfb8f01b3590cc2592ac30bf7bd670043600a7d24f9dd93974b537c2b0511721488a6f9a06b078d751ce3fa4653bb0901
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ .idea/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rspec', '~> 2.0'
5
+ gem 'rake', '~> 10.0'
6
+ end
7
+
8
+ # Specify your gem's dependencies in naturally-unicode.gemspec
9
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ naturally-unicode (1.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.1.1)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.7)
16
+ rspec-expectations (2.14.4)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.4)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ naturally-unicode!
25
+ rake (~> 10.0)
26
+ rspec (~> 2.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Robb Shecter
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,79 @@
1
+ # Naturally-unicode
2
+
3
+ Natural (version number) sorting with added support for legal document numbering and unicode characters.
4
+ 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/)
5
+ for the motivations to make this library. This is also the kind of ordering you want if you're sorting version numbers.
6
+
7
+ The core of the search is [from here](https://github.com/ahoward/version_sorter). I then made
8
+ several changes to handle the particular types of numbers that come up in statutes, such
9
+ as *335.1, 336, 336a*, etc.
10
+
11
+ `NaturallyUnicode` will also sort "numbers" in college course code format such as
12
+ *MATH101, MATH102, ...*. See the specs for examples.
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'naturally-unicode'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it outside of bundler with:
26
+
27
+ $ gem install naturally-unicode
28
+
29
+
30
+ ## Usage
31
+
32
+ ```Ruby
33
+ require 'naturally-unicode'
34
+
35
+ # Sort a simple array of strings
36
+ NaturallyUnicode.sort(["1.1", "1.10", "1.2"]) # => ["1.1", "1.2", "1.10"]
37
+ ```
38
+
39
+ Usually, however, the library is used to sort an array of some kind of
40
+ object:
41
+
42
+
43
+ ```Ruby
44
+ # Sort an array of objects by one attribute
45
+ Thing = Struct.new(:number, :name)
46
+ objects = [
47
+ Thing.new('1', 'USA'),
48
+ Thing.new('2', 'Canada'),
49
+ Thing.new('1.1', 'Oregon'),
50
+ Thing.new('1.2', 'Washington'),
51
+ Thing.new('1.1.1', 'Portland'),
52
+ Thing.new('1.10', 'Texas'),
53
+ Thing.new('2.1', 'British Columbia'),
54
+ Thing.new('1.3', 'California'),
55
+ Thing.new('1.1.2', 'Eugene')
56
+ ]
57
+ objects.sort_by{ |o| NaturallyUnicode.normalize(o.number) }
58
+
59
+ # Results in:
60
+ [<struct Thing number="1.1", name="Oregon">,
61
+ <struct Thing number="1.1.1", name="Portland">,
62
+ <struct Thing number="1.1.2", name="Eugene">,
63
+ <struct Thing number="1.2", name="Washington">,
64
+ <struct Thing number="1.3", name="California">,
65
+ <struct Thing number="1.10", name="Texas">,
66
+ <struct Thing number="2", name="Canada">,
67
+ <struct Thing number="2.1", name="British Columbia">]
68
+ ```
69
+
70
+ See [the spec for more examples](https://github.com/Loriowar/naturally-unicode/blob/master/spec/naturally_spec.rb).
71
+
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ task :default => :spec
@@ -0,0 +1,76 @@
1
+ require 'naturally-unicode/version'
2
+
3
+ module NaturallyUnicode
4
+ # Perform a natural sort.
5
+ #
6
+ # @param [Array<String>] an_array the list of numbers to sort.
7
+ # @return [Array<String>] the numbers sorted naturally.
8
+ def self.sort(an_array)
9
+ return an_array.sort_by { |x| normalize(x) }
10
+ end
11
+
12
+ # Convert the given number into an object that can be sorted
13
+ # naturally. This object is an array of {NumberElement} instances.
14
+ #
15
+ # @param [String] number the number in complex form such as 1.2a.3.
16
+ # @return [Array<NumberElement>] an array of NumberElements which is
17
+ # able to be sorted naturally via a normal 'sort'.
18
+ def self.normalize(number)
19
+ number.to_s.scan(%r/\p{Word}+/o).map { |i| NumberElement.new(i) }
20
+ end
21
+
22
+ private
23
+
24
+ # An entity which can be compared to other like elements for
25
+ # sorting in an array. It's an object representing
26
+ # a value which implements the {Comparable} interface.
27
+ class NumberElement
28
+ include Comparable
29
+ attr_accessor :val
30
+
31
+ def initialize(v)
32
+ @val = v
33
+ end
34
+
35
+ def <=>(other)
36
+ if pure_integer? && other.pure_integer?
37
+ @val.to_i <=> other.val.to_i
38
+ elsif numbers_with_letters? || other.numbers_with_letters?
39
+ simple_normalize(@val) <=> simple_normalize(other.val)
40
+ elsif letters_with_numbers? || other.letters_with_numbers?
41
+ reverse_simple_normalize(@val) <=> reverse_simple_normalize(other.val)
42
+ else
43
+ @val <=> other.val
44
+ end
45
+ end
46
+
47
+ def pure_integer?
48
+ @val =~ /^\d+$/
49
+ end
50
+
51
+ def numbers_with_letters?
52
+ val =~ /^\d+\p{Alpha}+$/
53
+ end
54
+
55
+ def letters_with_numbers?
56
+ val =~ /^\p{Alpha}+\d+$/
57
+ end
58
+
59
+ def simple_normalize(n)
60
+ if n =~ /^(\d+)(\p{Alpha}+)$/
61
+ [$1.to_i, $2]
62
+ else
63
+ [n.to_i]
64
+ end
65
+ end
66
+
67
+ def reverse_simple_normalize(n)
68
+ if n =~ /^(\p{Alpha}+)(\d+)$/
69
+ [$1, $2.to_i]
70
+ else
71
+ [n.to_s]
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module NaturallyUnicode
2
+ VERSION = "1.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'naturally-unicode/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'naturally-unicode'
8
+ gem.version = NaturallyUnicode::VERSION
9
+ gem.authors = ["Robb Shecter"]
10
+ gem.email = ["robb@weblaws.org"]
11
+ gem.summary = %q{Sorts numbers according to the way people are used to seeing them.}
12
+ gem.description = %q{Natural Sorting with support for legal numbering and unicode characters}
13
+ gem.homepage = "https://github.com/Loriowar/naturally-unicode"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,107 @@
1
+ require 'naturally-unicode'
2
+
3
+ describe NaturallyUnicode do
4
+ describe '#sort' do
5
+ it 'sorts an array of strings nicely as if they were legal numbers' do
6
+ a = %w[676 676.1 676.11 676.12 676.2 676.3 676.9 676.10]
7
+ b = %w[676 676.1 676.2 676.3 676.9 676.10 676.11 676.12]
8
+ NaturallyUnicode.sort(a).should == b
9
+ end
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
+ NaturallyUnicode.sort(a).should == b
15
+ end
16
+
17
+ it 'sorts a more complex list of strings' do
18
+ a = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.45 354.5]
19
+ b = %w[350 351 352 352.1 352.5 353.1 354 354.3 354.4 354.5 354.45]
20
+ NaturallyUnicode.sort(a).should == b
21
+ end
22
+
23
+ it 'sorts when numbers have letters in them' do
24
+ a = %w[335 335.1 336a 336 337 337a 337.1 337.15 337.2]
25
+ b = %w[335 335.1 336 336a 337 337.1 337.2 337.15 337a]
26
+ NaturallyUnicode.sort(a).should == b
27
+ end
28
+
29
+ it 'sorts when numbers have unicode letters in them' do
30
+ a = %w[335 335.1 336a 336 337 337я 337.1 337.15 337.2]
31
+ b = %w[335 335.1 336 336a 337 337.1 337.2 337.15 337я]
32
+ NaturallyUnicode.sort(a).should == b
33
+ end
34
+
35
+ it 'sorts when letters have numbers in them' do
36
+ a = %w[PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, PBLI, SBP1, SBP3]
37
+ b = %w[PBLI, PC1, PC3, PC5, PC7, PC9, PC10, PC11, PC12, PC13, PC14, PROF2, SBP1, SBP3]
38
+ NaturallyUnicode.sort(a).should == b
39
+ end
40
+
41
+ it 'sorts when letters have numbers and uncode characters in them' do
42
+ a = %w[АБ4, АБ2, АБ10, АБ12, АБ1, АБ3, АД8, АД5, АЩФ12, АЩФ8, ЫВА1]
43
+ b = %w[АБ1, АБ2, АБ3, АБ4, АБ10, АБ12, АД5, АД8, АЩФ8, АЩФ12, ЫВА1]
44
+ NaturallyUnicode.sort(a).should == b
45
+ end
46
+
47
+ it 'sorts double digits with letters correctly' do
48
+ a = %w[12a 12b 12c 13a 13b 2 3 4 5 10 11 12]
49
+ b = %w[2 3 4 5 10 11 12 12a 12b 12c 13a 13b]
50
+ NaturallyUnicode.sort(a).should == b
51
+ end
52
+
53
+ it 'sorts double digits with unicode letters correctly' do
54
+ a = %w[12а 12б 12в 13а 13б 2 3 4 5 10 11 12]
55
+ b = %w[2 3 4 5 10 11 12 12а 12б 12в 13а 13б]
56
+ NaturallyUnicode.sort(a).should == b
57
+ end
58
+ end
59
+
60
+ describe '#normalize' do
61
+ Thing = Struct.new(:number, :name)
62
+
63
+ it 'enables sorting objects by one particular attribute' do
64
+ objects = [
65
+ Thing.new('1.1', 'color'),
66
+ Thing.new('1.2', 'size'),
67
+ Thing.new('1.1.1', 'opacity'),
68
+ Thing.new('1.1.2', 'lightness'),
69
+ Thing.new('1.10', 'hardness'),
70
+ Thing.new('2.1', 'weight'),
71
+ Thing.new('1.3', 'shape')
72
+ ]
73
+ sorted = objects.sort_by{ |o| NaturallyUnicode.normalize(o.number) }
74
+ sorted.map{|o| o.name}.should == %w[
75
+ color
76
+ opacity
77
+ lightness
78
+ size
79
+ shape
80
+ hardness
81
+ weight
82
+ ]
83
+ end
84
+
85
+ it 'enables sorting objects by one particular attribute which contain unicode characters' do
86
+ objects = [
87
+ Thing.new('1.1', 'Москва'),
88
+ Thing.new('1.2', 'Киев'),
89
+ Thing.new('1.1.1', 'Париж'),
90
+ Thing.new('1.1.2', 'Будапешт'),
91
+ Thing.new('1.10', 'Брест'),
92
+ Thing.new('2.1', 'Калуга'),
93
+ Thing.new('1.3', 'Васюки')
94
+ ]
95
+ sorted = objects.sort_by{ |o| NaturallyUnicode.normalize(o.name) }
96
+ sorted.map{|o| o.name}.should == %w[
97
+ Брест
98
+ Будапешт
99
+ Васюки
100
+ Калуга
101
+ Киев
102
+ Москва
103
+ Париж
104
+ ]
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: naturally-unicode
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robb Shecter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Natural Sorting with support for legal numbering and unicode characters
14
+ email:
15
+ - robb@weblaws.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - .travis.yml
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/naturally-unicode.rb
28
+ - lib/naturally-unicode/version.rb
29
+ - naturally-unicode.gemspec
30
+ - spec/naturally-unicode_spec.rb
31
+ homepage: https://github.com/Loriowar/naturally-unicode
32
+ licenses: []
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Sorts numbers according to the way people are used to seeing them.
54
+ test_files:
55
+ - spec/naturally-unicode_spec.rb
56
+ has_rdoc: