-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} ] | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" ], | ||
]); |