Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix #970 aria.utils.Date.format can now handle falsy values and empty…

… objects

It now returns `null` in those cases instead of failing with a `TypeError`.

Close #971.
  • Loading branch information...
commit 8820a68f668487599d0002d6f684b66615a3d62c 1 parent 19936ea
dpreussner dpreussner authored jakub-g committed
Showing with 13 additions and 4 deletions.
  1. +8 −4 src/aria/utils/Date.js
  2. +5 −0 test/aria/utils/Date.js
12 src/aria/utils/Date.js
View
@@ -1448,6 +1448,9 @@ Aria.classDefinition({
* @return {String}
*/
format : function (date, pattern, utcTime) {
+ if (!aria.utils.Type.isDate(date)) {
+ return null;
+ }
if (typeof pattern === 'function') {
pattern = pattern();
@@ -1455,14 +1458,15 @@ Aria.classDefinition({
this.$logError(this.INVALID_FORMAT_TYPE);
}
- var formatFn = this._getFormatFunction(pattern), formattedDate;
- this.$assert(118, aria.utils.Type.isFunction(formatFn));
if (utcTime) {
// create a date object whose local time is the UTC time:
date = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
}
- formattedDate = formatFn(date);
- return formattedDate;
+
+ var formatFn = this._getFormatFunction(pattern);
+ this.$assert(118, aria.utils.Type.isFunction(formatFn));
+
+ return formatFn(date);
},
/**
5 test/aria/utils/Date.js
View
@@ -263,6 +263,11 @@ Aria.classDefinition({
testAsyncDateFormatting : function () {
var ariaDateUtil = aria.utils.Date;
+ this.assertEquals(null, ariaDateUtil.format(null, "dd/MM/yyyy"));
+ this.assertEquals(null, ariaDateUtil.format(undefined, "dd/MM/yyyy"));
+ this.assertEquals(null, ariaDateUtil.format("", "dd/MM/yyyy"));
+ this.assertEquals(null, ariaDateUtil.format({}, "dd/MM/yyyy"));
+
var formattedValue = ariaDateUtil.format(new Date(2010, 3, 1, 0, 0, 0), "d MMM y");
this.assertTrue(formattedValue === "1 Apr 10", "Wrong output: " + formattedValue);
Please sign in to comment.
Something went wrong with that request. Please try again.