convert-csv-to-json
Advanced tools
Comparing version 0.0.10 to 0.0.11
25
index.js
@@ -6,6 +6,3 @@ 'use strict'; | ||
/** | ||
* Parse .csv file and put its content into a file in json format. | ||
* @param {inputFileName} path/filename | ||
* @param {outputFileName} path/filename | ||
* | ||
* Prints a digit as Number type (for example 32 instead of '32') | ||
*/ | ||
@@ -17,2 +14,16 @@ exports.formatValueByType = function () { | ||
/** | ||
* Defines the field delimiter which will be used to split the fields | ||
*/ | ||
exports.fieldDelimiter = function (delimiter) { | ||
csvToJson.fieldDelimiter(delimiter); | ||
return this; | ||
}; | ||
/** | ||
* Parses .csv file and put its content into a file in json format. | ||
* @param {inputFileName} path/filename | ||
* @param {outputFileName} path/filename | ||
* | ||
*/ | ||
exports.generateJsonFileFromCsv = function (inputFileName, outputFileName) { | ||
@@ -23,3 +34,3 @@ if(!inputFileName){ | ||
if(!outputFileName){ | ||
throw new Error('outpuFileName is not defined!!!'); | ||
throw new Error('outputFileName is not defined!!!'); | ||
} | ||
@@ -30,3 +41,3 @@ csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
/** | ||
* Parse .csv file and put its content into an Array of Object in json format. | ||
* Parses .csv file and put its content into an Array of Object in json format. | ||
* @param {inputFileName} path/filename | ||
@@ -44,3 +55,3 @@ * @return {Array} Array of Object in json format | ||
/** | ||
* Parse .csv file and put its content into a file in json format. | ||
* Parses .csv file and put its content into a file in json format. | ||
* @param {inputFileName} path/filename | ||
@@ -47,0 +58,0 @@ * @param {outputFileName} path/filename |
{ | ||
"name": "convert-csv-to-json", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Convert CSV to JSON", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# CSVtoJSON | ||
[![Build Status](https://travis-ci.org/iuccio/csvToJson.svg?branch=master)](https://travis-ci.org/iuccio/csvToJson) [![Code Climate](https://codeclimate.com/github/iuccio/csvToJson/badges/gpa.svg)](https://codeclimate.com/github/iuccio/csvToJson) | ||
Convert *csv* file to *JSON* file with Node.js. | ||
**This project is not dependent on others packages or libraries.** | ||
The cvs file must be **semicolon (;) separated**. | ||
## Table of Contents | ||
1. [Description](#description) | ||
1. [Prerequisites](#prerequisites) | ||
1. [Install npm *convert-csv-to-json package*](#install-npm-convert-csv-to-json-package) | ||
1. [Usage](#usage) | ||
* [Generate JSON file](#generate-json-file) | ||
* [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format) | ||
* [Define field delimiter](#define-field-delimiter) | ||
* [Format property value by type](#format-property-value-by-type) | ||
1. [License](#license) | ||
Give an input file like: | ||
## Description | ||
Converts *csv* files to *JSON* files with Node.js. | ||
Given an input file like: | ||
|first_name|last_name|email|gender|age| | ||
|----------|:-------:|:---:|:----:|:-:| | ||
|:----------:|:-------:|:---:|:----:|:---:| | ||
|Constantin|Langsdon|clangsdon0@hc360.com|Male|96| | ||
@@ -49,3 +60,3 @@ |Norah|Raison|nraison1@wired.com|Female|32| | ||
## npm | ||
## Install npm *convert-csv-to-json package* | ||
Go to NPM package [convert-csv-to-json](https://www.npmjs.com/package/convert-csv-to-json). | ||
@@ -82,6 +93,16 @@ | ||
``` | ||
#### Define field delimiter | ||
As default the filed delimiter is the **semicolon** (**;**). You can define another field delimiter | ||
by call the function ```fieldDelimiter(myDilimiter)```. | ||
If you want that the field delimiter is a **~**: | ||
```js | ||
csvToJson.fieldDelimiter('~') .getJsonFromCsv(fileInputName); | ||
``` | ||
#### Format property value by type | ||
If you want that a number will be printed as a Number type and not as a String type, use: | ||
```js | ||
csvToJson.formatValueByType().getJsonFromCsv(fileInputName) | ||
csvToJson.formatValueByType().getJsonFromCsv(fileInputName); | ||
``` | ||
@@ -88,0 +109,0 @@ In this case the result will be: |
@@ -7,4 +7,4 @@ 'use strict'; | ||
const fieldDelimiter = ';'; | ||
const newLine = '\n'; | ||
const defaultFieldDelimiter = ';'; | ||
@@ -18,2 +18,7 @@ class CsvToJson { | ||
fieldDelimiter(delimieter){ | ||
this.delimiter = delimieter; | ||
return this; | ||
} | ||
generateJsonFileFromCsv(fileInputName, fileOutputName) { | ||
@@ -38,2 +43,3 @@ let jsonStringified = this.getJsonFromCsvStringified(fileInputName); | ||
let lines = parsedCsv.split(newLine); | ||
let fieldDelimiter = this.getFieldDelimiter(); | ||
let headers = lines[0].split(fieldDelimiter); | ||
@@ -43,3 +49,3 @@ | ||
for (let i = 1; i < lines.length; i++) { | ||
let currentLine = lines[i].replace(/""/g, '').replace(/"/g, '').split(fieldDelimiter); | ||
let currentLine = lines[i].split(fieldDelimiter); | ||
if (stringUtils.hasContent(currentLine)) { | ||
@@ -52,2 +58,9 @@ jsonResult.push(this.buildJsonResult(headers, currentLine)); | ||
getFieldDelimiter(){ | ||
if(this.delimiter){ | ||
return this.delimiter; | ||
} | ||
return defaultFieldDelimiter; | ||
} | ||
buildJsonResult(headers, currentLine) { | ||
@@ -54,0 +67,0 @@ let jsonObject = {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131638
175
139