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
The `cast` option alter a value. It works at the fieldlevel of a record. It is possible to transform its value or change its type.
10
+
The `cast` option works at the field-level to alter its value. It is possible to transform the field's value or change its type.
11
11
12
12
* Type: `function`
13
13
* Optional
14
14
* Default: `undefined`
15
15
* Since: 2.2.0
16
16
* Related: `cast_date`, [`info`](/parse/options/info/), [`on_record`](/parse/options/on_record/)— see [Available Options](/parse/options/#available-options)
17
17
18
-
Its value is expected to be a function which receives a contextrich of information. It gives full control over a field. The [`test/option.cast.coffee`](https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/test/option.cast.coffee) test provides insights on how to use it and its supported functionalities.
18
+
The `cast`value is expected to be a function that receives context-rich information. The function has full control over a field. The [`test/option.cast.coffee`](https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/test/option.cast.coffee) test provides insights on how to use it and its supported functionalities.
19
19
20
20
## Context
21
21
22
-
The function is called with 2 arguments: the field value and a context object. The context object expose the following properties:
22
+
The `cast`function is called with 2 arguments: the field `value` and the `context` object.
23
23
24
+
The `context` object exposes the following properties:
24
25
*`column` (number|string)
25
-
The column name if the `columns` options is defined or the field position.
26
+
The column name if the `columns` options is defined, or the field position.
26
27
*`empty_lines` (number)
27
28
Internal counter of empty lines encountered until this field.
28
29
*`header` (boolean)
29
30
A boolean indicating if the provided value is a part of the header.
30
31
*`index` (number)
31
-
The field position starting at 0.
32
+
The field position within the current record starting at 0.
32
33
*`invalid_field_length` (number)
33
34
Number of records with a non uniform length when [`relax_column_count`](/parse/options/relax_column_count/) is true. It was named `skipped_lines` until version 3.
34
35
*`lines` (number)
@@ -38,15 +39,15 @@ The function is called with 2 arguments: the field value and a context object. T
38
39
*`records` (number)
39
40
The number of records which have been fully parsed. It was named `count` until version 3.
40
41
41
-
The [cast example](https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/samples/option.cast.js) uses the context to transform the first filed into a date and replace the second field with the injected context:
42
+
The [cast example](https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/samples/option.cast.js) uses the context to transform the first field into a date and replace the second field with the injected context:
42
43
43
44
`embed:packages/csv-parse/samples/option.cast.js`
44
45
45
46
## Using the `cast` and `columns` functions conjointly
46
47
47
-
The `cast` function is called field by field, whether it is considered a header or not. The `columns` function is called once the first record is created (if treated as a header). For this reason, `cast` is executed before `columns`.
48
+
The `cast` function is called for each and every field, whether it is considered a header or not. The `columns` function is called once the first record is created (if treated as a header). For this reason, `cast` is executed before `columns`.
48
49
49
-
To distinguish a header field from a data field in the `cast` function, use the [`context.header` property](https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/samples/option.cast.header.column.fn.js)in the second argument of the `cast` function:
50
+
To distinguish a header field from a data field in the `cast` function, use the [`context.header` property](https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/samples/option.cast.header.column.fn.js)from the second argument to the `cast` function:
0 commit comments