From e746250e908a1a7b4140b8011c0677c449bdae93 Mon Sep 17 00:00:00 2001 From: Kayla Kremer Date: Fri, 23 Jun 2023 12:23:11 -0400 Subject: [PATCH 1/5] fix: add conditional for minutes calculation so that minutes still properly shows as :59 until hour is increased by one --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 570d7f3..9d6c7ce 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,12 @@ const toHHMM = (input) => { 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}`; }; From 6e35f2e0c4e222385fccd1ecaa2125919363d7f5 Mon Sep 17 00:00:00 2001 From: Kayla Kremer Date: Fri, 23 Jun 2023 12:30:44 -0400 Subject: [PATCH 2/5] refactor: remove unnecessary <= --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9d6c7ce..4a2aff0 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,7 @@ const toHHMM = (input) => { total = Math.abs(total); const hours = Math.floor(total / 60); let minutes = total % 60; - if (minutes >= 59.5 && minutes <= 60) { + if (minutes >= 59.5 && minutes < 60) { minutes = Math.floor(minutes); } else { minutes = Math.round(minutes); From 2d579c7123f25662bab9dffaf86a1c9655c92fdf Mon Sep 17 00:00:00 2001 From: Kayla Kremer Date: Fri, 23 Jun 2023 13:50:13 -0400 Subject: [PATCH 3/5] fix: edit source index file, not root index file --- index.js | 7 +------ src/index.ts | 7 ++++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4a2aff0..341c13b 100644 --- a/index.js +++ b/index.js @@ -32,12 +32,7 @@ const toHHMM = (input) => { const sign = total < 0 ? '-' : ''; total = Math.abs(total); const hours = Math.floor(total / 60); - let minutes = total % 60; - if (minutes >= 59.5 && minutes < 60) { - minutes = Math.floor(minutes); - } else { - minutes = Math.round(minutes); - } + const minutes = Math.round(total % 60); const paddedMinutes = minutes.toString().padStart(2, '0'); return `${sign}${hours}:${paddedMinutes}`; }; diff --git a/src/index.ts b/src/index.ts index facfafe..b8a27e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}` From 354b98e3dd2b53d711a8e62c30fdf8d0ad8a410a Mon Sep 17 00:00:00 2001 From: Kayla Kremer Date: Fri, 23 Jun 2023 13:50:23 -0400 Subject: [PATCH 4/5] feat: add test coverage --- src/index.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index 8c8e140..90a1ef9 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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') }) From add41d3c719f406f25a78c8585994b698950b070 Mon Sep 17 00:00:00 2001 From: Kayla Kremer Date: Fri, 23 Jun 2023 13:52:51 -0400 Subject: [PATCH 5/5] chore: restore index.js back to original --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 341c13b..570d7f3 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ const toHHMM = (input) => { const sign = total < 0 ? '-' : ''; total = Math.abs(total); const hours = Math.floor(total / 60); - const minutes = Math.round(total % 60); + const minutes = Math.round(total) % 60; const paddedMinutes = minutes.toString().padStart(2, '0'); return `${sign}${hours}:${paddedMinutes}`; };