grosser-sort_alphabetical 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +26 -0
- data/Rakefile +22 -0
- data/VERSION +1 -0
- data/lib/sort_alphabetical.rb +33 -0
- data/lib/sort_alphabetical/core_ext.rb +9 -0
- data/sort_alphabetical.gemspec +47 -0
- data/spec/README.markdown +2 -0
- data/spec/sort_alphabetical_spec.rb +43 -0
- data/spec/spec_helper.rb +10 -0
- metadata +71 -0
data/README.markdown
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Adds `sort_alphabetical` and `sort_alphabetical_by` to Enumberable(Array/Hash...),
|
2
|
+
which sorts UTF8 Strings alphabetical.
|
3
|
+
This sorting is done by placing variants on the same level as base character (A comes before Ä but ÄA comes before AB).
|
4
|
+
|
5
|
+
|
6
|
+
Setup
|
7
|
+
=====
|
8
|
+
sudo gem install grosser-sort_alphabetical -s http://gems.github.com/
|
9
|
+
|
10
|
+
Usage
|
11
|
+
=====
|
12
|
+
['b','á'].sort_alphabetical == ['á','b']
|
13
|
+
[['b',1],['á',2]].sort_alphabetical_by(&:first) == [['á',2],['b',1]]
|
14
|
+
|
15
|
+
TODO
|
16
|
+
====
|
17
|
+
- Sort non-ascii-convertables like ß(ss), œ(oe) , fi(fi), see [Ligatures](http://en.wikipedia.org/wiki/Typographical_ligature)
|
18
|
+
- Integrate natural sorting e.g. `['a11', 'a2'] => ['a2', 'a11']` like [NaturalSort](http://rubyforge.org/projects/naturalsort)
|
19
|
+
|
20
|
+
Authors
|
21
|
+
=======
|
22
|
+
- original string_to_ascii: [Marc-Andre](http://marc-andre.ca/).
|
23
|
+
|
24
|
+
[Michael Grosser](http://pragmatig.wordpress.com)
|
25
|
+
grosser.michael@gmail.com
|
26
|
+
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
desc "Run all specs in spec directory"
|
2
|
+
task :default do |t|
|
3
|
+
options = "--colour --format progress --loadby --reverse"
|
4
|
+
files = FileList['spec/**/*_spec.rb']
|
5
|
+
system("spec #{options} #{files}") rescue nil
|
6
|
+
system("ACTIVE_SUPPORT_VERSION='2.1.1' spec #{options} #{files}")
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
project_name = 'sort_alphabetical'
|
11
|
+
require 'jeweler'
|
12
|
+
Jeweler::Tasks.new do |gem|
|
13
|
+
gem.name = project_name
|
14
|
+
gem.summary = "Sort UTF8 Strings alphabetical via Enumerable extension"
|
15
|
+
gem.email = "grosser.michael@gmail.com"
|
16
|
+
gem.homepage = "http://github.com/grosser/#{project_name}"
|
17
|
+
gem.authors = ["Michael Grosser"]
|
18
|
+
gem.add_dependency "activesupport"
|
19
|
+
end
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
22
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'sort_alphabetical/core_ext'
|
4
|
+
|
5
|
+
module SortAlphabetical
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def sort(set)
|
9
|
+
set.sort_by do |item|
|
10
|
+
if block_given?
|
11
|
+
item = yield(item).to_s
|
12
|
+
else
|
13
|
+
item = item.to_s
|
14
|
+
end
|
15
|
+
[to_ascii(item),item]#when both á and a are present, sort them a, á
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_ascii(string)
|
20
|
+
split_codepoints(string).map(&:to_s).reject{|e| e.length > 1}.join
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# returns an array of unicode codepoints, in canonical order
|
26
|
+
def split_codepoints(string)
|
27
|
+
if defined? ActiveSupport::Multibyte::Handlers #pre ActiveSupport 2.3
|
28
|
+
ActiveSupport::Multibyte::Handlers::UTF8Handler.normalize(string,:d).split(//u)
|
29
|
+
else
|
30
|
+
string.mb_chars.normalize(:d).split(//u)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{sort_alphabetical}
|
5
|
+
s.version = "0.1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Michael Grosser"]
|
9
|
+
s.date = %q{2009-09-07}
|
10
|
+
s.email = %q{grosser.michael@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"README.markdown"
|
13
|
+
]
|
14
|
+
s.files = [
|
15
|
+
"README.markdown",
|
16
|
+
"Rakefile",
|
17
|
+
"VERSION",
|
18
|
+
"lib/sort_alphabetical.rb",
|
19
|
+
"lib/sort_alphabetical/core_ext.rb",
|
20
|
+
"sort_alphabetical.gemspec",
|
21
|
+
"spec/README.markdown",
|
22
|
+
"spec/sort_alphabetical_spec.rb",
|
23
|
+
"spec/spec_helper.rb"
|
24
|
+
]
|
25
|
+
s.homepage = %q{http://github.com/grosser/sort_alphabetical}
|
26
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
s.rubygems_version = %q{1.3.4}
|
29
|
+
s.summary = %q{Sort UTF8 Strings alphabetical via Enumerable extension}
|
30
|
+
s.test_files = [
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"spec/sort_alphabetical_spec.rb"
|
33
|
+
]
|
34
|
+
|
35
|
+
if s.respond_to? :specification_version then
|
36
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
41
|
+
else
|
42
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
43
|
+
end
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
if version = ENV['ACTIVE_SUPPORT_VERSION']
|
2
|
+
gem 'activesupport', version
|
3
|
+
version = "(AS: #{version})"
|
4
|
+
else
|
5
|
+
gem 'activesupport', '>=2.3'
|
6
|
+
end
|
7
|
+
require File.expand_path("spec_helper", File.dirname(__FILE__))
|
8
|
+
|
9
|
+
describe "SortAlphabetical #{version}"do
|
10
|
+
it "sorts ascii correctly" do
|
11
|
+
%w(b c a).sort_alphabetical.should == %w(a b c)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sorts UTF8 chars without accents" do
|
15
|
+
%w(b a á ä o ó x ö í i c).sort_alphabetical.should == %w(a á ä b c i í o ó ö x)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sorts by merging base with accented" do
|
19
|
+
%w(AA AB ÄA).sort_alphabetical.should == %w(AA ÄA AB)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "sorts words" do
|
23
|
+
%w(hellö hello hellá).sort_alphabetical.should == %w(hellá hello hellö)
|
24
|
+
end
|
25
|
+
|
26
|
+
pending_it "obeys order for ligatures" do
|
27
|
+
%w(asb aßc asd).sort_alphabetical.should == %w(asb aßc asd)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :to_ascii do
|
31
|
+
it "removes any accents" do
|
32
|
+
SortAlphabetical.to_ascii('á').should == 'a'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe :sort_alphabetical_by do
|
37
|
+
it "sorts using given block" do
|
38
|
+
silence_warnings do
|
39
|
+
%w(za yá xä xa).sort_alphabetical_by{|x|x.chars.to_a.reverse.join}.should == %w(xa xä yá za)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grosser-sort_alphabetical
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Grosser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: grosser.michael@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.markdown
|
33
|
+
files:
|
34
|
+
- README.markdown
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- lib/sort_alphabetical.rb
|
38
|
+
- lib/sort_alphabetical/core_ext.rb
|
39
|
+
- sort_alphabetical.gemspec
|
40
|
+
- spec/README.markdown
|
41
|
+
- spec/sort_alphabetical_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: false
|
44
|
+
homepage: http://github.com/grosser/sort_alphabetical
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.2.0
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Sort UTF8 Strings alphabetical via Enumerable extension
|
69
|
+
test_files:
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/sort_alphabetical_spec.rb
|