UNPKG

tsbase

Version:

Base class libraries for TypeScript

118 lines 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Xml = void 0; const Regex_1 = require("./Regex"); const 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 */ class Xml { constructor() { } /** * Returns the XML string representation of the given json object * @param json */ static FromJson(json, rootNodeName = 'root', schema) { return `<?xml version="1.0" encoding="UTF-8"?>${this.getXmlStringFromValue(json, rootNodeName, schema)}`; } /** * Returns the parsed json representation of the given xml string * @param xml */ static ToJson(xml) { const xmlTagsRegex = /(?<openTag><(?<tagName>[^\s]*)[\s]?(?<typeAttribute>type="xs:(?<type>[^"]*)")?[^<]*>)(?<content>.*)(?<closeTag><\/\2>)/g; const results = Regex_1.Regex.AggregateMatches(xmlTagsRegex, xml, [], (match, cv) => { if (match) { cv.push(match.groups); } }); const getValueForTypeFunctionMap = new Map([ [NodeTypes.Object, (tag) => this.ToJson(tag.content || '')], [NodeTypes.String, (tag) => { var _a; return (_a = tag.content) === null || _a === void 0 ? void 0 : _a.toString().trim(); }], [NodeTypes.Integer, (tag) => parseInt(tag.content || '0')], [NodeTypes.Decimal, (tag) => parseFloat(tag.content || '0')], [NodeTypes.Boolean, (tag) => tag.content === 'true'], [NodeTypes.Array, (tag) => { // eslint-disable-next-line max-len const xmlArrayItemsRegex = /(?<openTag><(?<tagName>[^\s]*)[\s]?(?<typeAttribute>type="xs:(?<type>[^"]*)")?[^<]*>)(?<content>[^<]*)(?<closeTag><\/\2>)/g; return Regex_1.Regex.AggregateMatches(xmlArrayItemsRegex, tag.content || '', [], (match, cv) => { if (match) { const valueFunction = getValueForTypeFunctionMap.get(match.groups.type); cv.push(valueFunction ? valueFunction(match.groups) : match.groups.content); } }); }] ]); let obj = {}; results.forEach(tag => { const type = tag.type || (() => { var _a; if (['array', 'set', 'list'].some(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 { const valueFunction = getValueForTypeFunctionMap.get(type); obj = { ...obj, [tag.tagName]: valueFunction ? valueFunction(tag) : tag.content }; } } }); return obj; } static getXmlStringFromValue(value, nodeName, schema) { nodeName = isNaN(parseInt(nodeName)) ? nodeName : 'item'; const getXmlNodeWithType = (type, innerValue = value) => `<${nodeName}${schema ? ` xmlns="${schema}"` : Strings_1.Strings.Empty} type="xs:${type}">${innerValue}</${nodeName}>`; const getXmlFromNumber = () => { const isDecimal = value.toString().includes('.'); return getXmlNodeWithType(isDecimal ? NodeTypes.Decimal : NodeTypes.Integer); }; const valueType = typeof value; const valueTypeFunctionMap = new Map([ ['number', getXmlFromNumber], ['bigint', getXmlFromNumber], ['string', () => getXmlNodeWithType(valueType)], ['boolean', () => getXmlNodeWithType(valueType)], ['object', () => { const isArray = Array.isArray(value); let content = ''; for (const key in value) { const keyValue = value[key]; content = content.concat(this.getXmlStringFromValue(keyValue, key)); } return getXmlNodeWithType(isArray ? NodeTypes.Array : NodeTypes.Object, content); }] ]); const valueTypeFunction = valueTypeFunctionMap.get(valueType); if (valueTypeFunction) { return valueTypeFunction(); } else { throw new Error(`Unable to parse xml from type ${valueType}`); } } } exports.Xml = Xml; //# sourceMappingURL=Xml.js.map