convert-csv-to-json
Advanced tools
Comparing version
@@ -39,2 +39,10 @@ "use strict"; | ||
/** | ||
* If active the content of the Header Fields is trimmed including the white spaces, e.g. "My Name" -> "MyName" | ||
*/ | ||
exports.trimHeaderFieldWhiteSpace = function (active = false) { | ||
csvToJson.trimHeaderFieldWhiteSpace(active); | ||
return this; | ||
}; | ||
/** | ||
* Defines the index where the header is defined | ||
@@ -41,0 +49,0 @@ */ |
{ | ||
"name": "convert-csv-to-json", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Convert CSV to JSON", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,2 +35,4 @@ # CSVtoJSON | ||
+ [Define field delimiter](#define-field-delimiter) | ||
+ [Trim header field](#trim-header-field) | ||
+ [Trim header field with whitespaces](#trim-header-field-with-whitespaces) | ||
+ [Support Quoted Fields](#support-quoted-fields) | ||
@@ -172,2 +174,15 @@ + [Index header](#index-header) | ||
#### Trim header field | ||
The content of the field header is cut off at the beginning and end of the string. E.g. " Last Name " -> "Last Name". | ||
#### Trim header field with whitespaces | ||
Use the method *trimHeaderFieldWhiteSpace(true)* to remove the whitespaces in an header field (E.g. " Last Name " -> "LastName"): | ||
```js | ||
csvToJson.rimHeaderFieldWhiteSpace(true) | ||
.getJsonFromCsv(fileInputName); | ||
``` | ||
#### Support Quoted Fields | ||
@@ -174,0 +189,0 @@ To be able to parse correctly fields wrapped in quote, like the **last_name** in the first row in the following example: |
@@ -27,2 +27,7 @@ "use strict"; | ||
trimHeaderFieldWhiteSpace(active) { | ||
this.isTrimHeaderFieldWhiteSpace = active; | ||
return this; | ||
} | ||
indexHeader(indexHeader) { | ||
@@ -119,3 +124,3 @@ if(isNaN(indexHeader)){ | ||
for (let j = 0; j < headers.length; j++) { | ||
let propertyName = stringUtils.trimPropertyName(headers[j]); | ||
let propertyName = stringUtils.trimPropertyName(this.isTrimHeaderFieldWhiteSpace, headers[j]); | ||
let value = currentLine[j]; | ||
@@ -122,0 +127,0 @@ |
@@ -5,4 +5,8 @@ 'use strict'; | ||
trimPropertyName(value) { | ||
return value.replace(/\s/g, ''); | ||
trimPropertyName(isTrimHeaderFieldWhiteSpace,value) { | ||
if(isTrimHeaderFieldWhiteSpace) { | ||
return value.replace(/\s/g, ''); | ||
} | ||
return value.trim(); | ||
} | ||
@@ -9,0 +13,0 @@ |
50363
2.13%539
2.67%355
4.41%