Created
September 2, 2015 18:45
-
-
Save nakosung/a6e54cd893899e453605 to your computer and use it in GitHub Desktop.
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
var test2 = { | |
"font.size" : 3, | |
"font.family" : "Arial", | |
"text" : "Attr", | |
"some-nice-attr.some-nice-child.some-nice-grand-child" : 3, | |
"some-nice-attr.some-nice-niece" : 4 | |
}; | |
var _ = require('lodash') | |
function beautify(k) { | |
var arr = k.split('-').map(function(x) { | |
return x.charAt(0).toUpperCase() + x.slice(1); | |
}); | |
return arr.join(''); | |
} | |
function conv(obj) { | |
var out = {}; | |
for (var k in obj) { | |
var v = obj[k]; | |
var index = k.indexOf('.'); | |
if (index >= 0) { | |
var lhs = k.substr(0,index); | |
var rhs = k.substr(index+1); | |
var tmp = {}; | |
tmp[rhs] = v; | |
k = lhs; | |
v = tmp; | |
} | |
k = beautify(k); | |
if (_.isObject(v)) { | |
out[k] = out[k] || {}; | |
_.extend(out[k],conv(v)); | |
} else { | |
out[k] = v; | |
} | |
} | |
return out; | |
} | |
console.log(JSON.stringify(conv(test2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment