Skip to content

Instantly share code, notes, and snippets.

@carloscarvallo
Last active February 21, 2018 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carloscarvallo/5265c5958d9a623959c56eef13be1ead to your computer and use it in GitHub Desktop.
Save carloscarvallo/5265c5958d9a623959c56eef13be1ead to your computer and use it in GitHub Desktop.
Recursive JS script example using lodash 4.x to transform all camel case occurrences to snake case
/*
* Recursive script example using lodash 4.x to transform all camel case occurrences to snake case
* carloscarvallo87@gmail.com
*/
//====================================================================================================================
// script
//====================================================================================================================
var regex = /([a-z])([A-Z])/g
var _toSnakeCase = function (val) {
if (typeof val === "string") {
if (regex.test(val)) {
return (val.replace(regex, function (_, $1, $2) {
return ($1+"_"+$2).toLowerCase()
})).toLowerCase()
} else return val.toLowerCase()
} else return val
}
var _mapToSnakeCase = function(obj) {
if (_.isPlainObject(obj)) {
var mappedKeys = _.mapKeys(obj, function(val, key) { return _toSnakeCase(key) })
return _.mapValues(mappedKeys, function(val, key) { return _mapToSnakeCase(val) })
}
else if (_.isArray(obj)) return _.map(obj, function(item) { return _mapToSnakeCase(item) })
else return _toSnakeCase(obj)
}
//====================================================================================================================
// sample data
//====================================================================================================================
var simpleSource = {
"thisIsSimple": "simpleValue"
}
var moreComplexSource = {
"userId": 1,
"id": 1,
"title": "suntAutFacere",
"thisBody": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto",
"otherPosts": [
{
"thisIsTheTitle": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"thisIsTheBody": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
[
"hisFather",
"hisMother",
"hisBrother",
"hisSon"
]
],
"whereToGo": [
"toUS",
"toCA",
"toES",
"toMX"
]
}
var arraySource = [
{
"userId": 1,
"id": 1,
"thisIsTheTitle": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"thisIsTheBody": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 4,
"id": 2,
"thisIsTheTitle": "qui est esse",
"thisIsTheBody": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}
]
//====================================================================================================================
// testing
//====================================================================================================================
console.log(JSON.stringify(_mapToSnakeCase(simpleSource),null,4))
console.log(JSON.stringify(_mapToSnakeCase(moreComplexSource),null,4))
console.log(JSON.stringify(_mapToSnakeCase(arraySource),null,4))
//====================================================================================================================
// output
//====================================================================================================================
/*
{
"this_is_simple": "simple_value"
}
{
"user_id": 1,
"id": 1,
"title": "sunt_aut_facere",
"this_body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto",
"other_posts": [
{
"this_is_the_title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"this_is_the_body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
[
"his_father",
"his_mother",
"his_brother",
"his_son"
]
],
"where_to_go": [
"to_us",
"to_ca",
"to_es",
"to_mx"
]
}
[
{
"user_id": 1,
"id": 1,
"this_is_the_title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"this_is_the_body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"user_id": 4,
"id": 2,
"this_is_the_title": "qui est esse",
"this_is_the_body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment