Created
December 23, 2016 01:26
-
-
Save masaeedu/1d3871b3ec54380e36df88e94cd23be6 to your computer and use it in GitHub Desktop.
Parse csv
This file contains hidden or 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
function readCSV(path: string) { | |
const lines = fs.readFileSync(path) | |
.toString() | |
.split(/\r?\n/) | |
.map(line => line.split(/\s*,\s*/)); | |
const header = lines[0]; | |
const data = lines.slice(1); | |
return data | |
.map(fields => fields.reduce((o, field, i) => Object.assign(o, { [header[i]]: field }), {})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment