Skip to content

Commit

Permalink
fix up cleanDate error reporting
Browse files Browse the repository at this point in the history
no error on `undefined` but yes on non-finite numbers
  • Loading branch information
alexcjohnson committed Sep 24, 2018
1 parent 7714aa6 commit c5cb42c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/lib/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ function includeTime(dateStr, h, m, s, msec10) {
// a Date object or milliseconds
// optional dflt is the return value if cleaning fails
exports.cleanDate = function(v, dflt, calendar) {
if(exports.isJSDate(v) || typeof v === 'number') {
// let us use cleanDate to provide a missing default without an error
if(v === BADNUM) return dflt;
if(exports.isJSDate(v) || (typeof v === 'number' && isFinite(v))) {
// do not allow milliseconds (old) or jsdate objects (inherently
// described as gregorian dates) with world calendars
if(isWorldCalendar(calendar)) {
Expand Down
8 changes: 5 additions & 3 deletions test/jasmine/tests/lib_date_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,22 @@ describe('dates', function() {
errors.push(msg);
});

[
var cases = [
new Date(-20000, 0, 1),
new Date(20000, 0, 1),
new Date('fail'),
undefined, null, NaN,
[], {}, [0], {1: 2}, '',
'2001-02-29' // not a leap year
].forEach(function(v) {
];
cases.forEach(function(v) {
expect(Lib.cleanDate(v)).toBeUndefined();
if(!isNumeric(+v)) expect(Lib.cleanDate(+v)).toBeUndefined();
expect(Lib.cleanDate(v, '2000-01-01')).toBe('2000-01-01');
});

expect(errors.length).toBe(16);
// two errors for each case except `undefined`
expect(errors.length).toBe(2 * (cases.length - 1));
});

it('should not alter valid date strings, even to truncate them', function() {
Expand Down

0 comments on commit c5cb42c

Please sign in to comment.