From e68bcdd34c24c3259d0bff4d01929390ee510a28 Mon Sep 17 00:00:00 2001 From: Alec M Date: Thu, 5 Dec 2024 13:48:15 -0500 Subject: [PATCH] Migrate to DayJs for seconds parsing --- src/utils/dateUtils.test.ts | 17 +++++++++++++++++ src/utils/dateUtils.ts | 12 ++++++++++++ src/utils/stringUtils.test.ts | 16 ---------------- src/utils/stringUtils.ts | 10 ---------- 4 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 src/utils/dateUtils.test.ts diff --git a/src/utils/dateUtils.test.ts b/src/utils/dateUtils.test.ts new file mode 100644 index 000000000..3e460d3fb --- /dev/null +++ b/src/utils/dateUtils.test.ts @@ -0,0 +1,17 @@ +import * as utils from "./dateUtils"; + +describe("secondsToMinuteString utility function", () => { + it.each<[number, string]>([ + [0, "00:00"], + [59, "00:59"], + [60, "01:00"], + [61, "01:01"], + [119, "01:59"], + [120, "02:00"], + [121, "02:01"], + [300, "05:00"], + [3599, "59:59"], + ])("should handle format %p seconds to %p", (seconds, expected) => { + expect(utils.secondsToMinuteString(seconds)).toBe(expected); + }); +}); diff --git a/src/utils/dateUtils.ts b/src/utils/dateUtils.ts index 2209023da..fc9ea5a08 100644 --- a/src/utils/dateUtils.ts +++ b/src/utils/dateUtils.ts @@ -1,9 +1,11 @@ import dayjs from "dayjs"; import utc from "dayjs/plugin/utc"; import timezone from "dayjs/plugin/timezone"; +import duration from "dayjs/plugin/duration"; dayjs.extend(utc); dayjs.extend(timezone); +dayjs.extend(duration); /** * Format a date string to a specified pattern @@ -32,3 +34,13 @@ export const FormatDate = ( return fallbackValue; } }; + +/** + * Parses a number of seconds into a string formatted as "mm:ss". + * + * @note Supports up to 59 minutes and 59 seconds. + * @param seconds The number of seconds to convert. + * @returns A string formatted as "mm:ss". + */ +export const secondsToMinuteString = (seconds: number) => + dayjs.duration(seconds, "seconds").format("mm:ss"); diff --git a/src/utils/stringUtils.test.ts b/src/utils/stringUtils.test.ts index c22cc9b9e..62e928439 100644 --- a/src/utils/stringUtils.test.ts +++ b/src/utils/stringUtils.test.ts @@ -486,19 +486,3 @@ describe("formatName utility function", () => { expect(utils.formatName(123 as unknown as string, 456 as unknown as string)).toBe(""); }); }); - -describe("secondsToMinuteString utility function", () => { - it.each<[number, string]>([ - [0, "00:00"], - [59, "00:59"], - [60, "01:00"], - [61, "01:01"], - [119, "01:59"], - [120, "02:00"], - [121, "02:01"], - [300, "05:00"], - [3599, "59:59"], - ])("should handle format %p seconds to %p", (seconds, expected) => { - expect(utils.secondsToMinuteString(seconds)).toBe(expected); - }); -}); diff --git a/src/utils/stringUtils.ts b/src/utils/stringUtils.ts index 20f1f35fc..864714508 100644 --- a/src/utils/stringUtils.ts +++ b/src/utils/stringUtils.ts @@ -220,13 +220,3 @@ export const formatName = (firstName?: string, lastName?: string): string => { } return ""; }; - -/** - * Parses a number of seconds into a string formatted as "mm:ss". - * - * @note Supports a maximum of 59 minutes and 59 seconds. - * @param seconds The number of seconds to convert. - * @returns A string formatted as "mm:ss". - */ -export const secondsToMinuteString = (seconds: number) => - new Date(seconds * 1000).toISOString().substring(14, 19);