UNPKG

emsi-lib

Version:

A EMSI (Emergency Management Shared Information) object model to manipulate emergency messages defined in ISO/TR 22351 document

52 lines (51 loc) 1.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.xml2js = exports.js2xml = void 0; const he_1 = __importDefault(require("he")); const fast_xml_parser_1 = require("fast-xml-parser"); const options2xml = { ignoreAttributes: true, format: true, indentBy: ' ', supressEmptyNode: true, tagValueProcessor: (value) => he_1.default.encode(value, { useNamedReferences: true }) }; const options2js = { ignoreAttributes: true, ignoreNameSpace: true, allowBooleanAttributes: false, parseNodeValue: false, parseAttributeValue: false, trimValues: true, parseTrueNumberOnly: false, arrayMode: false, tagValueProcessor: (text) => he_1.default.decode(text) }; function js2xml(node, options, fn) { return (fn || defaultJs2Xml)(node, options); } exports.js2xml = js2xml; function defaultJs2Xml(node, options) { const p2xml = new fast_xml_parser_1.j2xParser(Object.assign(Object.assign({}, options2xml), options)); let xml = p2xml.parse(node); if (options === null || options === void 0 ? void 0 : options.toUpperCase) xml = xml.replace(/<\/?[\w]+\/?>/g, (c) => c.toUpperCase()); if (options === null || options === void 0 ? void 0 : options.setNameSpace) xml = xml.replace(/(<\/?)/g, (c, p1) => p1 + (options === null || options === void 0 ? void 0 : options.setNameSpace) + ':'); return xml; } function xml2js(xml, options, fn) { return (fn || defaultXml2js)(xml, options); } exports.xml2js = xml2js; function defaultXml2js(xml, options) { if (fast_xml_parser_1.validate(xml) === true) { // Remove NameSpace const toJs = xml.replace(/(<\/?)[\w]*:/g, (c, p1) => p1); return fast_xml_parser_1.parse(toJs, Object.assign(Object.assign({}, options2js), options)); } return undefined; }