From ca33b38dc982b3266202f9bb6abdea22acd7a110 Mon Sep 17 00:00:00 2001 From: Onja Date: Tue, 10 Oct 2023 15:27:22 +0300 Subject: [PATCH] Refactor the processRow method to generate a new file with the columns and return the exported row --- src/models/file.js | 94 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/src/models/file.js b/src/models/file.js index 5c10269..1eb911e 100644 --- a/src/models/file.js +++ b/src/models/file.js @@ -10,6 +10,7 @@ const EventEmitter = require('events'); const csvParser = require('csv-parser'); const { PassThrough } = require('stream'); const slugify = require('slugify'); +const e = require('express'); const dest = path.join(appRoot, 'public/csv'); @@ -124,7 +125,15 @@ class File extends EventEmitter { } // Create a variable to hold csv columns indexes - const indexes = []; + const columnsIndex = {}; + for (let column of columns) { + columnsIndex[column] = { + index: -1, + main: (String(column)).split('.')[0], + value: column, + last: (String(column)).split('.').pop(), + }; + } let i = 0; fs.createReadStream(this.filepath) @@ -133,20 +142,25 @@ class File extends EventEmitter { // Emit a parse.start event with the url and filepath this.emit('parse.start', { url: this.url, filepath: this.filepath, headers }); }) - .on('data', (data) => { + .on('data', (row) => { // Emit a parse.data event with the url, filepath and data - this.emit('parse.data', { url: this.url, filepath: this.filepath, data }); + this.emit('parse.data', { url: this.url, filepath: this.filepath, data: row }); + let result = []; if ( i === 0 ) { - // Loop through columns and push the index of the column to indexes - columns.forEach((column) => { - indexes.push(headers.indexOf(column)); - }); + for(let i = 0; i < row.length; i++) { + for (let key of columnsIndex) { + if (row[i] === columnsIndex[key].main) { + columnsIndex[key].index = i; + result.push(columnsIndex[key].last); + } + } + } } else { // Loop through indexes and push the value of the column to result - const result = this.processRow(data, columns, indexes); - stream.write(result.join(';')); + result = this.processRow(row, columnsIndex); } + stream.write(result.join(';')); i++; }) .on('error', (err) => { @@ -162,9 +176,69 @@ class File extends EventEmitter { return stream; } - processRow(data, columns, columnsIndex) { + /** + * Generate a new file with the columns + * + * @param object obj + * @param string path + * @return string + */ + getValueByPath(obj, path) { + const parts = path.split('.'); + let result = obj; + for (let part of parts) { + // Add a check before accessing the property + if (result[part]) { + result = result[part]; + } else { + return ''; + } + } + return result; + } + + + + /** + * Generate exported row + * + * @param array row + * @param object columnsIndex + * @return array + */ + processRow(row, columnsIndex) { const result = []; + for (let i = 0; i < row.length; i++) { + try { + row[i] = JSON.parse(row[i]); + } catch (err) { + // result.push(''); + console.log(err); + } + } + + const validIndexes = Object.keys(columnsIndex).filter(key => columnsIndex[key].index !== -1); + + for (let i=0; i < row.length; i++) { + if (!validIndexes.includes(i)) { + continue; + } + + const column = columnsIndex[i]; + const item = row[i]; + + if ( column.primary === column.value ) { + result.push(item); + } else { + if ( typeof item === 'object' ) { + result.push(this.getValueByPath(item, column.value)); + } else { + result.push(item); + } + } + } + return result; } }