UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

48 lines (44 loc) 1.4 kB
const { readFileSync } = require('fs') /* Remove the _text attributes and convert value it into array input : "groupId":[{"_text":["customer"]}] output: "groupId":["customer"] */ function _removeTextAttributes(value, parentElement) { const parentKeys = Object.keys(parentElement._parent) const key = parentKeys[parentKeys.length - 1] const _text = parentElement._parent[key] if (_text.length > 0) { _text[_text.length - 1] = value } else { parentElement._parent[key] = value } } // REVISIT: Simplify config // REVISIT: fast-xml-parser /** * Parse a given string containing xml * used to remove the _text object from converted json i.e. from xml-js. * example: converted json has "groupId":[{"_text":["customer"]}] * and after removing textAttributes (using this function): "groupId":["customer"] * @param xml string to parse * @returns parsed json object */ function parseXml(path) { const xmljs = require('xml-js') const xml = readFileSync(path, { encoding: 'utf-8' }) return JSON.parse(xmljs.xml2json(xml, { ignoreComment: true, compact: true, alwaysArray: true, ignoreDeclaration: true, attributesKey: "$", trim: true, nativeType: true, ignoreInstruction: true, ignoreCdata: true, ignoreDoctype: true, textFn: _removeTextAttributes }))?.project?.[0] } module.exports = { parseXml }