feat: adding converter
This commit is contained in:
33
utils/converter.js
Normal file
33
utils/converter.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require('fs')
|
||||
|
||||
const csvToJson = (csv) => {
|
||||
const lines = csv.split('\n')
|
||||
const result = [];
|
||||
const headers = lines[0].split(',');
|
||||
const contents = lines.slice(1, lines.length - 1)
|
||||
const obj = {};
|
||||
for (const content of contents) {
|
||||
const currentline = content.split(',');
|
||||
for (let j = 0; j < headers.length; j++) {
|
||||
obj[headers[j]] = currentline[j];
|
||||
}
|
||||
result.push(obj);
|
||||
}
|
||||
return JSON.parse(JSON.stringify(result)); //JSON
|
||||
}
|
||||
|
||||
exports.readFile = async (pathFile) => {
|
||||
const result = new Promise((resolve, reject) => {
|
||||
fs.readFile(`${pathFile}`, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
const result = csvToJson(data)
|
||||
resolve(result)
|
||||
});
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user