Module: MuchSlug::Slug

Defined in:
lib/much-slug/slug.rb

Class Method Summary collapse

Class Method Details

.new(string, preprocessor:, separator:, allow_underscores: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/much-slug/slug.rb', line 6

def self.new(string, preprocessor:, separator:, allow_underscores: true)
  regexp_escaped_sep = Regexp.escape(separator)

  slug = preprocessor.call(string.to_s.dup)
  # Turn unwanted chars into the separator
  slug.gsub!(/[^\w#{regexp_escaped_sep}]+/, separator)
  # Turn underscores into the separator, unless allowing
  slug.gsub!(/_/, separator) unless allow_underscores
  # No more than one of the separator in a row.
  slug.gsub!(/#{regexp_escaped_sep}{2,}/, separator)
  # Remove leading/trailing separator.
  slug.gsub!(/\A#{regexp_escaped_sep}|#{regexp_escaped_sep}\z/, "")
  slug
end