tsbase
Version:
Base class libraries for TypeScript
125 lines • 5.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Xml = void 0;
var tslib_1 = require("tslib");
var Regex_1 = require("./Regex");
var Strings_1 = require("./Strings");
var NodeTypes;
(function (NodeTypes) {
NodeTypes["String"] = "string";
NodeTypes["Integer"] = "integer";
NodeTypes["Decimal"] = "decimal";
NodeTypes["Boolean"] = "boolean";
NodeTypes["Object"] = "object";
NodeTypes["Array"] = "array";
})(NodeTypes || (NodeTypes = {}));
/**
* This feature is currently in development - it is not exported from System/module or the package root for this reason.
* It may meet your needs, however, it may change significantly without being reflected such in the package version
*/
var Xml = /** @class */ (function () {
function Xml() {
}
/**
* Returns the XML string representation of the given json object
* @param json
*/
Xml.FromJson = function (json, rootNodeName, schema) {
if (rootNodeName === void 0) { rootNodeName = 'root'; }
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".concat(this.getXmlStringFromValue(json, rootNodeName, schema));
};
/**
* Returns the parsed json representation of the given xml string
* @param xml
*/
Xml.ToJson = function (xml) {
var _this = this;
var xmlTagsRegex = /(?<openTag><(?<tagName>[^\s]*)[\s]?(?<typeAttribute>type="xs:(?<type>[^"]*)")?[^<]*>)(?<content>.*)(?<closeTag><\/\2>)/g;
var results = Regex_1.Regex.AggregateMatches(xmlTagsRegex, xml, [], function (match, cv) {
if (match) {
cv.push(match.groups);
}
});
var getValueForTypeFunctionMap = new Map([
[NodeTypes.Object, function (tag) { return _this.ToJson(tag.content || ''); }],
[NodeTypes.String, function (tag) { var _a; return (_a = tag.content) === null || _a === void 0 ? void 0 : _a.toString().trim(); }],
[NodeTypes.Integer, function (tag) { return parseInt(tag.content || '0'); }],
[NodeTypes.Decimal, function (tag) { return parseFloat(tag.content || '0'); }],
[NodeTypes.Boolean, function (tag) { return tag.content === 'true'; }],
[NodeTypes.Array, function (tag) {
// eslint-disable-next-line max-len
var xmlArrayItemsRegex = /(?<openTag><(?<tagName>[^\s]*)[\s]?(?<typeAttribute>type="xs:(?<type>[^"]*)")?[^<]*>)(?<content>[^<]*)(?<closeTag><\/\2>)/g;
return Regex_1.Regex.AggregateMatches(xmlArrayItemsRegex, tag.content || '', [], function (match, cv) {
if (match) {
var valueFunction = getValueForTypeFunctionMap.get(match.groups.type);
cv.push(valueFunction ? valueFunction(match.groups) : match.groups.content);
}
});
}]
]);
var obj = {};
results.forEach(function (tag) {
var _a;
var type = tag.type || (function () {
var _a;
if (['array', 'set', 'list'].some(function (e) { var _a; return ((_a = tag.tagName) === null || _a === void 0 ? void 0 : _a.includes(e)) || ''; })) {
return NodeTypes.Array;
}
else if ((_a = tag.content) === null || _a === void 0 ? void 0 : _a.includes('</')) {
return NodeTypes.Object;
}
else {
return NodeTypes.String;
}
})();
if (tag.tagName) {
if (tag.tagName === 'root') {
obj = _this.ToJson(tag.content || '');
}
else {
var valueFunction = getValueForTypeFunctionMap.get(type);
obj = tslib_1.__assign(tslib_1.__assign({}, obj), (_a = {}, _a[tag.tagName] = valueFunction ? valueFunction(tag) : tag.content, _a));
}
}
});
return obj;
};
Xml.getXmlStringFromValue = function (value, nodeName, schema) {
var _this = this;
nodeName = isNaN(parseInt(nodeName)) ? nodeName : 'item';
var getXmlNodeWithType = function (type, innerValue) {
if (innerValue === void 0) { innerValue = value; }
return "<".concat(nodeName).concat(schema ? " xmlns=\"".concat(schema, "\"") : Strings_1.Strings.Empty, " type=\"xs:").concat(type, "\">").concat(innerValue, "</").concat(nodeName, ">");
};
var getXmlFromNumber = function () {
var isDecimal = value.toString().includes('.');
return getXmlNodeWithType(isDecimal ? NodeTypes.Decimal : NodeTypes.Integer);
};
var valueType = typeof value;
var valueTypeFunctionMap = new Map([
['number', getXmlFromNumber],
['bigint', getXmlFromNumber],
['string', function () { return getXmlNodeWithType(valueType); }],
['boolean', function () { return getXmlNodeWithType(valueType); }],
['object', function () {
var isArray = Array.isArray(value);
var content = '';
for (var key in value) {
var keyValue = value[key];
content = content.concat(_this.getXmlStringFromValue(keyValue, key));
}
return getXmlNodeWithType(isArray ? NodeTypes.Array : NodeTypes.Object, content);
}]
]);
var valueTypeFunction = valueTypeFunctionMap.get(valueType);
if (valueTypeFunction) {
return valueTypeFunction();
}
else {
throw new Error("Unable to parse xml from type ".concat(valueType));
}
};
return Xml;
}());
exports.Xml = Xml;
//# sourceMappingURL=Xml.js.map