Skip to content

Instantly share code, notes, and snippets.

@kirilloid
Created March 3, 2017 16:51
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 kirilloid/5890e201255327a4341e1626a9d60ab3 to your computer and use it in GitHub Desktop.
Save kirilloid/5890e201255327a4341e1626a9d60ab3 to your computer and use it in GitHub Desktop.
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