Sha256: e247fa1595181b710ac0597c86b55e6145d681cd5b6c9f42b37849edcc6ea92b

Contents?: true

Size: 735 Bytes

Versions: 2

Compression:

Stored size: 735 Bytes

Contents

# frozen_string_literal: true

module AppInfo
  module Inflector
    def snakecase
      gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .gsub(/\s/, '_')
        .gsub(/__+/, '_')
        .downcase
    end

    def camelcase(first_letter: :upper, separators: ['-', '_', '\s'])
      str = dup

      separators.each do |s|
        str = str.gsub(/(?:#{s}+)([a-z])/) { $1.upcase }
      end

      case first_letter
      when :upper, true
        str = str.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase }
      when :lower, false
        str = str.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
      end

      str
    end
  end
end

class String
  include AppInfo::Inflector
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
app-info-2.7.0.beta2 lib/app_info/core_ext/string/inflector.rb
app-info-2.7.0.beta1 lib/app_info/core_ext/string/inflector.rb