i76-has_slug 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,37 @@
1
1
  class String
2
- # convert strings to slugs that are lowercase an only contain alphanumeric
3
- # characters, dashes and sometimes dots
4
- def to_slug
5
- # Transliterate
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
- # Dot's should be saved only when they have letter or number on both sides
15
- # (this preserves file extensions)
16
- # slug.gsub!(/[^\w\d]\.+/, '-')
17
- # slug.gsub!(/\.+[^\w\d]/, '-')
18
-
19
- # Strip dots from begining and end
20
- slug.gsub!(/^\.+/, '')
21
- slug.gsub!(/\.+$/, '')
22
-
23
- # Change multiple succesive dashes to single dashe.
24
- slug.gsub!(/-+/, '-')
25
-
26
- # Strip dashes from begining and end
27
- slug.gsub!(/^-+/, '')
28
- slug.gsub!(/-+$/, '')
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
@@ -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 'handle quotes well' do
6
- assert_equal 'l-atelier', "l'Atelier".to_slug
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i76-has_slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom-Eric Gerritsen