Skip to content

Commit 963571b

Browse files
committed
fix(dateutils): Use 3rd party parser if provided
1 parent 57e36ab commit 963571b

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/utils/DateUtils.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ const getParsableDate = ({ formatStr, dateStr, translation, time }) => {
4444
* @return {Date | String}
4545
*/
4646
function parseDateWithLibrary(dateStr, parser) {
47-
if (!parser || typeof parser !== 'function') {
48-
throw new Error(
49-
'Parser needs to be a function if you are using a custom formatter',
50-
)
47+
if (typeof parser !== 'function') {
48+
throw new Error('Parser needs to be a function')
5149
}
5250

5351
return parser(dateStr)
@@ -318,7 +316,7 @@ const utils = {
318316
return dateStr
319317
}
320318

321-
if (typeof formatStr === 'function') {
319+
if (parser) {
322320
return parseDateWithLibrary(dateStr, parser)
323321
}
324322

test/unit/specs/DateUtils.spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,10 @@ describe('dateUtils', () => {
132132
)
133133
})
134134

135-
it('fails to parse because of missing parser', () => {
135+
it('fails to parse because parser is not a function', () => {
136136
expect(() => {
137-
dateUtils.parseDate('16 April 2020', () => {})
138-
}).toThrowError(
139-
'Parser needs to be a function if you are using a custom formatter',
140-
)
137+
dateUtils.parseDate('16 April 2020', () => {}, en, 'dd/MM/yyyy')
138+
}).toThrowError('Parser needs to be a function')
141139
})
142140

143141
it('parses formats without day', () => {

0 commit comments

Comments
 (0)