convert-csv-to-json 4.42.0 → 4.43.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 +1 -0
- package/docs/BROWSER.md +16 -0
- package/docs/SYNC.md +25 -0
- package/index.d.ts +7 -0
- package/index.js +20 -0
- package/package.json +1 -1
- package/src/browserApi.js +11 -0
- package/src/csvToJson.js +15 -0
- package/src/csvToJsonAsync.js +11 -0
package/README.md
CHANGED
|
@@ -147,6 +147,7 @@ All APIs (Sync, Async and Browser) support the same configuration methods:
|
|
|
147
147
|
- `supportQuotedField(bool)` - Handle quoted fields with embedded delimiters
|
|
148
148
|
- `indexHeader(num)` - Specify header row (default: 0)
|
|
149
149
|
- `trimHeaderFieldWhiteSpace(bool)` - Remove spaces from headers
|
|
150
|
+
- `ignoreColumnIndexes(indexes)` - Exclude specific columns by index from the JSON output
|
|
150
151
|
- `parseSubArray(delim, sep)` - Parse delimited arrays
|
|
151
152
|
- `mapRows(fn)` - Transform, filter, or enrich each row
|
|
152
153
|
- `getJsonFromStreamAsync(stream)` - Process CSV from Readable streams for NodeJS and Browser
|
package/docs/BROWSER.md
CHANGED
|
@@ -227,6 +227,22 @@ const json = convert.browser
|
|
|
227
227
|
// Output: { Name: 'Alice', Age: '30' } (spaces removed from header)
|
|
228
228
|
```
|
|
229
229
|
|
|
230
|
+
### Ignore Column Indexes
|
|
231
|
+
|
|
232
|
+
```js
|
|
233
|
+
const csv = 'name,age,active\nAlice,30,true\nBob,25,false';
|
|
234
|
+
|
|
235
|
+
const json = convert.browser
|
|
236
|
+
.ignoreColumnIndexes([2])
|
|
237
|
+
.csvStringToJson(csv);
|
|
238
|
+
|
|
239
|
+
// Output:
|
|
240
|
+
// [
|
|
241
|
+
// { name: 'Alice', age: 30 },
|
|
242
|
+
// { name: 'Bob', age: 25 }
|
|
243
|
+
// ]
|
|
244
|
+
```
|
|
245
|
+
|
|
230
246
|
### Method Chaining
|
|
231
247
|
|
|
232
248
|
```js
|
package/docs/SYNC.md
CHANGED
|
@@ -198,6 +198,31 @@ const json = csvToJson.csvStringToJson(csv);
|
|
|
198
198
|
const jsonString = csvToJson.csvStringToJsonStringified(csv);
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
+
### Ignore Column Indexes
|
|
202
|
+
Exclude specific columns from the JSON output by specifying their column indexes:
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
// Ignore columns at index 2 and 3
|
|
206
|
+
csvToJson
|
|
207
|
+
.ignoreColumnIndexes([2, 3])
|
|
208
|
+
.getJsonFromCsv('file.csv');
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
**Example Input:**
|
|
212
|
+
```csv
|
|
213
|
+
name,email,active,id
|
|
214
|
+
John,john@example.com,true,0012
|
|
215
|
+
Jane,jane@example.com,false,987
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**Output:**
|
|
219
|
+
```json
|
|
220
|
+
[
|
|
221
|
+
{ "name": "John", "email": "John,john@example.com" },
|
|
222
|
+
{ "name": "Jane", "email": "jane@example.com" }
|
|
223
|
+
]
|
|
224
|
+
```
|
|
225
|
+
|
|
201
226
|
### Method Chaining
|
|
202
227
|
|
|
203
228
|
Combine multiple configuration options:
|
package/index.d.ts
CHANGED
|
@@ -32,6 +32,12 @@ declare module 'convert-csv-to-json' {
|
|
|
32
32
|
*/
|
|
33
33
|
parseSubArray(delimiter: string, separator: string): this;
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Set column indexes to ignore
|
|
37
|
+
* Specified columns will be excluded from the JSON output
|
|
38
|
+
*/
|
|
39
|
+
ignoreColumnIndexes(indexes: number[]): this;
|
|
40
|
+
|
|
35
41
|
/**
|
|
36
42
|
* Defines a custom encoding to decode a file
|
|
37
43
|
*/
|
|
@@ -139,6 +145,7 @@ declare module 'convert-csv-to-json' {
|
|
|
139
145
|
fieldDelimiter(delimiter: string): this;
|
|
140
146
|
indexHeader(index: number): this;
|
|
141
147
|
parseSubArray(delimiter: string, separator: string): this;
|
|
148
|
+
ignoreColumnIndexes(indexes: number[]): this;
|
|
142
149
|
mapRows(mapperFn: (row: any, index: number) => any | null): this;
|
|
143
150
|
|
|
144
151
|
csvStringToJson(csvString: string): any[];
|
package/index.js
CHANGED
|
@@ -95,6 +95,26 @@ exports.parseSubArray = function (delimiter, separator) {
|
|
|
95
95
|
return this;
|
|
96
96
|
};
|
|
97
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Set column indexes to ignore
|
|
100
|
+
* Specified columns will be excluded from the JSON output
|
|
101
|
+
* @category 1-Core API
|
|
102
|
+
* @param {Array<number>} indexes - Array of column indexes to ignore
|
|
103
|
+
* @returns {object} Module context for method chaining
|
|
104
|
+
* @example
|
|
105
|
+
* csvToJson.ignoreColumnIndexes([1, 3]) // Ignore columns at index 1 and 3
|
|
106
|
+
*/
|
|
107
|
+
exports.ignoreColumnIndexes = function (indexes) {
|
|
108
|
+
if (!Array.isArray(indexes)) {
|
|
109
|
+
throw new TypeError('indexes must be an array of numbers');
|
|
110
|
+
}
|
|
111
|
+
if (!indexes.every(idx => Number.isInteger(idx) && idx >= 0)) {
|
|
112
|
+
throw new TypeError('All elements in indexes must be valid non-negative numbers (>= 0)');
|
|
113
|
+
}
|
|
114
|
+
csvToJson.ignoreColumnIndexes(indexes);
|
|
115
|
+
return this;
|
|
116
|
+
};
|
|
117
|
+
|
|
98
118
|
/**
|
|
99
119
|
* Set custom file encoding for reading CSV files
|
|
100
120
|
* Useful for non-UTF8 encoded files
|
package/package.json
CHANGED
package/src/browserApi.js
CHANGED
|
@@ -71,6 +71,17 @@ class BrowserApi {
|
|
|
71
71
|
return this;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Configure columns to exclude from output
|
|
76
|
+
* @param {Array<number>} indexes - Column indexes to ignore
|
|
77
|
+
* @returns {this} For method chaining
|
|
78
|
+
* @private Used internally after validation in index.js
|
|
79
|
+
*/
|
|
80
|
+
ignoreColumnIndexes(indexes) {
|
|
81
|
+
this.csvToJson.ignoreColumnIndexes(indexes);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
|
|
74
85
|
/**
|
|
75
86
|
* Configure sub-array parsing for special field values
|
|
76
87
|
* @param {string} delimiter - Bracket character (default: '*')
|
package/src/csvToJson.js
CHANGED
|
@@ -107,6 +107,17 @@ class CsvToJson {
|
|
|
107
107
|
return this;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Configure columns to exclude from output
|
|
112
|
+
* @param {Array<number>} indexes - Column indexes to ignore
|
|
113
|
+
* @returns {this} For method chaining
|
|
114
|
+
* @private Used internally after validation in index.js
|
|
115
|
+
*/
|
|
116
|
+
ignoreColumnIndexes(indexes) {
|
|
117
|
+
this.indexesToIgnore = new Set(indexes);
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
110
121
|
/**
|
|
111
122
|
* Sets a mapper function to transform each row after conversion
|
|
112
123
|
* @param {function(object, number): (object|null)} mapperFn - Function that receives (row, index) and returns transformed row or null to filter out
|
|
@@ -375,6 +386,10 @@ class CsvToJson {
|
|
|
375
386
|
buildJsonResult(headers, currentLine) {
|
|
376
387
|
let jsonObject = {};
|
|
377
388
|
for (let j = 0; j < headers.length; j++) {
|
|
389
|
+
if (this.indexesToIgnore?.has(j)) {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
|
|
378
393
|
let propertyName = stringUtils.trimPropertyName(this.isTrimHeaderFieldWhiteSpace, headers[j]);
|
|
379
394
|
let value = currentLine[j];
|
|
380
395
|
|
package/src/csvToJsonAsync.js
CHANGED
|
@@ -101,6 +101,17 @@ class CsvToJsonAsync {
|
|
|
101
101
|
return this;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Configure columns to exclude from output
|
|
106
|
+
* @param {Array<number>} indexes - Column indexes to ignore
|
|
107
|
+
* @returns {this} For method chaining
|
|
108
|
+
* @private Used internally after validation in index.js
|
|
109
|
+
*/
|
|
110
|
+
ignoreColumnIndexes(indexes) {
|
|
111
|
+
this.csvToJson.ignoreColumnIndexes(indexes);
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
104
115
|
/**
|
|
105
116
|
* Read a CSV file and write parsed JSON to an output file (async)
|
|
106
117
|
* @param {string} fileInputName - Path to input CSV file
|