convert-csv-to-json 3.27.0 → 4.0.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/docs/SYNC.md ADDED
@@ -0,0 +1,239 @@
1
+ # Synchronous API Documentation
2
+
3
+ Transform CSV files into JSON objects synchronously. Perfect for simple use cases and straightforward data processing workflows.
4
+
5
+ ## Table of Contents
6
+ - [Basic Usage](#basic-usage)
7
+ - [Generate JSON File](#generate-json-file)
8
+ - [Generate Array of Objects](#generate-array-of-objects)
9
+ - [Configuration Options](#configuration-options)
10
+ - [Advanced Features](#advanced-features)
11
+ - [TypeScript Support](#typescript-support)
12
+
13
+ ## Basic Usage
14
+
15
+ ```js
16
+ const csvToJson = require('convert-csv-to-json');
17
+
18
+ // Parse CSV file
19
+ const json = csvToJson.getJsonFromCsv('input.csv');
20
+ console.log(json);
21
+ ```
22
+
23
+ ## Generate JSON File
24
+
25
+ ```js
26
+ const csvToJson = require('convert-csv-to-json');
27
+
28
+ csvToJson.generateJsonFileFromCsv('input.csv', 'output.json');
29
+ ```
30
+
31
+ ## Generate Array of Objects
32
+
33
+ ```js
34
+ const csvToJson = require('convert-csv-to-json');
35
+
36
+ const json = csvToJson.getJsonFromCsv('input.csv');
37
+ json.forEach(record => {
38
+ console.log(record);
39
+ });
40
+ ```
41
+
42
+ ## Configuration Options
43
+
44
+ ### Field Delimiter
45
+
46
+ Comma (`,`) is the default delimiter per RFC 4180. Use `fieldDelimiter()` for other delimiters:
47
+
48
+ ```js
49
+ // Semicolon-delimited
50
+ csvToJson.fieldDelimiter(';').getJsonFromCsv('file.csv');
51
+
52
+ // Tab-delimited
53
+ csvToJson.fieldDelimiter('\t').getJsonFromCsv('file.tsv');
54
+
55
+ // Pipe-delimited
56
+ csvToJson.fieldDelimiter('|').getJsonFromCsv('file.psv');
57
+ ```
58
+
59
+ ### Quoted Fields
60
+
61
+ Enable support for fields wrapped in quotes (useful when fields contain delimiters, newlines, or quotes):
62
+
63
+ ```js
64
+ csvToJson
65
+ .supportQuotedField(true)
66
+ .getJsonFromCsv('file.csv');
67
+ ```
68
+
69
+ **Example Input:**
70
+ ```csv
71
+ name,description
72
+ "Smith, John","He said ""Hello"""
73
+ Jane,"Multi-line
74
+ description"
75
+ ```
76
+
77
+ **Output:**
78
+ ```json
79
+ [
80
+ { "name": "Smith, John", "description": "He said \"Hello\"" },
81
+ { "name": "Jane", "description": "Multi-line\ndescription" }
82
+ ]
83
+ ```
84
+
85
+ ### Format Values by Type
86
+
87
+ Automatically convert string values to appropriate types (numbers, booleans):
88
+
89
+ ```js
90
+ csvToJson.formatValueByType().getJsonFromCsv('file.csv');
91
+ ```
92
+
93
+ **Conversion Rules:**
94
+ - `"true"` / `"false"` (case-insensitive) → boolean
95
+ - `"123"` → number
96
+ - `"0012"` → string (preserves leading zeros)
97
+ - Other values → string
98
+
99
+ **Example:**
100
+ ```csv
101
+ name,age,active,id
102
+ John,30,true,0012
103
+ Jane,25,false,987
104
+ ```
105
+
106
+ **Output:**
107
+ ```json
108
+ [
109
+ { "name": "John", "age": 30, "active": true, "id": "0012" },
110
+ { "name": "Jane", "age": 25, "active": false, "id": "987" }
111
+ ]
112
+ ```
113
+
114
+ ### Trim Header Field
115
+
116
+ Remove whitespace from header names:
117
+
118
+ ```js
119
+ // Trim leading/trailing spaces
120
+ csvToJson.trimHeaderField().getJsonFromCsv('file.csv');
121
+
122
+ // Remove all whitespace from headers
123
+ csvToJson.trimHeaderFieldWhiteSpace(true).getJsonFromCsv('file.csv');
124
+ ```
125
+
126
+ **Example:**
127
+ ```csv
128
+ First Name , Last Name
129
+ John,Doe
130
+ ```
131
+
132
+ **With `trimHeaderFieldWhiteSpace(true)`:**
133
+ ```json
134
+ [{ "FirstName": "John", "LastName": "Doe" }]
135
+ ```
136
+
137
+ ### Custom Encoding
138
+
139
+ Read files with specific encoding:
140
+
141
+ ```js
142
+ csvToJson.utf8Encoding().getJsonFromCsv('file.csv'); // UTF-8
143
+ csvToJson.latin1Encoding().getJsonFromCsv('file.csv'); // Latin-1
144
+ csvToJson.ucs2Encoding().getJsonFromCsv('file.csv'); // UCS-2
145
+ csvToJson.utf16leEncoding().getJsonFromCsv('file.csv'); // UTF-16LE
146
+ csvToJson.asciiEncoding().getJsonFromCsv('file.csv'); // ASCII
147
+ csvToJson.base64Encoding().getJsonFromCsv('file.csv'); // Base64
148
+ csvToJson.hexEncoding().getJsonFromCsv('file.csv'); // Hex
149
+ csvToJson.customEncoding('utf8').getJsonFromCsv('file.csv'); // Custom
150
+ ```
151
+
152
+ ### Header Index
153
+
154
+ Specify which row contains headers (default is row 0):
155
+
156
+ ```js
157
+ // Headers in row 2 (3rd row)
158
+ csvToJson.indexHeader(2).getJsonFromCsv('file.csv');
159
+ ```
160
+
161
+ ## Advanced Features
162
+
163
+ ### Parse Sub-Arrays
164
+
165
+ Parse delimited values within fields into arrays:
166
+
167
+ ```js
168
+ csvToJson
169
+ .parseSubArray('*', ',') // Fields wrapped in * contain comma-separated values
170
+ .getJsonFromCsv('file.csv');
171
+ ```
172
+
173
+ **Example Input:**
174
+ ```csv
175
+ name,email,tags
176
+ John,john@example.com,*javascript,nodejs,typescript*
177
+ Jane,jane@example.com,*python,django*
178
+ ```
179
+
180
+ **Output:**
181
+ ```json
182
+ [
183
+ { "name": "John", "email": "john@example.com", "tags": ["javascript", "nodejs", "typescript"] },
184
+ { "name": "Jane", "email": "jane@example.com", "tags": ["python", "django"] }
185
+ ]
186
+ ```
187
+
188
+ ### Work with CSV Strings
189
+
190
+ Parse CSV content directly from strings:
191
+
192
+ ```js
193
+ const csv = 'name,age\nJohn,30\nJane,25';
194
+ const json = csvToJson.csvStringToJson(csv);
195
+ // Output: [{ name: 'John', age: '30' }, { name: 'Jane', age: '25' }]
196
+
197
+ // Get as JSON string
198
+ const jsonString = csvToJson.csvStringToJsonStringified(csv);
199
+ ```
200
+
201
+ ### Method Chaining
202
+
203
+ Combine multiple configuration options:
204
+
205
+ ```js
206
+ const json = csvToJson
207
+ .fieldDelimiter(';')
208
+ .formatValueByType()
209
+ .supportQuotedField(true)
210
+ .trimHeaderFieldWhiteSpace(true)
211
+ .getJsonFromCsv('file.csv');
212
+ ```
213
+
214
+ ## TypeScript Support
215
+
216
+ ```ts
217
+ import csvToJson from 'convert-csv-to-json';
218
+
219
+ interface Person {
220
+ name: string;
221
+ age: number;
222
+ email: string;
223
+ }
224
+
225
+ // Parse with type assertion
226
+ const people = csvToJson
227
+ .formatValueByType()
228
+ .getJsonFromCsv('people.csv') as Person[];
229
+
230
+ // Parse CSV string
231
+ const csv = 'name,age\nAlice,30';
232
+ const parsed = csvToJson.csvStringToJson(csv) as Person[];
233
+ ```
234
+
235
+ ## See Also
236
+
237
+ - [Main README](../README.md) - Overview and installation
238
+ - [Async API](ASYNC.md) - Asynchronous operations with Promises/async-await
239
+ - [Browser API](BROWSER.md) - Client-side CSV parsing
package/index.d.ts CHANGED
@@ -72,6 +72,14 @@ declare module 'convert-csv-to-json' {
72
72
  */
73
73
  hexEncoding(): this;
74
74
 
75
+ /**
76
+ * Sets a mapper function to transform each row after conversion.
77
+ * The mapper function receives (row, index) where row is the JSON object
78
+ * and index is the 0-based row number. Return null/undefined to filter out rows.
79
+ * @param mapperFn Function that receives (row: any, index: number) => any | null
80
+ */
81
+ mapRows(mapperFn: (row: any, index: number) => any | null): this;
82
+
75
83
  /**
76
84
  * Parses .csv file and put its content into a file in json format.
77
85
  * @param {inputFileName} path/filename
@@ -105,14 +113,7 @@ declare module 'convert-csv-to-json' {
105
113
  */
106
114
  csvStringToJsonStringified(csvString: string): string;
107
115
 
108
- /**
109
- * Parses .csv file and put its content into a file in json format.
110
- * @param {inputFileName} path/filename
111
- * @param {outputFileName} path/filename
112
- *
113
- * @deprecated Use generateJsonFileFromCsv()
114
- */
115
- jsonToCsv(inputFileName: string, outputFileName: string): void;
116
+
116
117
  }
117
118
  const converter: ConvertCsvToJson;
118
119
  export default converter;
@@ -127,6 +128,7 @@ declare module 'convert-csv-to-json' {
127
128
  fieldDelimiter(delimiter: string): this;
128
129
  indexHeader(index: number): this;
129
130
  parseSubArray(delimiter: string, separator: string): this;
131
+ mapRows(mapperFn: (row: any, index: number) => any | null): this;
130
132
 
131
133
  csvStringToJson(csvString: string): any[];
132
134
  csvStringToJsonStringified(csvString: string): string;
package/index.js CHANGED
@@ -123,6 +123,18 @@ exports.hexEncoding = function () {
123
123
  return this;
124
124
  };
125
125
 
126
+ /**
127
+ * Sets a mapper function to transform each row after conversion.
128
+ * The mapper function receives (row, index) where row is the JSON object
129
+ * and index is the 0-based row number. Return null/undefined to filter out rows.
130
+ * @param {Function} mapperFn - Function to transform each row
131
+ * @return {object} this for chaining
132
+ */
133
+ exports.mapRows = function (mapperFn) {
134
+ csvToJson.mapRows(mapperFn);
135
+ return this;
136
+ };
137
+
126
138
  /**
127
139
  * Parses .csv file and put its content into a file in json format.
128
140
  * @param {inputFileName} path/filename
@@ -192,16 +204,7 @@ exports.csvStringToJsonStringified = function(csvString) {
192
204
  return csvToJson.csvStringToJsonStringified(csvString);
193
205
  };
194
206
 
195
- /**
196
- * Parses .csv file and put its content into a file in json format.
197
- * @param {inputFileName} path/filename
198
- * @param {outputFileName} path/filename
199
- *
200
- * @deprecated Use generateJsonFileFromCsv()
201
- */
202
- exports.jsonToCsv = function(inputFileName, outputFileName) {
203
- csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName);
204
- };
207
+
205
208
 
206
209
  /**
207
210
  * Browser API
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convert-csv-to-json",
3
- "version": "3.27.0",
3
+ "version": "4.0.0",
4
4
  "description": "Convert CSV to JSON",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/browserApi.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const csvToJson = require('./csvToJson');
4
+ const { InputValidationError, BrowserApiError } = require('./util/errors');
4
5
 
5
6
  class BrowserApi {
6
7
  constructor() {
@@ -39,17 +40,32 @@ class BrowserApi {
39
40
  return this;
40
41
  }
41
42
 
43
+ mapRows(mapperFn) {
44
+ this.csvToJson.mapRows(mapperFn);
45
+ return this;
46
+ }
47
+
42
48
  // Synchronous parse from CSV string (browser friendly)
43
49
  csvStringToJson(csvString) {
44
50
  if (csvString === undefined || csvString === null) {
45
- throw new Error('csvString is not defined!!!');
51
+ throw new InputValidationError(
52
+ 'csvString',
53
+ 'string',
54
+ `${typeof csvString}`,
55
+ 'Provide valid CSV content as a string to parse.'
56
+ );
46
57
  }
47
58
  return this.csvToJson.csvToJson(csvString);
48
59
  }
49
60
 
50
61
  csvStringToJsonStringified(csvString) {
51
62
  if (csvString === undefined || csvString === null) {
52
- throw new Error('csvString is not defined!!!');
63
+ throw new InputValidationError(
64
+ 'csvString',
65
+ 'string',
66
+ `${typeof csvString}`,
67
+ 'Provide valid CSV content as a string to parse.'
68
+ );
53
69
  }
54
70
  return this.csvToJson.csvStringToJsonStringified(csvString);
55
71
  }
@@ -71,24 +87,31 @@ class BrowserApi {
71
87
  */
72
88
  parseFile(file, options = {}) {
73
89
  if (!file) {
74
- return Promise.reject(new Error('file is not defined!!!'));
90
+ return Promise.reject(new InputValidationError(
91
+ 'file',
92
+ 'File or Blob object',
93
+ `${typeof file}`,
94
+ 'Provide a valid File or Blob object to parse.'
95
+ ));
75
96
  }
76
97
 
77
98
  return new Promise((resolve, reject) => {
78
99
  if (typeof FileReader === 'undefined') {
79
- reject(new Error('FileReader is not available in this environment'));
100
+ reject(BrowserApiError.fileReaderNotAvailable());
80
101
  return;
81
102
  }
82
103
 
83
104
  const reader = new FileReader();
84
- reader.onerror = () => reject(reader.error || new Error('Failed to read file'));
105
+ reader.onerror = () => reject(BrowserApiError.parseFileError(
106
+ reader.error || new Error('Unknown file reading error')
107
+ ));
85
108
  reader.onload = () => {
86
109
  try {
87
110
  const text = reader.result;
88
111
  const result = this.csvToJson.csvToJson(String(text));
89
112
  resolve(result);
90
113
  } catch (err) {
91
- reject(err);
114
+ reject(BrowserApiError.parseFileError(err));
92
115
  }
93
116
  };
94
117