summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--jquery.weekcalendar.js20
-rw-r--r--test/tests.js47
2 files changed, 34 insertions, 33 deletions
diff --git a/jquery.weekcalendar.js b/jquery.weekcalendar.js
index 6f56763..85994c9 100644
--- a/jquery.weekcalendar.js
+++ b/jquery.weekcalendar.js
@@ -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;
},
diff --git a/test/tests.js b/test/tests.js
index 91aecf4..bf5e731 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -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() {