Project

evt-casing

0.01
No release in over 3 years
Low commit activity in last 3 years
Convert the case of strings, symbols, and hash keys, including camelCase, PascalCase, and underscore_case
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

casing

Convert the case of strings, symbols, and hash keys, including camelCase, PascalCase, and underscore_case

Examples

Convert a Hash's Keys to Camel Case

Useful for converting Ruby names to JavaScript style naming in preparation for serializing a Hash instance to a JSON string

h = {
  symbol_key: 'symbol value',
  'text_key' => 'text value',
  camelKey: 'camel value',
  :PascalKey => 'pascal value',
  nested_hash: {
    nested_symbol_key: 'nested symbol value',
    'nested_text_key' => 'nested text value',
    nestedCamelKey: 'nested camel value',
    :NestedPascalKey => 'nested pascal value'
  },
  nested_array: [
    { array_nested_hash_1: 'nested value 1' },
    { array_nested_hash_2: 'nested value 2' }
  ]
}

camel_hash = Casing::Camel.(h)

# => {
  "symbolKey" => "symbol value",
  "textKey" => "text value",
  "camelKey" => "camel value",
  "pascalKey" => "pascal value",
  "nestedHash" => {
    "nestedSymbolKey" => "nested symbol value",
    "nestedTextKey" => "nested text value",
    "nestedCamelKey" => "nested camel value",
    "nestedPascalKey" => "nested pascal value"
  },
  "nestedArray" => [
    {"arrayNestedHash1" => "nested value 1"},
    {"arrayNestedHash2" => "nested value 2"}
  ]
}

Convert a Hash's Keys to Underscore Case

Useful for converting JavaScript names to Ruby style naming after deserializing a JSON string to a Ruby Hash.

h = {
  symbol_key: 'symbol value',
  'text_key' => 'text value',
  camelKey: 'camel value',
  :PascalKey => 'pascal value',
  nested_hash: {
    nested_symbol_key: 'nested symbol value',
    'nested_text_key' => 'nested text value',
    nestedCamelKey: 'nested camel value',
    :NestedPascalKey => 'nested pascal value'
  },
  nested_array: [
    { array_nested_hash_1: 'nested value 1' },
    { array_nested_hash_2: 'nested value 2' }
  ]
}

camel_hash = Casing::Underscore.(h)

# => {
  "symbol_key" => "symbol value",
  "text_key" => "text value",
  "camel_key" => "camel value",
  "pascal_key" => "pascal value",
  "nested_hash"=> {
    "nested_symbol_key" => "nested symbol value",
    "nested_text_key" => "nested text value",
    "nested_camel_key" => "nested camel value",
    "nested_pascal_key" => "nested pascal value"},
  "nested_array"=> [
    {"array_nested_hash_1" => "nested value 1"},
    {"array_nested_hash_2" => "nested value 2"}
  ]
}

License

The casing library is released under the MIT License.