UNPKG

xml2customjs

Version:

Library to custom convert an XML into a Javascript object or JSON

55 lines 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformObject = void 0; const index_1 = require("./index"); /** * This function takes an object with the xmlbuilder2 library format and additional * formatting options as params and returns an object transformed according to * the given set of options * This function will transform all nested objects * @param param * @returns object */ function transformObject(object, options) { let clonedObject = JSON.parse(JSON.stringify(object)); const newObject = {}; Object.keys(clonedObject).forEach((key) => { // Process value to array options.arrayFields.forEach((path) => { const pathArr = path.split(/[/]/); if (pathArr.length && key == pathArr[0]) { clonedObject = index_1.convertNestedProp(clonedObject, pathArr, 'array'); } }); // Process value to object options.objectFields.forEach((path) => { const pathArr = path.split(/[/]/); if (pathArr.length && key == pathArr[0]) { clonedObject = index_1.convertNestedProp(clonedObject, pathArr, 'object'); } }); let value = clonedObject[key]; // Transform nested values value = index_1.isObject(value) ? transformObject(value, options) : Array.isArray(value) ? index_1.transformArray(value, options) : value; // Change key name and format let newKey = key in options.fieldNameMapping ? options.fieldNameMapping[key] : key; switch (options.fieldNameFormat) { case 'camel': newKey = index_1.toCamelCase(newKey); break; case 'snake': newKey = index_1.toSnakeCase(newKey); break; default: break; } newObject[newKey] = value; }); return newObject; } exports.transformObject = transformObject; //# sourceMappingURL=transform-object.js.map