Browse Source

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>
v6.x
Rumkin 9 years ago
committed by Evan Lucas
parent
commit
6d1527bb37
  1. 6
      lib/util.js
  2. 3
      test/parallel/test-util-inspect.js

6
lib/util.js

@ -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);

3
test/parallel/test-util-inspect.js

@ -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'");

Loading…
Cancel
Save