Created
March 3, 2017 16:51
-
-
Save kirilloid/5890e201255327a4341e1626a9d60ab3 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
function pascalize(string) { | |
return string.replace(/-([a-z])/g, (_, c) => c.toUpperCase()); | |
} | |
function normalize(value) { | |
if (/^(null|true|false)$/.test(value)) { | |
return JSON.parse(value); | |
} | |
return value; | |
} | |
export function xml2json(node) { | |
var obj = {}; | |
[].slice.call(node.attributes).forEach(function (name) { | |
obj[pascalize(attr.name)] = attr.value; | |
}); | |
if (node.children) { | |
obj[pascalize(obj.tagName.toLowerCase())] = [].map.call(node.children, xml2json); | |
} else if (node.text) { | |
obj.value = normalize(node.text); | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment