1.2.2 • Published 5 years ago

simply-parse-csv v1.2.2

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

Simply Parse CSV

NPM

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 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.

Data Types and Quoted Strings

  • 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).
  • 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.
1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago