Skip to content

Commit

Permalink
feat(csv-parse): cast_date as a function (fix #342)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jun 14, 2022
1 parent fb1b21c commit 2807d29
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/cjs/index.cjs
Expand Up @@ -208,7 +208,7 @@ const normalize_options = function(opts){
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/cjs/sync.cjs
Expand Up @@ -206,7 +206,7 @@ const normalize_options = function(opts){
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/esm/index.js
Expand Up @@ -5264,7 +5264,7 @@ const normalize_options = function(opts){
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/esm/sync.js
Expand Up @@ -2174,7 +2174,7 @@ const normalize_options = function(opts){
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/iife/index.js
Expand Up @@ -5267,7 +5267,7 @@ var csv_parse = (function (exports) {
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/iife/sync.js
Expand Up @@ -2177,7 +2177,7 @@ var csv_parse_sync = (function (exports) {
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/umd/index.js
Expand Up @@ -5270,7 +5270,7 @@
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/dist/umd/sync.js
Expand Up @@ -2180,7 +2180,7 @@
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else {
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/lib/api/normalize_options.js
Expand Up @@ -53,7 +53,7 @@ const normalize_options = function(opts){
const date = Date.parse(value);
return !isNaN(date) ? new Date(date) : value;
};
}else{
}else if (typeof options.cast_date !== 'function'){
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
'Invalid option cast_date:', 'cast_date must be true or a function,',
`got ${JSON.stringify(options.cast_date)}`
Expand Down
3 changes: 3 additions & 0 deletions packages/csv-parse/test/api.types.ts
Expand Up @@ -143,6 +143,9 @@ describe('API Types', () => {
const options: Options = {}
options.cast_date = true
options.castDate = true
options.cast_date = (value: string, context: CastingContext) => {
return new Date(`${value} ${context.index}`)
}
})

it('columns', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/csv-parse/test/option.cast_date.coffee
Expand Up @@ -25,6 +25,24 @@ describe 'Option `cast_date`', ->
]
next err

it 'as a function', (next) ->
# Current implementation rely on `isNaN(Date.parse(value))`
# While it return `NaN` in Firefox, Node.js return a timestamp for
# `Date.parse('Test 1')`
parser = parse """
2000-01-01
2050-11-27
""",
cast: true
cast_date: (value, context) ->
new Date( (new Date(value)).getTime() + context.lines*60*60*1000)
, (err, records) ->
records.should.eql [
[ new Date('2000-01-01T01:00:00.000Z') ],
[ new Date('2050-11-27T02:00:00.000Z') ]
]
next err

it 'value end with space and number (issue #342)', (next) ->
# Current implementation rely on `isNaN(Date.parse(value))`
# While it return `NaN` in Firefox, Node.js return a timestamp for
Expand Down

0 comments on commit 2807d29

Please sign in to comment.