Skip to content

Instantly share code, notes, and snippets.

@czj
Created September 2, 2011 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czj/1188243 to your computer and use it in GitHub Desktop.
Save czj/1188243 to your computer and use it in GitHub Desktop.
# encoding: utf-8
class String
EUROPEAN_ACCENTS = {
['Ã','Ä','Â','À'] => 'A',
['á','à','â','ä','ã'] => 'a',
['Ë','É','È','Ê'] => 'E',
['é','è','ê','ë'] => 'e',
['Î','Ì'] => 'I',
['í','ì','î','ï'] => 'i',
['Õ','Ö','Ô','Ò'] => 'O',
['ó','ò','ô','ö','õ'] => 'o',
['œ'] => 'oe',
['ç'] => 'c',
['ñ'] => 'n',
['ÿ'] => 'y',
['œ'] => 'oe',
['ß'] => 'ss',
['ú','ù','û','ü','Û','Ù'] => 'u'
}.freeze
def without_accents
self.clone.without_accents!
end
def without_accents!
EUROPEAN_ACCENTS.each { |accents, replacement| accents.each { |accent| gsub! accent, replacement } }
self
end
def limit_to(limit)
(self.size > limit and limit > 0) ? self[0..(limit - 1)] : self
end
def sanitized_and_limited_to(limit)
strip.without_accents.limit_to(limit)
end
def all_upcase?
self == self.upcase
end
def all_downcase?
self == self.downcase
end
def crop(length)
self.length > length ? self[0..(length - 1)] : self
end
def without_orphan_in_html
clone.tap do |str|
str.without_orphan_in_html!
end
end
def without_orphan_in_html!
self.gsub! /\s+(\S+)$/, " \\1"
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment