Skip to content

Instantly share code, notes, and snippets.

@masaeedu
Created December 23, 2016 01:26
Show Gist options
  • Save masaeedu/1d3871b3ec54380e36df88e94cd23be6 to your computer and use it in GitHub Desktop.
Save masaeedu/1d3871b3ec54380e36df88e94cd23be6 to your computer and use it in GitHub Desktop.
Parse csv
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