npm package: @novigi/csv-to-json
Description: Simple library for csv parse to Javascript object from any CSV string or csv file 🚀
Author: Novigi
Latest version: 1.0.0-2

![npm (scoped)](https://img.shields.io/npm/v/@novigi/csv-to-json) ![NPM](https://img.shields.io/npm/l/@novigi/csv-to-json) ![Statements](https://img.shields.io/badge/statements-100%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-100%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-100%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-100%25-brightgreen.svg?style=flat)
# @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
```

2. Import the library
```
const lib = require('@novigi/csv-to-json');
```

## 📖 Documentation

<a name="module_csv-to-json"></a>

## 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 🚀```js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](#module_csv-to-json)
    * [~Csv](#module_csv-to-json..Csv)
        * [.delimiter](#module_csv-to-json..Csv+delimiter) ⇒ <code>Csv</code>
        * [.lineBreak](#module_csv-to-json..Csv+lineBreak) ⇒ <code>Csv</code>
        * [.noHeaders](#module_csv-to-json..Csv+noHeaders) ⇒ <code>Csv</code>
        * [.headers](#module_csv-to-json..Csv+headers) ⇒ <code>Csv</code>
        * [.parseFile](#module_csv-to-json..Csv+parseFile) ⇒ <code>Csv</code>
        * [.parseData](#module_csv-to-json..Csv+parseData) ⇒ <code>Csv</code>
    * [~csv([options])](#module_csv-to-json..csv) ⇒ <code>Csv</code>

<a name="module_csv-to-json..Csv"></a>

### csv-to-json~Csv
**Kind**: inner class of [<code>csv-to-json</code>](#module_csv-to-json)  

* [~Csv](#module_csv-to-json..Csv)
    * [.delimiter](#module_csv-to-json..Csv+delimiter) ⇒ <code>Csv</code>
    * [.lineBreak](#module_csv-to-json..Csv+lineBreak) ⇒ <code>Csv</code>
    * [.noHeaders](#module_csv-to-json..Csv+noHeaders) ⇒ <code>Csv</code>
    * [.headers](#module_csv-to-json..Csv+headers) ⇒ <code>Csv</code>
    * [.parseFile](#module_csv-to-json..Csv+parseFile) ⇒ <code>Csv</code>
    * [.parseData](#module_csv-to-json..Csv+parseData) ⇒ <code>Csv</code>

<a name="module_csv-to-json..Csv+delimiter"></a>

#### csv.delimiter ⇒ <code>Csv</code>
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 [<code>Csv</code>](#module_csv-to-json..Csv)  
**Returns**: <code>Csv</code> - new instance of `Csv` object with the same options  

| Param | Type | Description |
| --- | --- | --- |
| delimiter | <code>string</code> | The delimiter as a parameter to separate the values in the csv |

**Example**  
```js
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' }]
```
<a name="module_csv-to-json..Csv+lineBreak"></a>

#### csv.lineBreak ⇒ <code>Csv</code>
Line-break is for for indicating the ending of a row in the csv content.

**Kind**: instance property of [<code>Csv</code>](#module_csv-to-json..Csv)  
**Returns**: <code>Csv</code> - new instance of `Csv` object with the same options  

| Param | Type | Description |
| --- | --- | --- |
| lineBreak | <code>string</code> | The lineBreak as a parameter to separate the lines in the csv |

**Example**  
```js
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' }]
```
<a name="module_csv-to-json..Csv+noHeaders"></a>

#### csv.noHeaders ⇒ <code>Csv</code>
This method is to indicate whether csv content has the header data or not.

**Kind**: instance property of [<code>Csv</code>](#module_csv-to-json..Csv)  
**Returns**: <code>Csv</code> - new instance of `Csv` object with the same options  

| Param | Type | Description |
| --- | --- | --- |
| noHeaders | <code>\*</code> | Indicate whether csv content has headers. Undefined parameter is considered as `true` |

**Example**  
```js
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
```
<a name="module_csv-to-json..Csv+headers"></a>

#### csv.headers ⇒ <code>Csv</code>
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 [<code>Csv</code>](#module_csv-to-json..Csv)  
**Returns**: <code>Csv</code> - new instance of `Csv` object with the same options  

| Param | Type | Description |
| --- | --- | --- |
| headers | <code>array</code> | Custom header array to be used as headers of the csv content |

**Example**  
```js
const csvString = 'kasun,33,male\nsteve,35,male'const customHeader = ['user_name', 'user_age', 'user_gender']const result = csv().headers(customHeader).parseData(csvString)
```
<a name="module_csv-to-json..Csv+parseFile"></a>

#### csv.parseFile ⇒ <code>Csv</code>
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 [<code>Csv</code>](#module_csv-to-json..Csv)  
**Returns**: <code>Csv</code> - new instance of `Csv` object with the same options  

| Param | Type | Description |
| --- | --- | --- |
| csvFile | <code>string</code> | valid csv file path , the path passes as a parameter and read the file in that file is exist |

**Example**  
```js
const path = './resources/csvFile.csv'           // csv file directory pathcsv().parseFile(csvFile)                           // [{ name: 'kasun', age: '33', sex: 'male' }, { name: 'steve', age: '35', sex: 'male' }]
```
<a name="module_csv-to-json..Csv+parseData"></a>

#### csv.parseData ⇒ <code>Csv</code>
CSV parse to javascript data structure,'parse' method convert the csv string to the javascript array object.

**Kind**: instance property of [<code>Csv</code>](#module_csv-to-json..Csv)  
**Returns**: <code>Csv</code> - new instance of `Csv` object with the same options  

| Param | Type | Description |
| --- | --- | --- |
| csvString | <code>string</code> | valid string to passes as a parameter |

**Example**  
```js
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' }]
```
<a name="module_csv-to-json..csv"></a>

### csv-to-json~csv([options]) ⇒ <code>Csv</code>
Create an Csv chain builder instance

**Kind**: inner method of [<code>csv-to-json</code>](#module_csv-to-json)  
**Returns**: <code>Csv</code> - new instance of 'Csv' object  

| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>object</code> | optional configurations for the chain |
| [options.delimiter] | <code>string</code> | delimiter which csv fields are separated |
| [options.lineBreak] | <code>string</code> | line break character of csv rows |
| [options.noHeaders] | <code>boolean</code> | falg to indicate csv content has a header row or not |
| [options.headers] | <code>object</code> | custom headers for parsing csv content |

**Example**  
```js
csv()csv(options)
```

_This is an auto generated file. Please don't make changes manually_