Skip to content

Commit

Permalink
Merge pull request #10 from KaylaKremer/fix-toHHMM-minute-calculation
Browse files Browse the repository at this point in the history
Fix minute calculation for a running timer
  • Loading branch information
kaylakremer authored Jun 26, 2023
2 parents b88385d + add41d3 commit e23d6ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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)
} else {
minutes = Math.round(minutes)
}
const paddedMinutes = minutes.toString().padStart(2, '0')

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

0 comments on commit e23d6ad

Please sign in to comment.