-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add times under review buttons
- Loading branch information
1 parent
9d67671
commit 911d2dc
Showing
5 changed files
with
208 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { vi, it, beforeEach, afterEach, expect } from 'vitest'; | ||
import { formatTimeDifference } from './util'; | ||
|
||
const mockNow = new Date(); | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
vi.setSystemTime(mockNow); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it('should return "1 min" for exactly one minute or less than that', () => { | ||
const exactlyOneDate = new Date(mockNow.getTime() + 60 * 1000); | ||
expect(formatTimeDifference(exactlyOneDate)).toBe('1 min'); | ||
|
||
const lessThanOneDate = new Date(mockNow.getTime() + 30 * 1000); | ||
expect(formatTimeDifference(lessThanOneDate)).toBe('1 min'); | ||
}); | ||
|
||
it.each([ | ||
[5 * 60 * 1000, '5 mins', 'multiple minutes'], | ||
[60 * 60 * 1000, '1 hour', 'exactly one hour'], | ||
[3 * 60 * 60 * 1000, '3 hours', 'multiple hours'], | ||
[25 * 60 * 60 * 1000, '1 day', 'exactly one day'], | ||
[4 * 25 * 60 * 60 * 1000, '4 days', 'multiple days'], | ||
[7 * 25 * 60 * 60 * 1000, '1 week', 'exactly one week'], | ||
[3 * 7 * 25 * 60 * 60 * 1000, '3 weeks', 'multiple weeks'], | ||
[30 * 25 * 60 * 60 * 1000, '1 month', 'approximately one month'], | ||
[3 * 30 * 25 * 60 * 60 * 1000, '3 months', 'multiple months'], | ||
[365 * 25 * 60 * 60 * 1000, '1 year', 'approximately one year'], | ||
[2 * 365 * 25 * 60 * 60 * 1000, '2 years', 'multiple years'], | ||
])('should return "%s" for %s (%i ms)', (milliseconds, expected) => { | ||
const futureDate = new Date(mockNow.getTime() + milliseconds); | ||
expect(formatTimeDifference(futureDate)).toBe(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function formatTimeDifference(futureDate: Date): string { | ||
const now = new Date(); | ||
const differenceInMillis = futureDate.getTime() - now.getTime(); | ||
const seconds = Math.floor(differenceInMillis / 1000); | ||
const minutes = Math.floor(seconds / 60); | ||
const hours = Math.floor(minutes / 60); | ||
const days = Math.floor(hours / 24); | ||
const weeks = Math.floor(days / 7); | ||
const months = Math.floor(days / 30.44); | ||
const years = Math.floor(days / 365.25); | ||
|
||
if (years > 0) { | ||
return `${years} ${years === 1 ? 'year' : 'years'}`; | ||
} else if (months > 0) { | ||
return `${months} ${months === 1 ? 'month' : 'months'}`; | ||
} else if (weeks > 0) { | ||
return `${weeks} ${weeks === 1 ? 'week' : 'weeks'}`; | ||
} else if (days > 0) { | ||
return `${days} ${days === 1 ? 'day' : 'days'}`; | ||
} else if (hours > 0) { | ||
return `${hours} ${hours === 1 ? 'hour' : 'hours'}`; | ||
} else { | ||
const printedMinutes = Math.max(1, minutes); | ||
return `${printedMinutes} ${printedMinutes === 1 ? 'min' : 'mins'}`; | ||
} | ||
} |