craydent-xml-to-json
Version:
Node module to parse xml into json
35 lines (34 loc) • 1.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = __processAttributes;
const _generalTrim_1 = require("../protected/_generalTrim");
const craydent_filltemplate_1 = require("craydent.filltemplate");
const craydent_tryeval_1 = require("craydent.tryeval");
function __processAttributes(node, refs) {
let obj = {}, tagend = node.indexOf('>'), tag = node.substring(1, tagend), attIndex = tag.search(/\s|>/), attr = !~attIndex ? "" : tag.substring(attIndex), textEnd = node.indexOf('<', tagend), text = node.substring(tagend + 1, textEnd);
if (!~textEnd) {
text = '';
}
// self closing
if (attr[attr.length - 1] == "/") {
attr = attr.substring(0, attr.length - 1);
text = "";
}
attr = (0, _generalTrim_1.default)(attr);
if (text) {
obj['#text'] = (0, craydent_filltemplate_1.default)(text, refs);
}
if (attr) {
let attributes = attr.split(' ');
for (let i = 0, len = attributes.length; i < len; i++) {
let attribute = attributes[i];
if (!attribute) {
continue;
}
let key_val = attribute.split('=');
obj['@attributes'] = obj['@attributes'] || {};
obj['@attributes'][key_val[0].trim()] = (0, craydent_tryeval_1.default)(key_val[1]) || key_val[1].trim();
}
}
return obj;
}