Sha256: 941bafc0c6cfcbd96df2dc50cc6d144839779b71f131540f97d27e93534db29a

Contents?: true

Size: 899 Bytes

Versions: 15

Compression:

Stored size: 899 Bytes

Contents

# frozen_string_literal: true

class Ree::StringUtils
  class << self
    def truncate(str, limit = 80)
      str.length > limit ? "#{str[0..limit]}..." : str
    end

    def underscore(camel_cased_word)
      return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
      word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
      word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
      word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
      word.tr!("-".freeze, "_".freeze)
      word.downcase!
      word
    end

    def camelize(string, uppercase_first_letter = true)
      if uppercase_first_letter
        string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
      else
        string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
      end

      string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ree-1.0.47 lib/ree/utils/string_utils.rb
ree-1.0.46 lib/ree/utils/string_utils.rb
ree-1.0.45 lib/ree/utils/string_utils.rb
ree-1.0.44 lib/ree/utils/string_utils.rb
ree-1.0.43 lib/ree/utils/string_utils.rb
ree-1.0.42 lib/ree/utils/string_utils.rb
ree-1.0.41 lib/ree/utils/string_utils.rb
ree-1.0.40 lib/ree/utils/string_utils.rb
ree-1.0.39 lib/ree/utils/string_utils.rb
ree-1.0.38 lib/ree/utils/string_utils.rb
ree-1.0.37 lib/ree/utils/string_utils.rb
ree-1.0.36 lib/ree/utils/string_utils.rb
ree-1.0.35 lib/ree/utils/string_utils.rb
ree-1.0.34 lib/ree/utils/string_utils.rb
ree-1.0.33 lib/ree/utils/string_utils.rb