1.0.0-2 • Published 1 year ago

@novigi/csv-to-json v1.0.0-2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

npm (scoped) NPM Statements Branches Functions Lines

@novigi/csv-to-json

Simple library for csv parse to Javascript object from any CSV string or csv file 🚀

🐿 Features

  • Chainable and immutable API
  • Parse CSV content into JSON object
  • Parse CSV files into JSON objects
  • Supports custom headers

📦 Getting Started

  1. Install the dependency
npm install @novigi/csv-to-json
  1. Import the library
const lib = require('@novigi/csv-to-json');

📖 Documentation

csv-to-json

This library contains methods that allow user to parse the csv, either it is a csv string or file convert into the Javscript object.

This guideline is about the formats and examples that can follow for csv string parsing to Javascript object conversion 🚀

const csv = require('@novigi/csv-to-json')

const input = 'name,age,sex\nkasun,33,male\nsteve,35,male'
let json = csv().parseData(input)

// or

let json = csv().parseFile(path/to/file)

Chainable + immutable methods! ☝

csv-to-json~Csv

Kind: inner class of csv-to-json

csv.delimiter ⇒ Csv

Delimiter is for different separators that can manually pass with csv file or string, the separator separates the CSV string in the context.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

ParamTypeDescription
delimiterstringThe delimiter as a parameter to separate the values in the csv

Example

const csvString = "name;age;sex\nkasun;33;male\nsteve;35;male"
csv().fieldDelimiter(';').parseData(csvString)   // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv.lineBreak ⇒ Csv

Line-break is for for indicating the ending of a row in the csv content.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

ParamTypeDescription
lineBreakstringThe lineBreak as a parameter to separate the lines in the csv

Example

const csvString = "name,age,sex\r\nkasun,33,male\r\nsteve,35,male"
csv().lineBreak('\r\n').parseData(csvString)   // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv.noHeaders ⇒ Csv

This method is to indicate whether csv content has the header data or not.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

ParamTypeDescription
noHeaders*Indicate whether csv content has headers. Undefined parameter is considered as true

Example

const csvString = 'kasun,33,male\nsteve,35,male'
const customHeader = ['user_name', 'user_age', 'user_gender']
const result = csv().noHeaders().headers(customHeader).parseData(csvString) // custom headers wil be used as the headers of the csv content

csv.headers ⇒ Csv

Adds headers to use as the headers of the csv content. Specifying headers will override the headers in the csv content. When provided header list is insufficient, headers in the csv content are considered

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

ParamTypeDescription
headersarrayCustom header array to be used as headers of the csv content

Example

const csvString = 'kasun,33,male\nsteve,35,male'
const customHeader = ['user_name', 'user_age', 'user_gender']
const result = csv().headers(customHeader).parseData(csvString)

csv.parseFile ⇒ Csv

CSV file read and parse to javascript data structure , 'parseFile' method read and convert the csv file into the javascript array object.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

ParamTypeDescription
csvFilestringvalid csv file path , the path passes as a parameter and read the file in that file is exist

Example

const path = './resources/csvFile.csv'           // csv file directory path
csv().parseFile(csvFile)                           // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv.parseData ⇒ Csv

CSV parse to javascript data structure,'parse' method convert the csv string to the javascript array object.

Kind: instance property of Csv
Returns: Csv - new instance of Csv object with the same options

ParamTypeDescription
csvStringstringvalid string to passes as a parameter

Example

const csvString = "name,age,sex\nkasun,33,male\nsteve,35,male"
csv().parseData(csvString)                              // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]

csv-to-json~csv(options) ⇒ Csv

Create an Csv chain builder instance

Kind: inner method of csv-to-json
Returns: Csv - new instance of 'Csv' object

ParamTypeDescription
optionsobjectoptional configurations for the chain
options.delimiterstringdelimiter which csv fields are separated
options.lineBreakstringline break character of csv rows
options.noHeadersbooleanfalg to indicate csv content has a header row or not
options.headersobjectcustom headers for parsing csv content

Example

csv()
csv(options)

This is an auto generated file. Please don't make changes manually