@parametricos/bcf-js
Version:
BCF.js is a BIM Collaboration Format (BCF) reader & parser.
199 lines • 7.64 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Helpers = void 0;
const fast_xml_parser_1 = require("fast-xml-parser");
const SharedHelpers = __importStar(require("../SharedHelpers"));
var Helpers;
(function (Helpers) {
Helpers.XmlParserOptions = SharedHelpers.XmlParserOptions;
Helpers.XmlBuilderOptions = SharedHelpers.XmlBuilderOptions;
Helpers.GetViewpoint = SharedHelpers.GetViewpoint;
Helpers.XmlToJsonNotation = SharedHelpers.XmlToJsonNotation;
const ChangeToUppercase = SharedHelpers.ChangeToUppercase;
function GetMarkup(xmlString) {
const { Markup } = new fast_xml_parser_1.XMLParser(Helpers.XmlParserOptions).parse(xmlString);
return {
header: {
files: getHeaderFiles(Markup.Header)
},
topic: {
guid: Markup.Topic['@_Guid'],
topic_type: Markup.Topic["@_TopicType"],
topic_status: Markup.Topic["@_TopicStatus"],
reference_links: Markup.Topic["ReferenceLink"] && Helpers.ObjectToArray(Markup.Topic["ReferenceLink"]),
title: Markup.Topic["Title"],
priority: Markup.Topic["Priority"],
index: Markup.Topic["Index"],
labels: Markup.Topic["Labels"] && Helpers.ObjectToArray(Markup.Topic["Labels"]),
creation_date: Markup.Topic["CreationDate"],
creation_author: Markup.Topic["CreationAuthor"],
modified_date: Markup.Topic["ModifiedDate"],
modified_author: Markup.Topic["ModifiedAuthor"],
assigned_to: Markup.Topic["AssignedTo"],
description: Markup.Topic["Description"],
// bim_snippets: Markup.ITopic["BimSnippet"] ? ,
// related_topics: Markup.ITopic["ReferenceLink"],
comments: Helpers.GetComments(Markup.Comment),
viewpoints: Helpers.GetViewpoints(Markup.Viewpoints)
},
};
}
Helpers.GetMarkup = GetMarkup;
function GetViewpoints(data) {
if (!data)
return;
const constructViewpoint = (data) => {
return {
guid: data["@_Guid"],
viewpoint: data["Viewpoint"],
snapshot: data["Snapshot"]
};
};
const viewpoints = [];
if (Array.isArray(data)) {
data.forEach((x) => {
viewpoints.push(constructViewpoint(x));
});
}
else {
viewpoints.push(constructViewpoint(data));
}
return viewpoints;
}
Helpers.GetViewpoints = GetViewpoints;
function GetComments(data) {
if (!data)
return;
const constructComment = (data) => {
return {
guid: data["@_Guid"],
date: data["Date"],
author: data["Author"],
comment: data["Comment"],
viewpoint: data?.Viewpoint?.["@_Guid"],
modified_date: data["ModifiedDate"],
modified_author: data["ModifiedAuthor"]
};
};
const viewpoints = [];
if (Array.isArray(data)) {
data.forEach((x) => {
viewpoints.push(constructComment(x));
});
}
else {
viewpoints.push(constructComment(data));
}
return viewpoints;
}
Helpers.GetComments = GetComments;
/**
* Returns an object as an array
* Can also accept array and returns new array if type is unknown
*
* @return data as an array
* @param data
*/
function ObjectToArray(data) {
return Array.isArray(data) ? data : [data];
}
Helpers.ObjectToArray = ObjectToArray;
function ParsePoint(point) {
return {
x: point.X,
y: point.Y,
z: point.Z
};
}
Helpers.ParsePoint = ParsePoint;
const version21PluralWordsToSingular = [
"DocumentReferences",
"RelatedTopics"
];
function MarkupToXmlNotation(markup) {
const convertedMarkup = convert21To30(markup);
let purgedMarkup = {
"Markup": {
"@_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"header": convertedMarkup.header,
"topic": convertedMarkup.topic,
"comment": convertedMarkup.comment,
"viewpoints": convertedMarkup.viewpoints
}
};
return RenameJsonKeys(purgedMarkup);
}
Helpers.MarkupToXmlNotation = MarkupToXmlNotation;
function RenameJsonKeys(obj, options) {
let outputObj = {};
if (typeof obj === 'string')
return obj;
const opt_plural_to_singular = options?.plural_to_singular !== undefined ? options.plural_to_singular : true;
for (const key in obj) {
let value = obj[key];
if (!value)
continue;
if (key.startsWith('@_')) {
outputObj[key] = value;
continue;
}
let newKey = ChangeToUppercase(key, options);
if (Array.isArray(value)) {
const newArrNode = [];
for (const child of value)
newArrNode.push(RenameJsonKeys(child, options));
const pluralWord = version21PluralWordsToSingular.find(word => word.startsWith(newKey));
if (pluralWord && opt_plural_to_singular)
newKey = pluralWord.slice(0, -1);
outputObj[newKey] = newArrNode;
continue;
}
if (typeof value === 'object')
value = RenameJsonKeys(value, options);
outputObj[newKey] = value;
}
return outputObj;
}
Helpers.RenameJsonKeys = RenameJsonKeys;
function getHeaderFiles(header) {
if (header)
return Helpers.ObjectToArray(header).map((file) => Helpers.XmlToJsonNotation(file));
}
function convert21To30(markup) {
if (!markup.topic)
return;
const { comments, viewpoints, ...topic } = markup.topic;
const newMarkup = {
header: { file: markup.header?.files },
topic: topic,
comment: comments,
viewpoints: viewpoints
};
return newMarkup;
}
})(Helpers || (exports.Helpers = Helpers = {}));
//# sourceMappingURL=Helpers.js.map