Created
December 12, 2015 01:43
-
-
Save jiggak/23a6c25e70688c7234e2 to your computer and use it in GitHub Desktop.
Recursively rename properties from UpperCamelCase to lowerCamelCase
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
var lowerCamelCase = function(key) { | |
if (key === 'ID') return 'id'; | |
return key.charAt(0).toLowerCase() + key.slice(1); | |
}; | |
var lowerCamelCaseKeys = function(obj, opts) { | |
if (!opts) opts = {}; | |
var result = obj; | |
if (angular.isArray(obj)) { | |
result = _.map(obj, function(val) { | |
return lowerCamelCaseKeys(val); | |
}); | |
} else if (angular.isObject(obj)) { | |
if (!opts.inplace) | |
result = {}; | |
_.each(obj, function(val, key) { | |
result[lowerCamelCase(key)] = lowerCamelCaseKeys(val); | |
if (opts.inplace) | |
delete result[key]; | |
}); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment