Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Author: Gerardo Junior <me@gerardo-junior.com
// Date: 10/14/2019, 8:53:55 PM
// URL: https://gist.github.com/gerardo-junior/2dfda6b7557e1334d1ef2ba495c4ad7c/
function csvParser(filename) {
const fs = require('fs')
, path = require('path')
, filePath = path.join(__dirname, filename);
return new Promise((resolve, reject) => {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, file) {
if (!err) {
fileValues = file.split('\n')
fileKeys = fileValues[0].split(',')
fileValues.shift();
parser = []
fileValues.forEach(element => {
parserCusor = {}
element.split(',').forEach((element, index) => {
parserCusor[fileKeys[index]] = element.trim()
});
parser.push(parserCusor)
});
resolve(parser)
} else {
reject(err)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment