Skip to content

Commit 2807d29

Browse files
committed
feat(csv-parse): cast_date as a function (fix #342)
1 parent fb1b21c commit 2807d29

File tree

11 files changed

+30
-9
lines changed

11 files changed

+30
-9
lines changed

packages/csv-parse/dist/cjs/index.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ const normalize_options = function(opts){
208208
const date = Date.parse(value);
209209
return !isNaN(date) ? new Date(date) : value;
210210
};
211-
}else {
211+
}else if (typeof options.cast_date !== 'function'){
212212
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
213213
'Invalid option cast_date:', 'cast_date must be true or a function,',
214214
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/cjs/sync.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ const normalize_options = function(opts){
206206
const date = Date.parse(value);
207207
return !isNaN(date) ? new Date(date) : value;
208208
};
209-
}else {
209+
}else if (typeof options.cast_date !== 'function'){
210210
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
211211
'Invalid option cast_date:', 'cast_date must be true or a function,',
212212
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/esm/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5264,7 +5264,7 @@ const normalize_options = function(opts){
52645264
const date = Date.parse(value);
52655265
return !isNaN(date) ? new Date(date) : value;
52665266
};
5267-
}else {
5267+
}else if (typeof options.cast_date !== 'function'){
52685268
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
52695269
'Invalid option cast_date:', 'cast_date must be true or a function,',
52705270
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/esm/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,7 @@ const normalize_options = function(opts){
21742174
const date = Date.parse(value);
21752175
return !isNaN(date) ? new Date(date) : value;
21762176
};
2177-
}else {
2177+
}else if (typeof options.cast_date !== 'function'){
21782178
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
21792179
'Invalid option cast_date:', 'cast_date must be true or a function,',
21802180
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/iife/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5267,7 +5267,7 @@ var csv_parse = (function (exports) {
52675267
const date = Date.parse(value);
52685268
return !isNaN(date) ? new Date(date) : value;
52695269
};
5270-
}else {
5270+
}else if (typeof options.cast_date !== 'function'){
52715271
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
52725272
'Invalid option cast_date:', 'cast_date must be true or a function,',
52735273
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/iife/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,7 @@ var csv_parse_sync = (function (exports) {
21772177
const date = Date.parse(value);
21782178
return !isNaN(date) ? new Date(date) : value;
21792179
};
2180-
}else {
2180+
}else if (typeof options.cast_date !== 'function'){
21812181
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
21822182
'Invalid option cast_date:', 'cast_date must be true or a function,',
21832183
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/umd/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5270,7 +5270,7 @@
52705270
const date = Date.parse(value);
52715271
return !isNaN(date) ? new Date(date) : value;
52725272
};
5273-
}else {
5273+
}else if (typeof options.cast_date !== 'function'){
52745274
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
52755275
'Invalid option cast_date:', 'cast_date must be true or a function,',
52765276
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/dist/umd/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@
21802180
const date = Date.parse(value);
21812181
return !isNaN(date) ? new Date(date) : value;
21822182
};
2183-
}else {
2183+
}else if (typeof options.cast_date !== 'function'){
21842184
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
21852185
'Invalid option cast_date:', 'cast_date must be true or a function,',
21862186
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/lib/api/normalize_options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const normalize_options = function(opts){
5353
const date = Date.parse(value);
5454
return !isNaN(date) ? new Date(date) : value;
5555
};
56-
}else{
56+
}else if (typeof options.cast_date !== 'function'){
5757
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
5858
'Invalid option cast_date:', 'cast_date must be true or a function,',
5959
`got ${JSON.stringify(options.cast_date)}`

packages/csv-parse/test/api.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ describe('API Types', () => {
143143
const options: Options = {}
144144
options.cast_date = true
145145
options.castDate = true
146+
options.cast_date = (value: string, context: CastingContext) => {
147+
return new Date(`${value} ${context.index}`)
148+
}
146149
})
147150

148151
it('columns', () => {

0 commit comments

Comments
 (0)