Skip to content

Commit

Permalink
docs(csv-parse): new cast sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 24, 2024
1 parent db53709 commit 70fc026
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
36 changes: 36 additions & 0 deletions 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
} ]
]);
43 changes: 19 additions & 24 deletions 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" ],
]);

0 comments on commit 70fc026

Please sign in to comment.