Skip to content

Commit 70fc026

Browse files
committed
docs(csv-parse): new cast sample
1 parent db53709 commit 70fc026

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
import assert from 'node:assert';
3+
import { parse } from 'csv-parse/sync';
4+
5+
const data = `
6+
2000-01-01,date1
7+
2050-11-27,date2
8+
`.trim();
9+
const records = parse(data, {
10+
// The cast option exect a function which
11+
// is called with two arguments,
12+
// the parsed value and a context object
13+
cast: function(value, context){
14+
// You can return any value
15+
if(context.index === 0){
16+
// Such as a string
17+
return `${value}T05:00:00.000Z`;
18+
}else{
19+
// Or the `context` object literal
20+
return context;
21+
}
22+
},
23+
trim: true
24+
});
25+
assert.deepStrictEqual(records, [
26+
[ '2000-01-01T05:00:00.000Z', {
27+
bytes: 16, comment_lines: 0, empty_lines: 0, invalid_field_length: 0,
28+
lines: 1, records: 0, columns: false, error: undefined, header: false,
29+
index: 1, column: 1, quoting: false, raw: undefined
30+
} ],
31+
[ '2050-11-27T05:00:00.000Z', {
32+
bytes: 35, comment_lines: 0, empty_lines: 0, invalid_field_length: 0,
33+
lines: 2, records: 1, columns: false, error: undefined, header: false,
34+
index: 1, column: 1, quoting: false, raw: undefined
35+
} ]
36+
]);
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
11

2-
import assert from 'node:assert';
3-
import { parse } from 'csv-parse/sync';
2+
import assert from "node:assert";
3+
import { parse } from "csv-parse/sync";
44

55
const data = `
6-
2000-01-01,date1
7-
2050-11-27,date2
6+
1,2,3
7+
4,5,6
88
`.trim();
99
const records = parse(data, {
10-
// The cast option exect a function which
10+
// The cast option exect a function which
1111
// is called with two arguments,
1212
// the parsed value and a context object
13-
cast: function(value, context){
14-
// You can return any value
15-
if(context.index === 0){
16-
// Such as a string
17-
return `${value}T05:00:00.000Z`;
18-
}else{
19-
// Or the `context` object literal
20-
return context;
13+
cast: function (value, context) {
14+
// Index indicates the column position
15+
if (context.index === 0) {
16+
// Return the value untouched
17+
return value;
18+
} else if (context.index === 1) {
19+
// Convert the value to a string
20+
return parseInt(value);
21+
} else {
22+
// Return a different value
23+
return `Value is ${value}`
2124
}
2225
},
23-
trim: true
26+
trim: true,
2427
});
2528
assert.deepStrictEqual(records, [
26-
[ '2000-01-01T05:00:00.000Z', {
27-
bytes: 16, comment_lines: 0, empty_lines: 0, invalid_field_length: 0,
28-
lines: 1, records: 0, columns: false, error: undefined, header: false,
29-
index: 1, column: 1, quoting: false, raw: undefined
30-
} ],
31-
[ '2050-11-27T05:00:00.000Z', {
32-
bytes: 35, comment_lines: 0, empty_lines: 0, invalid_field_length: 0,
33-
lines: 2, records: 1, columns: false, error: undefined, header: false,
34-
index: 1, column: 1, quoting: false, raw: undefined
35-
} ]
29+
[ "1", 2, "Value is 3" ],
30+
[ "4", 5, "Value is 6" ],
3631
]);

0 commit comments

Comments
 (0)