convert-csv-to-json 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,6 +32,8 @@ show your :heart: and support.
32
32
  + [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format)
33
33
  + [Generate Object with sub array](#generate-object-with-sub-array)
34
34
  + [Define field delimiter](#define-field-delimiter)
35
+ + [Trim header field](#trim-header-field)
36
+ + [Trim header field with whitespaces](#trim-header-field-with-whitespaces)
35
37
  + [Support Quoted Fields](#support-quoted-fields)
36
38
  + [Index header](#index-header)
37
39
  + [Empty rows](#empty-rows)
@@ -168,6 +170,19 @@ E.g. if your field delimiter is the comma **,** then:
168
170
  .getJsonFromCsv(fileInputName);
169
171
  ```
170
172
 
173
+ #### Trim header field
174
+
175
+ The content of the field header is cut off at the beginning and end of the string. E.g. " Last Name " -> "Last Name".
176
+
177
+ #### Trim header field with whitespaces
178
+
179
+ Use the method *trimHeaderFieldWhiteSpace(true)* to remove the whitespaces in an header field (E.g. " Last Name " -> "LastName"):
180
+
181
+ ```js
182
+ csvToJson.rimHeaderFieldWhiteSpace(true)
183
+ .getJsonFromCsv(fileInputName);
184
+ ```
185
+
171
186
  #### Support Quoted Fields
172
187
  To be able to parse correctly fields wrapped in quote, like the **last_name** in the first row in the following example:
173
188
 
package/index.js CHANGED
@@ -35,6 +35,14 @@ exports.fieldDelimiter = function (delimiter) {
35
35
  return this;
36
36
  };
37
37
 
38
+ /**
39
+ * If active the content of the Header Fields is trimmed including the white spaces, e.g. "My Name" -> "MyName"
40
+ */
41
+ exports.trimHeaderFieldWhiteSpace = function (active = false) {
42
+ csvToJson.trimHeaderFieldWhiteSpace(active);
43
+ return this;
44
+ };
45
+
38
46
  /**
39
47
  * Defines the index where the header is defined
40
48
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convert-csv-to-json",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Convert CSV to JSON",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/csvToJson.js CHANGED
@@ -24,6 +24,11 @@ class CsvToJson {
24
24
  return this;
25
25
  }
26
26
 
27
+ trimHeaderFieldWhiteSpace(active) {
28
+ this.isTrimHeaderFieldWhiteSpace = active;
29
+ return this;
30
+ }
31
+
27
32
  indexHeader(indexHeader) {
28
33
  if(isNaN(indexHeader)){
29
34
  throw new Error('The index Header must be a Number!');
@@ -115,7 +120,7 @@ class CsvToJson {
115
120
  buildJsonResult(headers, currentLine) {
116
121
  let jsonObject = {};
117
122
  for (let j = 0; j < headers.length; j++) {
118
- let propertyName = stringUtils.trimPropertyName(headers[j]);
123
+ let propertyName = stringUtils.trimPropertyName(this.isTrimHeaderFieldWhiteSpace, headers[j]);
119
124
  let value = currentLine[j];
120
125
 
121
126
  if(this.isParseSubArray(value)){
@@ -2,8 +2,12 @@
2
2
 
3
3
  class StringUtils {
4
4
 
5
- trimPropertyName(value) {
6
- return value.replace(/\s/g, '');
5
+ trimPropertyName(isTrimHeaderFieldWhiteSpace,value) {
6
+ if(isTrimHeaderFieldWhiteSpace) {
7
+ return value.replace(/\s/g, '');
8
+ }
9
+ return value.trim();
10
+
7
11
  }
8
12
 
9
13
  getValueFormatByType(value) {