Skip to content

Commit

Permalink
Added sanitize_as_filename method to the String class.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjg committed Oct 9, 2008
1 parent b0540be commit c8f6152
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
15 changes: 13 additions & 2 deletions lib/ruby_extensions.rb
Expand Up @@ -20,8 +20,19 @@ def geometric_mean

# String extensions
String.class_eval do

def to_slug
# Unidecode is missing some hyphen conversions
self.gsub(/[-‐‒–—―⁃−­]/, '-').to_ascii.downcase.gsub(/[^a-z0-9 ]/, ' ').strip.gsub(/[ ]+/, '-')
self.transliterate.downcase.gsub(/[^a-z0-9 ]/, ' ').strip.gsub(/[ ]+/, '-')
end

# differs from the 'to_slug' method in that it leaves in the dot '.' character and removes Windows' crust from paths (removes "C:\Temp\" from "C:\Temp\mieczyslaw.jpg")
def sanitize_as_filename
self.gsub(/^.*(\\|\/)/, '').transliterate.downcase.gsub(/[^a-z0-9\. ]/, ' ').strip.gsub(/[ ]+/, '-')
end

def transliterate
# Unidecode gem is missing some hyphen transliterations
self.gsub(/[-‐‒–—―⁃−­]/, '-').to_ascii
end

end
29 changes: 16 additions & 13 deletions test/ruby_extensions_test.rb
Expand Up @@ -4,9 +4,9 @@
class RubyExtensionsTest < Test::Unit::TestCase

def test_object_extensions
assert "".respond_to?(:try)
assert_nil "".try(:non_existing_method)
assert_equal "a ".try(:strip), "a"
assert ''.respond_to?(:try)
assert_nil ''.try(:non_existing_method)
assert_equal 'a '.try(:strip), 'a'
end

def test_array_extensions
Expand All @@ -18,31 +18,34 @@ def test_array_extensions
end

def test_string_extensions
assert "".respond_to?(:to_slug)
assert ''.respond_to?(:to_slug)

# to_slug: Polish accented characters
assert_equal "ęóąśłżźćń ĘÓĄŚŁŻŹĆŃ".to_slug, "eoaslzzcn-eoaslzzcn"
assert_equal 'ęóąśłżźćń ĘÓĄŚŁŻŹĆŃ'.to_slug, 'eoaslzzcn-eoaslzzcn'

# to_slug: Kaszubian accented characters
assert_equal "ÃãÉéËëÒòÔôÙù".to_slug, "aaeeeeoooouu"
assert_equal 'ÃãÉéËëÒòÔôÙù'.to_slug, 'aaeeeeoooouu'

# to_slug: Iñtërnâtiônàlizætiøn
assert_equal "Iñtërnâtiônàlizætiøn".to_slug, "internationalizaetion"
assert_equal 'Iñtërnâtiônàlizætiøn'.to_slug, 'internationalizaetion'

# to_slug: other accented characters
assert_equal "āčēģīķļņū".to_slug, 'acegiklnu'
assert_equal 'āčēģīķļņū'.to_slug, 'acegiklnu'
assert_equal '中文測試'.to_slug, 'zhong-wen-ce-shi'
assert_equal 'fööbär'.to_slug, 'foobar'

# to_slug: hyphens
assert_equal "a-b‐c‒d–e—f―g⁃h−i­j".to_slug, "a-b-c-d-e-f-g-h-i-j"
assert_equal 'a-b‐c‒d–e—f―g⁃h−i­j'.to_slug, 'a-b-c-d-e-f-g-h-i-j'

# to_slug: various
assert_equal '!@#$%^&*()_+[]{}|;\':",./<>?/// aaaa_bbbb \\ ?@#$?`~'.to_slug, "aaaa-bbbb"
assert_equal '!@#$%^&*()_+[]{}|;\':",./<>?/// aaaa_bbbb \\ ?@#$?`~'.to_slug, 'aaaa-bbbb'
assert_equal '////// meph1sto r0x ! \\\\\\'.to_slug, 'meph1sto-r0x'
assert_equal "!@ To$Łódź?żółć!pójdź[]do-mnie".to_slug, "to-lodz-zolc-pojdz-do-mnie"
assert_equal "zAżÓłĆ_łódź-–--mą_mnĄ–kÔÔ-tęrr".to_slug, "zazolc-lodz-ma-mna-koo-terr"
assert_equal "Czy to był kaszëbskô horror w Żarach?".to_slug, 'czy-to-byl-kaszebsko-horror-w-zarach'
assert_equal '!@ To$Łódź?żółć!pójdź[]do-mnie'.to_slug, 'to-lodz-zolc-pojdz-do-mnie'
assert_equal 'zAżÓłĆ_łódź-–--mą_mnĄ–kÔÔ-tęrr'.to_slug, 'zazolc-lodz-ma-mna-koo-terr'
assert_equal 'Czy to był kaszëbskô horror w Żarach?'.to_slug, 'czy-to-byl-kaszebsko-horror-w-zarach'

# sanitize_as_filename
assert_equal 'C:\Temp\ęóąśłżźćń.txt'.sanitize_as_filename, 'eoaslzzcn.txt'
end

end

0 comments on commit c8f6152

Please sign in to comment.