Skip to content

Commit

Permalink
fix: invalid Date object will throw a TypeError (#58) (#59)
Browse files Browse the repository at this point in the history
* fix: Invalid Date object will throw a TypeError (#58)

* fixed lint error: unnecessary 'else' after 'return'
  • Loading branch information
dotnetCarpenter authored Nov 5, 2022
1 parent b4ee644 commit 3747fa8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/date.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { truncate } from './helpers'

export default function inspectDate(dateObject, options) {
// If we need to - truncate the time portion, but never the date
const split = dateObject.toJSON().split('T')
const stringRepresentation = dateObject.toJSON()

if (stringRepresentation === null) {
return 'Invalid Date'
}

const split = stringRepresentation.split('T')
const date = split[0]
// If we need to - truncate the time portion, but never the date
return options.stylize(`${date}T${truncate(split[1], options.truncate - date.length - 1)}`, 'date')
}
5 changes: 5 additions & 0 deletions test/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ describe('date', () => {
expect(inspect(new Date(1475318637123))).to.equal('2016-10-01T10:43:57.123Z')
})

it('returns "Invalid Date" if given an invalid Date object', () => {
// See: https://github.com/chaijs/loupe/issues/58
expect(inspect(new Date('not a date'))).to.equal('Invalid Date')
})

describe('colors', () => {
it('returns date with red color, if colour is set to true', () => {
expect(inspect(new Date(1475318637123), { colors: true })).to.equal(
Expand Down

0 comments on commit 3747fa8

Please sign in to comment.