diff --git a/packages/csv-parse/samples/option.cast.context.js b/packages/csv-parse/samples/option.cast.context.js new file mode 100644 index 00000000..8270f0c8 --- /dev/null +++ b/packages/csv-parse/samples/option.cast.context.js @@ -0,0 +1,36 @@ + +import assert from 'node:assert'; +import { parse } from 'csv-parse/sync'; + +const data = ` + 2000-01-01,date1 + 2050-11-27,date2 +`.trim(); +const records = parse(data, { + // The cast option exect a function which + // is called with two arguments, + // the parsed value and a context object + cast: function(value, context){ + // You can return any value + if(context.index === 0){ + // Such as a string + return `${value}T05:00:00.000Z`; + }else{ + // Or the `context` object literal + return context; + } + }, + trim: true +}); +assert.deepStrictEqual(records, [ + [ '2000-01-01T05:00:00.000Z', { + bytes: 16, comment_lines: 0, empty_lines: 0, invalid_field_length: 0, + lines: 1, records: 0, columns: false, error: undefined, header: false, + index: 1, column: 1, quoting: false, raw: undefined + } ], + [ '2050-11-27T05:00:00.000Z', { + bytes: 35, comment_lines: 0, empty_lines: 0, invalid_field_length: 0, + lines: 2, records: 1, columns: false, error: undefined, header: false, + index: 1, column: 1, quoting: false, raw: undefined + } ] +]); diff --git a/packages/csv-parse/samples/option.cast.js b/packages/csv-parse/samples/option.cast.js index 8270f0c8..120aa1d5 100644 --- a/packages/csv-parse/samples/option.cast.js +++ b/packages/csv-parse/samples/option.cast.js @@ -1,36 +1,31 @@ -import assert from 'node:assert'; -import { parse } from 'csv-parse/sync'; +import assert from "node:assert"; +import { parse } from "csv-parse/sync"; const data = ` - 2000-01-01,date1 - 2050-11-27,date2 + 1,2,3 + 4,5,6 `.trim(); const records = parse(data, { - // The cast option exect a function which + // The cast option exect a function which // is called with two arguments, // the parsed value and a context object - cast: function(value, context){ - // You can return any value - if(context.index === 0){ - // Such as a string - return `${value}T05:00:00.000Z`; - }else{ - // Or the `context` object literal - return context; + cast: function (value, context) { + // Index indicates the column position + if (context.index === 0) { + // Return the value untouched + return value; + } else if (context.index === 1) { + // Convert the value to a string + return parseInt(value); + } else { + // Return a different value + return `Value is ${value}` } }, - trim: true + trim: true, }); assert.deepStrictEqual(records, [ - [ '2000-01-01T05:00:00.000Z', { - bytes: 16, comment_lines: 0, empty_lines: 0, invalid_field_length: 0, - lines: 1, records: 0, columns: false, error: undefined, header: false, - index: 1, column: 1, quoting: false, raw: undefined - } ], - [ '2050-11-27T05:00:00.000Z', { - bytes: 35, comment_lines: 0, empty_lines: 0, invalid_field_length: 0, - lines: 2, records: 1, columns: false, error: undefined, header: false, - index: 1, column: 1, quoting: false, raw: undefined - } ] + [ "1", 2, "Value is 3" ], + [ "4", 5, "Value is 6" ], ]);