Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

convert-csv-to-json

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

convert-csv-to-json - npm Package Compare versions

Comparing version 1.5.0 to 2.0.0

.idea/codeStyles/codeStyleConfig.xml

7

index.js

@@ -24,2 +24,9 @@ "use strict";

/**
*
*/
exports.supportQuotedField = function (active = false) {
csvToJson.supportQuotedField(active);
return this;
};
/**
* Defines the field delimiter which will be used to split the fields

@@ -26,0 +33,0 @@ */

4

package.json
{
"name": "convert-csv-to-json",
"version": "1.5.0",
"version": "2.0.0",
"description": "Convert CSV to JSON",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec",
"test": "./node_modules/.bin/mocha --parallel --reporter spec",
"test-watch": "./node_modules/.bin/mocha -w"

@@ -9,0 +9,0 @@ },

@@ -26,2 +26,3 @@ # CSVtoJSON

+ [Define field delimiter](#define-field-delimiter)
+ [Support Quoted Fields](#support-quoted-fields)
+ [Index header](#index-header)

@@ -154,2 +155,26 @@ + [Empty rows](#empty-rows)

#### Support Quoted Fields
To be able to parse correctly fields wrapped in quote, like the **last_name** in the first row in the following example:
|first_name| last_name |email|
|:----------:|:--------------------------:|:---:|
|Constantin| "Langsdon,Nandson,Gangson" |clangsdon0@hc360.com|
you need to activate the support quoted fields feature:
```js
csvToJson.supportQuotedField(true).getJsonFromCsv(fileInputName);
```
The result will be:
```json
[
{
"firstName": "Constantin",
"lastName": "Langsdon,Nandson,Gangson",
"email": "clangsdon0@hc360.com"
}
]
```
#### Index header

@@ -156,0 +181,0 @@ If the header is not on the first line you can define the header index like:

@@ -17,2 +17,7 @@ "use strict";

supportQuotedField(active) {
this.isSupportQuotedField = active;
return this;
}
fieldDelimiter(delimiter) {

@@ -64,2 +69,3 @@ this.delimiter = delimiter;

csvToJson(parsedCsv) {
this.validateInputConfig();
let lines = parsedCsv.split(newLine);

@@ -77,7 +83,13 @@ let fieldDelimiter = this.getFieldDelimiter();

for (let i = (index + 1); i < lines.length; i++) {
let currentLine = lines[i].split(fieldDelimiter);
if (stringUtils.hasContent(currentLine)) {
jsonResult.push(this.buildJsonResult(headers, currentLine));
}
}
let currentLine;
if(this.isSupportQuotedField){
currentLine = this.split(lines[i]);
}
else{
currentLine = lines[i].split(fieldDelimiter);
}
if (stringUtils.hasContent(currentLine)) {
jsonResult.push(this.buildJsonResult(headers, currentLine));
}
}
return jsonResult;

@@ -143,4 +155,74 @@ }

validateInputConfig(){
if(this.isSupportQuotedField) {
if(this.getFieldDelimiter() === '"'){
throw new Error('When SupportQuotedFields is enabled you cannot defined the field delimiter as quote -> ["]');
}
if(this.parseSubArraySeparator === '"'){
throw new Error('When SupportQuotedFields is enabled you cannot defined the field parseSubArraySeparator as quote -> ["]');
}
if(this.parseSubArrayDelimiter === '"'){
throw new Error('When SupportQuotedFields is enabled you cannot defined the field parseSubArrayDelimiter as quote -> ["]');
}
}
}
hasQuotes(line) {
return line.includes('"');
}
split(line) {
if(line.length == 0){
return [];
}
let delim = this.getFieldDelimiter();
let subSplits = [''];
if (this.hasQuotes(line)) {
let chars = line.split('');
let subIndex = 0;
let startQuote = false;
let isDouble = false;
chars.forEach((c, i, arr) => {
if (isDouble) { //when run into double just pop it into current and move on
subSplits[subIndex] += c;
isDouble = false;
return;
}
if (c != '"' && c != delim ) {
subSplits[subIndex] += c;
} else if(c == delim && startQuote){
subSplits[subIndex] += c;
} else if( c == delim ){
subIndex++
subSplits[subIndex] = '';
return;
} else {
if (arr[i + 1] === '"') {
//Double quote
isDouble = true;
//subSplits[subIndex] += c; //Skip because this is escaped quote
} else {
if (!startQuote) {
startQuote = true;
//subSplits[subIndex] += c; //Skip because we don't want quotes wrapping value
} else {
//end
startQuote = false;
//subSplits[subIndex] += c; //Skip because we don't want quotes wrapping value
}
}
}
});
if(startQuote){
throw new Error('Row contains mismatched quotes!');
}
return subSplits;
} else {
return line.split(delim);
}
}
}
module.exports = new CsvToJson();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc