From 0f17664964a72b841fbf98d7a3067a8c861a6836 Mon Sep 17 00:00:00 2001 From: Onja Date: Mon, 9 Oct 2023 19:22:46 +0300 Subject: [PATCH] Rename File.js to file.js and make changes to the parse method to read a file and return a stream --- src/{models/File.js => file.js} | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) rename src/{models/File.js => file.js} (68%) diff --git a/src/models/File.js b/src/file.js similarity index 68% rename from src/models/File.js rename to src/file.js index e518e56..5c10269 100644 --- a/src/models/File.js +++ b/src/file.js @@ -112,6 +112,61 @@ class File extends EventEmitter { }); }); } + + + // create a parse method which read the file and return a stream + async parse(columns) { + const stream = new PassThrough(); + + // check if columns is valid + if (!columns || columns.length) { + return Promise.reject(new Error('Invalid columns')); + } + + // Create a variable to hold csv columns indexes + const indexes = []; + + let i = 0; + fs.createReadStream(this.filepath) + .pipe(csvParser({ separator: ';' })) + .on('headers', (headers) => { + // Emit a parse.start event with the url and filepath + this.emit('parse.start', { url: this.url, filepath: this.filepath, headers }); + }) + .on('data', (data) => { + // Emit a parse.data event with the url, filepath and data + this.emit('parse.data', { url: this.url, filepath: this.filepath, data }); + + if ( i === 0 ) { + // Loop through columns and push the index of the column to indexes + columns.forEach((column) => { + indexes.push(headers.indexOf(column)); + }); + } else { + // Loop through indexes and push the value of the column to result + const result = this.processRow(data, columns, indexes); + stream.write(result.join(';')); + } + i++; + }) + .on('error', (err) => { + // Emit a parse.error event with the error + this.emit('parse.error', { url: this.url, filepath: this.filepath, error: err.message }); + }) + .on('end', () => { + // Emit a parse.end event with the url and filepath + this.emit('parse.end', { url: this.url, filepath: this.filepath }); + stream.end(); + }); + + return stream; + } + + processRow(data, columns, columnsIndex) { + const result = []; + + return result; + } } module.exports = File; \ No newline at end of file