Sha256: 63a70c526a25fff96a6c863d02396caac73dc8be90dc4527986e106357036a7a

Contents?: true

Size: 1.18 KB

Versions: 40

Compression:

Stored size: 1.18 KB

Contents

module Eco
  module Data
    module Strings
      module CamelCase
        # From ActiveSupport
        # @see https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-camelize
        def camel_case(term, uppercase_first_letter = false)
          string = term.to_s
          # String#camelize takes a symbol (:upper or :lower), so here we also support :lower to keep the methods consistent.
          if !uppercase_first_letter || uppercase_first_letter == :lower
            string = string.sub(camel_regex) { |match| match.downcase! || match }
          elsif string.match?(/\A[a-z\d]*\z/)
            return string.capitalize
          else
            string = string.sub(/^[a-z\d]*/) { |match| match.capitalize! || match }
          end
          string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do
            word = $2
            substituted = word.capitalize! || word
            $1 ? "::#{substituted}" : substituted
          end
          string
        end

        def camel_regex
          @camel_regex ||= /^(?:#{acro_regex}(?=\b|[A-Z_])|\w)/
        end

        def acro_regex
          @acro_regex ||= /(?=a)b/
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.20 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.19 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.18 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.17 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.16 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.15 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.14 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.13 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.12 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.11 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.10 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.9 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.8 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.7 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.6 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.5 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.4 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.3 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.2 lib/eco/data/strings/camel_case.rb