Skip to content

Commit

Permalink
Return empty string for undefined/null date formatting (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thource authored Jun 7, 2024
1 parent d0cce2e commit 2144063
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions server/formatters/formatDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ describe('formatDate', () => {
expect(formatDate('an invalid date')).toEqual('an invalid date')
})
})

describe('when given undefined', () => {
it('returns ""', () => {
expect(formatDate(undefined)).toEqual('')
})
})

describe('when given null', () => {
it('returns ""', () => {
expect(formatDate(null)).toEqual('')
})
})
})
4 changes: 4 additions & 0 deletions server/formatters/formatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { format, parseISO } from 'date-fns'
export default function formatDate(dateOrString: string | Date) {
const date = typeof dateOrString === 'string' ? parseISO(dateOrString) : dateOrString

if (!date) {
return ''
}

if (Number.isNaN(date.getTime())) {
return dateOrString as string
}
Expand Down
12 changes: 12 additions & 0 deletions server/formatters/formatDateWithTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ describe('formatDateWithTime', () => {
expect(formatDateWithTime('an invalid date')).toEqual('an invalid date')
})
})

describe('when given undefined', () => {
it('returns ""', () => {
expect(formatDateWithTime(undefined)).toEqual('')
})
})

describe('when given null', () => {
it('returns ""', () => {
expect(formatDateWithTime(null)).toEqual('')
})
})
})
4 changes: 4 additions & 0 deletions server/formatters/formatDateWithTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export default function formatDateWithTime(
): string {
const date = typeof dateOrString === 'string' ? parseISO(dateOrString) : dateOrString

if (!date) {
return ''
}

if (Number.isNaN(date.getTime())) {
return dateOrString as string
}
Expand Down
12 changes: 12 additions & 0 deletions server/formatters/formatDaysAgo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ describe('formatTime', () => {
expect(formatDaysAgo('an invalid date')).toEqual('an invalid date')
})
})

describe('when given undefined', () => {
it('returns ""', () => {
expect(formatDaysAgo(undefined)).toEqual('')
})
})

describe('when given null', () => {
it('returns ""', () => {
expect(formatDaysAgo(null)).toEqual('')
})
})
})
4 changes: 4 additions & 0 deletions server/formatters/formatDaysAgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export default function formatDaysAgo(dateOrString: string | Date) {
const date = typeof dateOrString === 'string' ? parseISO(dateOrString) : dateOrString
const today = new Date()

if (!date) {
return ''
}

if (Number.isNaN(date.getTime())) {
return dateOrString as string
}
Expand Down
12 changes: 12 additions & 0 deletions server/formatters/formatTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ describe('formatTime', () => {
expect(formatTime('an invalid date')).toEqual('an invalid date')
})
})

describe('when given undefined', () => {
it('returns ""', () => {
expect(formatTime(undefined)).toEqual('')
})
})

describe('when given null', () => {
it('returns ""', () => {
expect(formatTime(null)).toEqual('')
})
})
})
4 changes: 4 additions & 0 deletions server/formatters/formatTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { format, parseISO } from 'date-fns'
export default function formatTime(dateOrString: string | Date) {
const date = typeof dateOrString === 'string' ? parseISO(dateOrString) : dateOrString

if (!date) {
return ''
}

if (Number.isNaN(date.getTime())) {
return dateOrString as string
}
Expand Down

0 comments on commit 2144063

Please sign in to comment.