Created
May 10, 2016 06:40
-
-
Save simongong/14d39e113f1514a0264a3355efb44b15 to your computer and use it in GitHub Desktop.
JavaScript: convert format of object keys between camel-case and snake-case
This file contains hidden or 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
/** | |
* @param {Object|String} data string or keys of object are named in form of snake | |
* @param {number} depth to which level of keys should it process | |
* @return {Object|String} string or keys of object are named in form of camel case | |
*/ | |
exports.snakeToCamel = function(data, depth) { | |
if (Util.isObject(data)) { | |
if (typeof depth === 'undefined') { | |
depth = 1; | |
} | |
return _processKeys(data, _camelize, depth); | |
} else { | |
return _camelize(data); | |
} | |
}; | |
/** | |
* @param {Object|String} data string or keys of object are named in form of camel case | |
* @param {number} depth to which level of keys should it process | |
* @return {Object|String} string or keys of object are named in form of snake | |
*/ | |
exports.camelToSnake = function(data, depth) { | |
if (Util.isObject(data)) { | |
if (typeof depth === 'undefined') { | |
depth = 1; | |
} | |
return _processKeys(data, _snakelize, depth); | |
} else { | |
return _snakelize(data); | |
} | |
}; | |
// snakelize a string formed in underscore | |
function _snakelize(key) { | |
let separator = '_'; | |
let split = /(?=[A-Z])/; | |
return key.split(split).join(separator).toLowerCase(); | |
} | |
// camelize a string formed in underscore | |
function _camelize(key) { | |
if (Util.isNumber(key)) { | |
return key; | |
} | |
key = key.replace(/[\-_\s]+(.)?/g, function(match, ch) { | |
return ch ? ch.toUpperCase() : ''; | |
}); | |
// Ensure 1st char is always lowercase | |
return key.substr(0, 1).toLowerCase() + key.substr(1); | |
} | |
// camelize/snakelize keys of an object | |
// @param {number} depth to which level of keys should it process | |
function _processKeys(obj, processer, depth) { | |
if (depth === 0 || !Util.isObject(obj)) { | |
return obj; | |
} | |
let result = {}; | |
let keys = Object.keys(obj); | |
for (let i = 0; i < keys.length; i++) { | |
result[processer(keys[i])] = _processKeys(obj[keys[i]], processer, depth - 1); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment