Permalink
Browse files

Fix: _cleanDate was not able to parse numerical values in strings obj…

…ects (fixes #103)
  • Loading branch information...
K-Phoen committed Apr 26, 2012
1 parent b041194 commit b112f4b01a999ba25237d2649bef69ff3ede88a1
Showing with 34 additions and 33 deletions.
  1. +15 −5 jquery.weekcalendar.js
  2. +19 −28 test/tests.js
View
@@ -2125,21 +2125,31 @@
return new Date(d.getTime());
},
- /*
- * return a date for different representations
- */
+ /**
+ * Return a Date instance for different representations.
+ * Valid representations are:
+ * * timestamps
+ * * Date objects
+ * * textual representations (only these accepted by the Date
+ * constructor)
+ *
+ * @return {Date} The clean date object.
+ */
_cleanDate: function(d) {
- if (typeof d == 'string') {
+ if (typeof d === 'string') {
// if is numeric
- if (!isNaN(parseFloat(d)) && isFinite()) {
+ if (!isNaN(Number(d))) {
return this._cleanDate(parseInt(d, 10));
}
+
// this is a human readable date
return Date.parse(d) || new Date(d);
}
+
if (typeof d == 'number') {
return new Date(d);
}
+
return d;
},
View
@@ -55,36 +55,27 @@ test('date parsing', function() {
var $calendar = $('#calendar');
$calendar.weekCalendar();
- expect(13);
-
- var _cleanDate = $calendar.data('weekCalendar')._cleanDate,
- _curdate;
- ok($.isFunction(_cleanDate), 'check _cleanDate is a function');
-
- _curdate = _cleanDate(new Date('Fri Jul 16 2010 14:15:00'));
- ok(_curdate instanceof Date, '"new Date(\'Fri Jul 16 2010 14:15:00\')": parsed date is a Date object');
- equal(_curdate.getTime(), new Date('Fri Jul 16 2010 14:15:00').getTime(), 'expected time');
-
- _curdate = _cleanDate(1276683300000);
- ok(_curdate instanceof Date, '"1276683300000": parsed date is a Date object');
- equal(_curdate.getTime(), 1276683300000, 'expected time');
-
- _curdate = _cleanDate('2010-06-16T12:15:00+02:00');
- ok(_curdate instanceof Date, '"2010-06-16T12:15:00+02:00": parsed date is a Date object');
- equal(_curdate.getTime(), 1276683300000, 'expected time');
-
- _curdate = _cleanDate('2010-06-16T12:15:00.000+02:00');
- ok(_curdate instanceof Date, '"2010-06-16T12:15:00.000+02:00": parsed date is a Date object');
- equal(_curdate.getTime(), 1276683300000, 'expected time');
-
- _curdate = _cleanDate('Wed Jun 16 2010 12:15:00 GMT+0200');
- ok(_curdate instanceof Date, '"Wed Jun 16 2010 12:15:00 GMT+0200": parsed date is a Date object');
- equal(_curdate.getTime(), 1276683300000, 'expected time');
+ expect(15);
+
+ var _cleanDate = $.proxy($calendar.data('weekCalendar'), '_cleanDate'),
+ _curdate,
+ testData = [
+ {value: new Date('Fri Jul 16 2010 14:15:00'), expected: new Date('Fri Jul 16 2010 14:15:00').getTime()},
+ {value: 1276683300000, expected: 1276683300000},
+ {value: '1276683300000', expected: 1276683300000},
+ {value: '2010-06-16T12:15:00+02:00', expected: 1276683300000},
+ {value: '2010-06-16T12:15:00.000+02:00', expected: 1276683300000},
+ {value: 'Wed Jun 16 2010 12:15:00 GMT+0200', expected: 1276683300000},
+ {value: '2010-06-16T12:15', expected: 1276683300000},
+ ];
- _curdate = _cleanDate('2010-06-16T12:15');
- ok(_curdate instanceof Date, '"2010-06-16T12:15": parsed date is a Date object');
- equal(_curdate.getTime(), 1276683300000, 'expected time');
+ ok($.isFunction($calendar.data('weekCalendar')._cleanDate), 'check _cleanDate is a function');
+ $(testData).each(function(i, item) {
+ _curdate = $calendar.data('weekCalendar')._cleanDate(item.value);
+ ok(_curdate instanceof Date, 'Case ' + i + ': "_cleanDate" returns a Date instance');
+ equal(_curdate.getTime(), item.expected, 'Case ' + i + ': The returned date is correct.');
+ });
});
test('Date internationalization', function() {

0 comments on commit b112f4b

Please sign in to comment.