Skip to content

Instantly share code, notes, and snippets.

@rashfael
Created January 12, 2017 15:00
Show Gist options
  • Save rashfael/7f4039a56f044ab76fbe6ae6807aa61e to your computer and use it in GitHub Desktop.
Save rashfael/7f4039a56f044ab76fbe6ae6807aa61e to your computer and use it in GitHub Desktop.
function snakeToCamel(str) {
var parts = str.split('_');
return parts.reduce(function (p, c) {
return p + c.charAt(0).toUpperCase() + c.slice(1);
}, parts.shift());
}
function toCamelCase(object, exceptions) {
exceptions = exceptions || [];
return Object.keys(object).reduce(function (p, key) {
var newKey = exceptions.indexOf(key) === -1 ? snakeToCamel(key) : key;
p[newKey] = typeof(object[key]) === 'object' ? toCamelCase(object[key]) : object[key];
return p;
}, {});
}
console.log(toCamelCase({app_metadata:{groups:["a", "b", "c"]}}))
// outputs: { appMetadata: { groups: { '0': 'a', '1': 'b', '2': 'c' } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment