Simply parse a .csv file into a javascript object with or without designated headers.
npm install simply-parse-csvcsv
"id","name","birthday"
1,"Jeff","1983-04-21"
2,"Micah","1995-11-05"
`
Output:
`javascript
[
{
id: 1,
name: "Jeff",
birthday: "1983-04-21"
},
{
id: 2,
name: "Micah",
birthday: "1995-11-05"
}
]
`
$3
In its simplest and intended form, you can run the parseCSV function seen below using the default options (see the Available Options section) by simply passing a standard .csv string with a header row: `parseCSV(csvData);`.
What follows are examples of slightly more involved use cases.
#### Node.js ES6 Example:
`javascript
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const parseCsv = require('simply-parse-csv');
async function main () {
const csvPath = './path/to/file.csv';
let csvData = await readFile(csvPath, 'utf8'); // Get csv string from file
let options = { // Set options
trim = true
}
let parsed = await parseCsv(csvData, options);
console.log(util.inspect(parsed));
}
main();
`
#### Node.js ES6 Example with defined Headers:
`javascript
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const parseCsv = require('simply-parse-csv');
async function main () {
const csvPath = './path/to/file.csv';
let csvData = await readFile(csvPath, 'utf8'); // Get csv string from file
const headers = ['id', 'name', 'birthday'];
let options = { // Set options
trim = true,
containsHeaders: false,
headers: headers
}
let parsed = await parseCsv(csvData, options);
console.log(util.inspect(parsed));
}
main();
`
#### Node.js Promise.then Example:
`javascript
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const parseCsv = require('simply-parse-csv');
async function main () {
const csvPath = './path/to/file.csv';
readFile(csvPath, 'utf8') // Read in CSV string
.then(csvData => {
let options = { // Set Options
trim = true
}
return parseCsv(csvData, options); // Pass CSV string and options into parseCsv Function
}).then(parsed => {
console.log(util.inspect(parsed));
/ Do something /
})
}
main();
`
$3
* `containsHeaders`:
* Accepts: boolean
* Default: `true`
* If `true`, read the headers contained in the first row of the csv file.
* If `false`, either use headers supplied in the `headers` option or use default headers; `column1, column2, ...`
* `headers`:
* Accepts: array of strings
* Default: `null`
* If defined, then associate columns to this header array, in order
* `trim`:
* Accepts: boolean
* Default: `false`
* Choose whether to use the str.trim() function on the csv data before returning.
* `parseAllAsString`
* Accepts: boolean
* Default: `false`
* Choose whether to return everything in the CSV as a string.
$3
* This module automatically parses datatypes; `"1.0"` remains a string, `1.0` becomes `1` as an integer, `1.2` becomes `1.2` as a floating point (unless `options.parseAllAsString === true``).