Skip to content

Commit

Permalink
Set moment in strict mode
Browse files Browse the repository at this point in the history
This will cause moment to return undefined when parsing a date that doesn't match the format

For: gpbl#584
  • Loading branch information
jbarco committed Dec 20, 2017
1 parent f27211f commit b60c882
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/addons/MomentLocaleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function formatDate(date, format = 'L', locale = 'en') {
}

export function parseDate(str, format = 'L', locale = 'en') {
const m = moment(str, format, locale);
const m = moment(str, format, locale, true);
if (m.isValid()) {
return m.toDate();
}
Expand Down
4 changes: 4 additions & 0 deletions test/addons/MomentLocaleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,9 @@ describe('MomentLocaleUtils', () => {
const parsed = MomentLocaleUtils.parseDate('20 foo 2018', 'LL', 'it');
expect(parsed).toBeUndefined();
});
it('returns undefined if date does not match the format', () => {
const parsed = MomentLocaleUtils.parseDate('20/11/201', 'DD/MM/YYYY');
expect(parsed).toBeUndefined();
});
});
});

0 comments on commit b60c882

Please sign in to comment.