UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

116 lines (115 loc) 5.59 kB
"use strict"; 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fast_xml_parser_1 = require("fast-xml-parser"); const Affirm_1 = __importDefault(require("../../core/Affirm")); const Algo_1 = __importDefault(require("../../core/Algo")); const fs = __importStar(require("fs")); const DEFAULT_OPTIONS = { attributeNamePrefix: '@_', ignoreAttributes: false, parseAttributeValue: true, parseTagValue: true, trimValues: true, isArray: () => { return false; } }; class XMLParserClass { constructor(options) { this.xmlToJson = (xmlData) => { (0, Affirm_1.default)(xmlData, 'XML data cannot be empty'); try { const parsedData = this._parser.parse(xmlData); if (typeof parsedData === 'object' && parsedData !== null) { const rootKeys = Object.keys(parsedData); if (rootKeys.length === 1) { const potentialArray = parsedData[rootKeys[0]]; if (Array.isArray(potentialArray)) { return potentialArray; } if (typeof potentialArray === 'object' && potentialArray !== null) { const innerKeys = Object.keys(potentialArray); if (innerKeys.length === 1 && Array.isArray(potentialArray[innerKeys[0]])) { return potentialArray[innerKeys[0]]; } if (Array.isArray(potentialArray)) { return potentialArray; } const values = Object.values(potentialArray).filter(Array.isArray); if (values.length === 1 && Array.isArray(values[0])) { return values[0]; } return [potentialArray]; } return [potentialArray]; } } return parsedData; } catch (error) { console.error('Error parsing XML:', error); throw new Error('Failed to parse XML data.'); } }; // TO DO: improve performances for larger file using streams instead of an array of string this.readXmlLines = (fileUri, lineFrom, lineTo) => __awaiter(this, void 0, void 0, function* () { const fileContent = fs.readFileSync(fileUri, 'utf-8'); const jsonData = XMLParser.xmlToJson(fileContent); // Convert JSON data to string lines. This might need adjustment based on XML structure. // Assuming jsonData is an array of objects, where each object is a record. let lines = Array.isArray(jsonData) ? jsonData.map(item => JSON.stringify(item)) : [JSON.stringify(jsonData)]; if (Algo_1.default.hasVal(lineFrom) && Algo_1.default.hasVal(lineTo)) { lines = lines.slice(lineFrom, lineTo + 1); } return lines; }); this._parser = new fast_xml_parser_1.XMLParser(Object.assign(Object.assign({}, DEFAULT_OPTIONS), options)); // Use 'as any' if type issues persist with library } } const XMLParser = new XMLParserClass(); exports.default = XMLParser;