i76-has_slug 0.1.4 → 0.1.5
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.
- data/lib/has_slug/slug.rb +29 -27
- data/test/unit/slug_test.rb +38 -2
- metadata +1 -1
data/lib/has_slug/slug.rb
CHANGED
@@ -1,35 +1,37 @@
|
|
1
1
|
class String
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
slug = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')
|
2
|
+
# convert strings to slugs that are lowercase an only contain alphanumeric
|
3
|
+
# characters, dashes and sometimes dots
|
4
|
+
def to_slug
|
5
|
+
slug = self
|
7
6
|
|
7
|
+
# Transliterate
|
8
|
+
slug = Unicode.normalize_KD(slug).gsub(/[^\x00-\x7F]/n,'')
|
9
|
+
|
8
10
|
# Convert to lowercase
|
9
11
|
slug.downcase!
|
10
12
|
|
11
13
|
# Change seperators (like spaces) to dashes
|
12
|
-
slug.gsub!(/[+_'
|
14
|
+
slug.gsub!(/[+_',\/|;; ]/, '-')
|
15
|
+
|
16
|
+
# Dot's should be saved only when they have letter or number on both sides
|
17
|
+
# (this preserves file extensions)
|
18
|
+
slug.gsub!(/[^\w\d]\.+/, '-')
|
19
|
+
slug.gsub!(/\.+[^\w\d]/, '-')
|
13
20
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# Remove everything that is not letter, number, dash or dot.
|
31
|
-
slug.gsub!(/[^\w\d.-]/, '')
|
32
|
-
|
33
|
-
slug
|
34
|
-
end
|
21
|
+
# Replace everything that is not letter, number, dash or dot with a dash
|
22
|
+
slug.gsub!(/[^\w\d.-]/, '-')
|
23
|
+
|
24
|
+
# Strip dots from begining and end
|
25
|
+
slug.gsub!(/^\.+/, '')
|
26
|
+
slug.gsub!(/\.+$/, '')
|
27
|
+
|
28
|
+
# Strip dashes from begining and end
|
29
|
+
slug.gsub!(/^-(.+)+/, '\1')
|
30
|
+
slug.gsub!(/(.+)-+$/, '\1')
|
31
|
+
|
32
|
+
# Change multiple succesive dashes to single dashe.
|
33
|
+
slug.gsub!(/-+/, '-')
|
34
|
+
|
35
|
+
slug
|
36
|
+
end
|
35
37
|
end
|
data/test/unit/slug_test.rb
CHANGED
@@ -2,8 +2,44 @@ require "#{File.dirname(__FILE__)}/../test_helper"
|
|
2
2
|
|
3
3
|
class SlugTest < Test::Unit::TestCase
|
4
4
|
context 'A Slug' do
|
5
|
-
should '
|
6
|
-
assert_equal '
|
5
|
+
should 'be lowercase' do
|
6
|
+
assert_equal 'abc', "ABC".to_slug
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'have replaced spaces with dashes' do
|
10
|
+
assert_equal 'abc-def', "abc def".to_slug
|
11
|
+
end
|
12
|
+
|
13
|
+
should 'have replaced special characters with dashes' do
|
14
|
+
assert_equal 'l-atelier', "l'Atelier".to_slug
|
15
|
+
assert_equal 'five-stars', "Five*Stars".to_slug
|
16
|
+
assert_equal '-', "***".to_slug
|
17
|
+
end
|
18
|
+
|
19
|
+
should 'have stripped utf-8 characters' do
|
20
|
+
assert_equal 'internationalization', "Iñtërnâtiônàlizâtiôn".to_slug
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'preserve dots that have numbers or letters on both sides' do
|
24
|
+
assert_equal 'filename.1', "filename.1".to_slug
|
25
|
+
assert_equal 'filename.txt', "filename.txt".to_slug
|
26
|
+
|
27
|
+
assert_equal 'a-sentence', "A sentence.".to_slug
|
28
|
+
assert_equal 'a-sentence-a-new-sentence', "A sentence. A new sentence.".to_slug
|
29
|
+
end
|
30
|
+
|
31
|
+
should 'not have trailing dashes' do
|
32
|
+
assert_equal 'abc-def', "abc def ".to_slug
|
33
|
+
assert_equal 'abc-def', "abc def-".to_slug
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'not have leading dashes' do
|
37
|
+
assert_equal 'abc-def', " abc def".to_slug
|
38
|
+
assert_equal 'abc-def', "-abc def".to_slug
|
39
|
+
end
|
40
|
+
|
41
|
+
should 'not have succesive dashes' do
|
42
|
+
assert_equal 'abc-def', "abc def".to_slug
|
7
43
|
end
|
8
44
|
end
|
9
45
|
end
|