simply-parse-csv
v1.2.2
Published
Simply parse a .csv file into a javascript object with or without designated headers.
Downloads
56
Maintainers
Readme
Simply Parse CSV
Usage
Simply pass a csv string into simply-parse-csv, and it will return an array of javascript objects containing the data from each row.
Example Input/Output:
Input (with headers):
"id","name","birthday"
1,"Jeff","1983-04-21"
2,"Micah","1995-11-05"
Output:
[
{
id: 1,
name: "Jeff",
birthday: "1983-04-21"
},
{
id: 2,
name: "Micah",
birthday: "1995-11-05"
}
]
Usage Examples
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:
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:
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:
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();
Available Options
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 theheaders
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.
Data Types and Quoted Strings
- This module automatically parses datatypes;
"1.0"
remains a string,1.0
becomes1
as an integer,1.2
becomes1.2
as a floating point (unlessoptions.parseAllAsString === true
). - This module should work just as well for .csv data without quote encapsulated strings as for .csv data with properly quoted (single or double) strings.