Class: String
Constant Summary collapse
- SLUGIFY_SEPARATORS =
%w[- _ +]
Instance Method Summary collapse
Instance Method Details
#slugify(separator = "-") ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/zwite/ext/string.rb', line 5 def slugify(separator = "-") unless SLUGIFY_SEPARATORS.include?(separator) raise "Word separator must be one of #{SLUGIFY_SEPARATORS}" end re_separator = Regexp.escape(separator) result = self.dup result.gsub!(/[^\x00-\x7F]+/, '') # Remove non-ASCII (e.g. diacritics). result.gsub!(/[^a-z0-9\-_\+]+/i, separator) # Turn non-slug chars into the separator. result.gsub!(/#{re_separator}{2,}/, separator) # No more than one of the separator in a row. result.gsub!(/^#{re_separator}|#{re_separator}$/, '') # Remove leading/trailing separator. result.downcase! return result end |