You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alter the value of a field. If true, the parser will attempt to convert input strings to native types. A function must return the new value and the received arguments are the value to cast and the context object. This option was named `auto_parse` until version 2.
Generate records as object literals instead of arrays. List of fields as an array, a user defined callback accepting the first line and returning the column names, or `true` if auto-discovered in the first CSV line. Defaults to `null`. Affects the result data set in the sense that records will be objects instead of arrays. A value "false" "null", or "undefined" inside the column array skips the column from the output.
Set the escape character as one character/byte only. It only applies to quote and escape characters inside quoted fields and it defaults to `"` (double quote).
46
-
*[`from`](/parse/options/from/) (number)
47
-
_Since version 1.2.0_
47
+
-[`from`](/parse/options/from/) (number)
48
+
_Since version 1.2.0_
48
49
Start handling records from a requested number of records. Count is 1-based, for example, provides `1` (and not `0`) to emit first record.
Disregard any delimiters present in the last field of the record, require the [`column`](/parse/options/columns/) option when `true`.
55
-
*[`info`](/parse/options/info/) (boolean)
56
-
_Since version 4.0.0_
56
+
-[`info`](/parse/options/info/) (boolean)
57
+
_Since version 4.0.0_
57
58
Generate two properties `info` and `record` where `info` is a snapshot of the info object at the time the record was created and `record` is the parsed array or object; note, it can be used conjointly with the `raw` option.
58
-
*[`ltrim`](/parse/options/ltrim/) (boolean)
59
-
_Since early days_
59
+
-[`ltrim`](/parse/options/ltrim/) (boolean)
60
+
_Since early days_
60
61
If `true`, ignore whitespace immediately following the delimiter (i.e. left-trim all fields). Defaults to `false`. Does not remove whitespace in a quoted field.
Maximum number of characters to be contained in the field and line buffers before an exception is raised. It was previously named "max_limit_on_data_read".
Optional character surrounding a field as one character only; disabled if null, false or empty; defaults to double quote.
73
-
*[`raw`](/parse/options/raw/) (boolean)
74
-
_Since version 1.1.6_
77
+
-[`raw`](/parse/options/raw/) (boolean)
78
+
_Since version 1.1.6_
75
79
Generate two properties `raw` and `record` where `raw` is the original CSV content and `record` is the parsed array or object; note, it can be used conjointly with the `info` option.
One or multiple characters used to delimit records; defaults to auto discovery if not provided. Supported auto discovery methods are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters. It was previously named `rowDelimiter`.
Preserve quotes inside unquoted field (be warned, it doesn't make coffee).
91
-
*[`rtrim`](/parse/options/rtrim/) (boolean)
92
-
_Since early days_
93
-
If `true`, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields). Defaults to `false`. Does not remove whitespace in a quoted field.
If `true`, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields). Defaults to `false`. Does not remove whitespace in a quoted field.
Don't generate records for lines containing empty values (column matching `/\s*/`), empty Buffer or equals to `null` and `undefined` if their value was casted, defaults to `false`.
The `on_skip` option provides a way to track invalid records without interrupting the parsing process. It defines a function called when records are skipped due to parsing errors.
11
+
12
+
- Type: `function`
13
+
- Optional
14
+
- Default: `undefined`
15
+
- Since: 5.1.0
16
+
- Related: [`on_record`](/parse/options/on_record/), [`skip_records_with_error`](/parse/options/raw/), [`raw`](/parse/options/skip_records_with_error/)— see [Available Options](/parse/options/#available-options)
17
+
18
+
The `on_skip` option works at the record level and requires the `skip_records_with_error` option to be enabled.
19
+
20
+
## Use cases
21
+
22
+
Use this option to:
23
+
24
+
- Log skipped records for later analysis
25
+
- Track parsing errors while maintaining the parsing process
26
+
- Monitor data quality issues in your CSV files
27
+
28
+
## Usage
29
+
30
+
The user function receives the error object as an argument. If the `raw` option is enabled, a second argument contains the CSV string being currently processed.
31
+
32
+
-`error`: Error encountered during parsing
33
+
-`message`: A descriptive error message
34
+
-`code`: The error code (e.g., "CSV_RECORD_INCONSISTENT_FIELDS_LENGTH")
35
+
-`record`: The raw record that caused the error
36
+
-`buffer`: Current processing buffer encoded as an UTF-8 string
37
+
38
+
## Example with inconsistent field lengths
39
+
40
+
The following example demonstrates how to handle records with inconsistent field counts:
The `on_skip` function is called after the parser has determined that a record should be skipped. It works in conjunction with the `skip_records_with_error` option:
47
+
48
+
1. When `skip_records_with_error` is `true`, invalid records are skipped and trigger the `on_skip` callback
49
+
2. When `skip_records_with_error` is `false` (default), parsing errors will cause the parser to emit an error and stop
50
+
51
+
## Error behaviour
52
+
53
+
Errors thrown inside the `on_skip` function are caught by the parser and handled as if `skip_records_with_error` was not enabled. It's recommended to implement proper error handling within your callback function to prevent the parsing process from being interrupted.
0 commit comments