Skip to content

Commit

Permalink
revert date logic back to original state
Browse files Browse the repository at this point in the history
  • Loading branch information
ttran-Therisa committed Oct 8, 2024
1 parent abee2f8 commit 141d2e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
25 changes: 6 additions & 19 deletions src/lib/utils/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,30 +246,17 @@ describe('formatDateObject', () => {
})

describe('deriveMostRecentDate', () => {
it('should return the closest ongoing or future event', () => {
const mockCurrentTime = new Date('2023-08-01T18:00:00Z').getTime()
it('should return the closest future event', () => {
const mockCurrentTime = new Date('2023-08-01T12:00:00Z').getTime()
jest.spyOn(Date, 'now').mockImplementation(() => mockCurrentTime)

const datetimeRange = [
{
value: new Date('2023-07-01T14:00:00Z').getTime() / 1000,
endValue: new Date('2023-07-01T16:00:00Z').getTime() / 1000,
}, // Past event
{
value: new Date('2023-08-01T14:00:00Z').getTime() / 1000,
endValue: new Date('2023-08-01T19:00:00Z').getTime() / 1000,
},
{
value: new Date('2023-09-01T14:00:00Z').getTime() / 1000,
endValue: new Date('2023-09-01T16:00:00Z').getTime() / 1000,
}, // Closest future event
{
value: new Date('2023-10-01T14:00:00Z').getTime() / 1000,
endValue: new Date('2023-10-01T16:00:00Z').getTime() / 1000,
}, // Later future event
{ value: new Date('2023-07-01T14:00:00Z').getTime() / 1000 }, // Past event
{ value: new Date('2023-09-01T14:00:00Z').getTime() / 1000 }, // Closest future event
{ value: new Date('2023-10-01T14:00:00Z').getTime() / 1000 }, // Later future event
]

const closestEvent = deriveMostRecentDate(datetimeRange)
const result = new Date(closestEvent.value * 1000)
expect(closestEvent.value).toEqual(datetimeRange[1].value)

jest.restoreAllMocks()
Expand Down
12 changes: 6 additions & 6 deletions src/lib/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ export const deriveMostRecentDate = (
) => {
const currentTime = Math.floor(Date.now() / 1000)

// Filter for ongoing and future events
const ongoingAndFutureEvents = datetimeRange.filter((event) => {
return event.endValue > currentTime
})
// Filter for future events
const futureEvents = datetimeRange.filter(
(event) => event.value > currentTime
)

// If there are future events, return closest event
if (ongoingAndFutureEvents.length > 0) {
return ongoingAndFutureEvents.reduce((closest, current) => {
if (futureEvents.length > 0) {
return futureEvents.reduce((closest, current) => {
return closest.value < current.value ? closest : current
})
}
Expand Down

0 comments on commit 141d2e7

Please sign in to comment.