Browse Source

Refactor the processRow method to generate a new file with the columns and return the exported row

node16
Onja 12 months ago
parent
commit
ca33b38dc9
  1. 94
      src/models/file.js

94
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;
}
}

Loading…
Cancel
Save