util: fix invalid date output with util.inspect

Prevent util.inspect of throwing on date object with invalid date value.
It changed to output result of toString method call.

PR-URL: https://github.com/nodejs/node/pull/6504
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Rumkin 2016-05-01 15:44:46 +00:00 committed by Anna Henningsen
parent f17b9494e1
commit 9c33e0ebe7
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
2 changed files with 7 additions and 2 deletions

View File

@ -348,7 +348,11 @@ function formatValue(ctx, value, recurseTimes) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
if (Number.isNaN(value.getTime())) {
return ctx.stylize(value.toString(), 'date');
} else {
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
}
}
if (isError(value)) {
return formatError(value);

View File

@ -12,8 +12,9 @@ assert.equal(util.inspect(function() {}), '[Function]');
assert.equal(util.inspect(undefined), 'undefined');
assert.equal(util.inspect(null), 'null');
assert.equal(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');
assert.equal(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
assert.strictEqual(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
new Date('2010-02-14T12:48:40+01:00').toISOString());
assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString());
assert.equal(util.inspect('\n\u0001'), "'\\n\\u0001'");