Sha256: 27ecd367da314b084a571c640ac89a9deae80e1c52493c159bce28d61f2d960a

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

# frozen_string_literal: true

module CaseTransform2
  # String class extension
  # Stolen from ActiveSupport::Inflector.camelize
  # @api private
  class StringExt
    def camelize(string, first_letter = :upper)
      raise ArgumentError, "Argument can not be nil" unless %i[upper lower].include?(first_letter)
      str = string.to_s
      str = str.gsub(/^[a-z\d]*/, &:capitalize) if first_letter == :upper
      str.gsub(/(?:_|(\/))([a-z\d]*)/i) do
        "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
      end.gsub("/", "::")
    end

    def dasherize(string)
      string.tr("_", "-")
    end

    # Only support camel to underscore
    def underscore(string)
      str = string.to_s
      return str unless str =~ /[A-Z-]|::/
      str.gsub("::", "/")
         .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
         .gsub(/([a-z\d])([A-Z])/, '\1_\2')
         .tr("-", "_")
         .downcase
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
case_transform2-1.0.0 lib/case_transform2/string_ext.rb