Sha256: 35d5acf1d8322ee995b066eae9d6b70e2c8096349e6b286224070d13a5b8c2ff

Contents?: true

Size: 884 Bytes

Versions: 3

Compression:

Stored size: 884 Bytes

Contents

# frozen_string_literal: true

module RubyLokaliseApi
  module Utils
    module Strings
      refine String do
        # Converts a string to snake_case format.
        # Original code inspired by the Facets gem by Rubyworks:
        # https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
        def snakecase
          gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). # Handle acronyms like 'HTMLParser'
            gsub(/([a-z\d])([A-Z])/, '\1_\2').      # Handle camelCase to snake_case
            tr('-', '_').                           # Replace dashes with underscores
            gsub(/\s/, '_').                        # Replace spaces with underscores
            gsub(/__+/, '_').                       # Collapse multiple underscores
            downcase # Convert everything to lowercase
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-lokalise-api-9.3.0 lib/ruby_lokalise_api/utils/strings.rb
ruby-lokalise-api-9.2.1 lib/ruby_lokalise_api/utils/strings.rb
ruby-lokalise-api-9.2.0 lib/ruby_lokalise_api/utils/strings.rb