Skip to content

Commit

Permalink
Migrate to DayJs for seconds parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
amattu2 committed Dec 5, 2024
1 parent c36a1b0 commit e68bcdd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
17 changes: 17 additions & 0 deletions src/utils/dateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
12 changes: 12 additions & 0 deletions src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
16 changes: 0 additions & 16 deletions src/utils/stringUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
10 changes: 0 additions & 10 deletions src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit e68bcdd

Please sign in to comment.