Skip to content

Commit c5cb42c

Browse files
committed
fix up cleanDate error reporting
no error on `undefined` but yes on non-finite numbers
1 parent 7714aa6 commit c5cb42c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Diff for: src/lib/dates.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ function includeTime(dateStr, h, m, s, msec10) {
345345
// a Date object or milliseconds
346346
// optional dflt is the return value if cleaning fails
347347
exports.cleanDate = function(v, dflt, calendar) {
348-
if(exports.isJSDate(v) || typeof v === 'number') {
348+
// let us use cleanDate to provide a missing default without an error
349+
if(v === BADNUM) return dflt;
350+
if(exports.isJSDate(v) || (typeof v === 'number' && isFinite(v))) {
349351
// do not allow milliseconds (old) or jsdate objects (inherently
350352
// described as gregorian dates) with world calendars
351353
if(isWorldCalendar(calendar)) {

Diff for: test/jasmine/tests/lib_date_test.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -391,20 +391,22 @@ describe('dates', function() {
391391
errors.push(msg);
392392
});
393393

394-
[
394+
var cases = [
395395
new Date(-20000, 0, 1),
396396
new Date(20000, 0, 1),
397397
new Date('fail'),
398398
undefined, null, NaN,
399399
[], {}, [0], {1: 2}, '',
400400
'2001-02-29' // not a leap year
401-
].forEach(function(v) {
401+
];
402+
cases.forEach(function(v) {
402403
expect(Lib.cleanDate(v)).toBeUndefined();
403404
if(!isNumeric(+v)) expect(Lib.cleanDate(+v)).toBeUndefined();
404405
expect(Lib.cleanDate(v, '2000-01-01')).toBe('2000-01-01');
405406
});
406407

407-
expect(errors.length).toBe(16);
408+
// two errors for each case except `undefined`
409+
expect(errors.length).toBe(2 * (cases.length - 1));
408410
});
409411

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

0 commit comments

Comments
 (0)