Sha256: 80c94edc366fe8f8eb94965f522797b9fdcbede31a8785f70035ab67d6740739

Contents?: true

Size: 774 Bytes

Versions: 28

Compression:

Stored size: 774 Bytes

Contents

# frozen_string_literal: true

class String
  #
  # These two methods are to cut down on object allocation
  #

  def self.optionize_unwanted_chars_regex
    @optionize_unwanted_chars_regex ||= /[^a-z0-9\-_]+/
  end

  def self.optionize_seperator_regexes
    @optionize_seperator_regexes ||= {}
  end

  def optionize(sep = '-')
    result = downcase
    result.strip!

    # Turn unwanted chars into the separator
    result.gsub!(String.optionize_unwanted_chars_regex, sep)

    # No more than one of the separator in a row.
    String.optionize_seperator_regexes[sep] ||= /#{Regexp.escape(sep)}{2,}/
    result.gsub!(String.optionize_seperator_regexes[sep], sep)

    result.underscore
  end
  alias_method :slugify, :optionize
  alias_method :systemize, :optionize
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.26 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.25 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.23 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.22 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.21 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.20 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.19 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.18 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.17 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.16 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.15 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.14 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.13 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.12 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.11 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.10 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.9 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.8 lib/workarea/ext/freedom_patches/string.rb
workarea-core-3.5.7 lib/workarea/ext/freedom_patches/string.rb