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.1 lib/eco/data/strings/camel_case.rb
eco-helpers-3.0.0 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.25 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.24 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.23 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.22 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.21 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.20 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.19 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.18 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.17 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.16 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.15 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.14 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.13 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.12 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.4 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.2 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.1 lib/eco/data/strings/camel_case.rb
eco-helpers-2.7.0 lib/eco/data/strings/camel_case.rb