Created
October 15, 2019 05:47
-
-
Save gerardo-junior/2dfda6b7557e1334d1ef2ba495c4ad7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Gerardo Junior <[email protected] | |
// 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