@mos-connection/helper
Version:
Helper functions for the MOS-connection library
90 lines (88 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLROFullStory = void 0;
const xmlConversion_1 = require("../profile2/xmlConversion");
const lib_1 = require("../lib");
const parseMosTypes_1 = require("../parseMosTypes");
const ParseError_1 = require("../ParseError");
const ensureMethods_1 = require("../../utils/ensureMethods");
/* eslint-disable @typescript-eslint/no-namespace */
var XMLROFullStory;
(function (XMLROFullStory) {
function fromXML(path, xml0, strict) {
try {
const xml = (0, ensureMethods_1.ensureXMLObject)(xml0, strict);
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const story0 = xmlConversion_1.XMLROStory.fromXML('', xml, strict);
const story = {
ID: story0.ID,
Slug: story0.Slug,
Number: story0.Number,
MosExternalMetaData: story0.MosExternalMetaData,
RunningOrderId: mosTypes.mosString128.createRequired(xml.roID, 'roID'),
Body: ParseError_1.ParseError.handleError(() => fromXMLStoryBody(xml.storyBody, strict), 'storyBody'),
};
(0, lib_1.omitUndefined)(story);
return story;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLROFullStory.fromXML = fromXML;
})(XMLROFullStory = exports.XMLROFullStory || (exports.XMLROFullStory = {}));
function fromXMLStoryBody(xml, strict) {
const body = [];
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
/*
// Not able to implement this currently, need to change {arrayNotation: true} in xml2json option
let elementKeys = Object.keys(xml)
elementKeys.forEach((key: string) => {
// let elements
let d = xml[key]
if (!Array.isArray(d)) d = [d]
d.forEach((el: any) => {
let bodyItem: IMOSROFullStoryBodyItem = {
Type: key,
Content: el
}
body.push(bodyItem)
})
})
*/
if (xml.elements && Array.isArray(xml.elements)) {
for (const item of (0, ensureMethods_1.ensureXMLObjectArray)(xml.elements, strict)) {
const type = mosTypes.string.createRequired(item.$name || item.$type, 'name/type');
let bodyItem;
if (type === 'storyItem') {
bodyItem = {
itemType: 'storyItem',
Type: 'storyItem',
Content: xmlConversion_1.XMLMosItem.fromXML('elements', item, strict),
};
}
else {
bodyItem = {
itemType: 'other',
Type: type,
Content: item,
};
}
body.push(bodyItem);
}
}
// Temporary implementation:
if (xml.storyItem) {
for (const item of (0, ensureMethods_1.ensureXMLObjectArray)(xml.storyItem, strict)) {
const bodyItem = {
itemType: 'storyItem',
Type: 'storyItem',
Content: xmlConversion_1.XMLMosItem.fromXML('storyItem', item, strict),
};
body.push(bodyItem);
}
}
return body;
}
//# sourceMappingURL=xmlConversion.js.map