From 7280820ba517d79da5bbeef5e5bc599c9b21825f Mon Sep 17 00:00:00 2001 From: Chris Nielsen Date: Thu, 8 Jun 2023 23:16:41 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A9=B9=20Singular/plural=20forms=20fo?= =?UTF-8?q?r=20time=20counts.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/MiscHelpers.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/utils/MiscHelpers.js b/src/utils/MiscHelpers.js index 8b3719c0..91b4f2cf 100644 --- a/src/utils/MiscHelpers.js +++ b/src/utils/MiscHelpers.js @@ -141,12 +141,26 @@ export const getTimeDifference = (startTime, endTime) => { const msDifference = new Date(endTime).getTime() - new Date(startTime).getTime(); const diff = Math.abs(Math.round(msDifference / 1000)); const divide = (time, round) => Math.round(time / round); - if (diff < 60) return `${divide(diff, 1)} seconds`; - if (diff < 3600) return `${divide(diff, 60)} minutes`; - if (diff < 86400) return `${divide(diff, 3600)} hours`; - if (diff < 604800) return `${divide(diff, 86400)} days`; - if (diff < 31557600) return `${divide(diff, 604800)} weeks`; - if (diff >= 31557600) return `${divide(diff, 31557600)} years`; + + const periods = [ + { noun: 'second', value: 1 }, + { noun: 'minute', value: 60 }, + { noun: 'hour', value: 3600 }, + { noun: 'day', value: 86400 }, + { noun: 'week', value: 604800 }, + { noun: 'fortnight', value: 1209600 }, + { noun: 'year', value: 31557600 }, + ]; + + for (let idx = 0; idx < periods.length; idx += 1) { + if (diff < periods[idx + 1]?.value ?? Infinity) { + const period = periods[idx]; + const value = divide(diff, period.value); + const noun = value === 1 ? period.noun : `${period.noun}s`; + return `${value} ${noun}`; + } + } + return 'unknown'; }; From f46bcc2eddde41c4a5da006e991538509b179037 Mon Sep 17 00:00:00 2001 From: Chris Nielsen Date: Thu, 8 Jun 2023 23:37:57 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20Correct=20DeepScan=20issue.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/MiscHelpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/MiscHelpers.js b/src/utils/MiscHelpers.js index 91b4f2cf..b3bbbd36 100644 --- a/src/utils/MiscHelpers.js +++ b/src/utils/MiscHelpers.js @@ -153,7 +153,7 @@ export const getTimeDifference = (startTime, endTime) => { ]; for (let idx = 0; idx < periods.length; idx += 1) { - if (diff < periods[idx + 1]?.value ?? Infinity) { + if (diff < (periods[idx + 1]?.value ?? Infinity)) { const period = periods[idx]; const value = divide(diff, period.value); const noun = value === 1 ? period.noun : `${period.noun}s`; From 03ae38577ee92f645a70ae7ae98d77973636f4eb Mon Sep 17 00:00:00 2001 From: Chris Nielsen Date: Thu, 8 Jun 2023 23:39:20 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8=20Support=20for=20`month`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/MiscHelpers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/MiscHelpers.js b/src/utils/MiscHelpers.js index b3bbbd36..dc8863ff 100644 --- a/src/utils/MiscHelpers.js +++ b/src/utils/MiscHelpers.js @@ -149,6 +149,7 @@ export const getTimeDifference = (startTime, endTime) => { { noun: 'day', value: 86400 }, { noun: 'week', value: 604800 }, { noun: 'fortnight', value: 1209600 }, + { noun: 'month', value: 2628000 }, { noun: 'year', value: 31557600 }, ];