Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minute calculation for a running timer #10

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('toHHMM', () => {
expect(toHHMM(0.01)).toEqual('0:01')
})

test('handles time ended in :59 correctly', () => {
expect(toHHMM(0.999)).toEqual('0:59')
})

test('handles time ended in :00 correctly', () => {
expect(toHHMM(1)).toEqual('1:00')
})

test('handles leading and trailing space', () => {
expect(toHHMM(' -1:30 + 1:44 ')).toEqual('0:14')
})
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export const toHHMM = (input?: number | string): string => {
const sign = total < 0 ? '-' : ''
total = Math.abs(total)
const hours = Math.floor(total / 60)
const minutes = Math.round(total) % 60
let minutes = total % 60
if (minutes >= 59.5 && minutes < 60) {
minutes = Math.floor(minutes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, we could just hardcode 59 here since it's a known value.

} else {
minutes = Math.round(minutes)
}
const paddedMinutes = minutes.toString().padStart(2, '0')

return `${sign}${hours}:${paddedMinutes}`
Expand Down