Skip to content

eventide-project/casing

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published