Skip to content

Commit 3747fa8

Browse files
fix: invalid Date object will throw a TypeError (#58) (#59)
* fix: Invalid Date object will throw a TypeError (#58) * fixed lint error: unnecessary 'else' after 'return'
1 parent b4ee644 commit 3747fa8

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/date.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { truncate } from './helpers'
22

33
export default function inspectDate(dateObject, options) {
4-
// If we need to - truncate the time portion, but never the date
5-
const split = dateObject.toJSON().split('T')
4+
const stringRepresentation = dateObject.toJSON()
5+
6+
if (stringRepresentation === null) {
7+
return 'Invalid Date'
8+
}
9+
10+
const split = stringRepresentation.split('T')
611
const date = split[0]
12+
// If we need to - truncate the time portion, but never the date
713
return options.stylize(`${date}T${truncate(split[1], options.truncate - date.length - 1)}`, 'date')
814
}

test/dates.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ describe('date', () => {
55
expect(inspect(new Date(1475318637123))).to.equal('2016-10-01T10:43:57.123Z')
66
})
77

8+
it('returns "Invalid Date" if given an invalid Date object', () => {
9+
// See: https://github.com/chaijs/loupe/issues/58
10+
expect(inspect(new Date('not a date'))).to.equal('Invalid Date')
11+
})
12+
813
describe('colors', () => {
914
it('returns date with red color, if colour is set to true', () => {
1015
expect(inspect(new Date(1475318637123), { colors: true })).to.equal(

0 commit comments

Comments
 (0)