Created
January 12, 2017 15:00
-
-
Save rashfael/7f4039a56f044ab76fbe6ae6807aa61e to your computer and use it in GitHub Desktop.
taken from https://github.com/auth0/auth0.js/blob/109f7ad005850bec4c742c437c1ce731b7cd71bb/src/helper/object.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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