-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: remove moment dependency * refactor * add tests * small format * rename argument
- Loading branch information
1 parent
014ba07
commit 6e34225
Showing
5 changed files
with
109 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
divisorDay, | ||
divisorHour, | ||
divisorMinute, | ||
divisorSecond, | ||
formatTime, | ||
} from "./time"; | ||
|
||
describe("formatTime", () => { | ||
it("should format day hour minute and second", () => { | ||
const uptimeInSeconds = 3661; | ||
|
||
const formattedTime = formatTime(uptimeInSeconds, [ | ||
{ unit: "d", divisor: divisorDay }, | ||
{ unit: "h", divisor: divisorHour }, | ||
{ unit: "m", divisor: divisorMinute }, | ||
{ unit: "s", divisor: divisorSecond }, | ||
]); | ||
|
||
expect(formattedTime).toBe("0d 01:01:01"); | ||
}); | ||
|
||
it("should format multiple digits of days", () => { | ||
const uptimeInSeconds = 50203661; | ||
|
||
const formattedTime = formatTime(uptimeInSeconds, [ | ||
{ unit: "d", divisor: divisorDay }, | ||
{ unit: "h", divisor: divisorHour }, | ||
{ unit: "m", divisor: divisorMinute }, | ||
{ unit: "s", divisor: divisorSecond }, | ||
]); | ||
|
||
expect(formattedTime).toBe("581d 25:27:41"); | ||
}); | ||
|
||
it("should format time day hour minute", () => { | ||
const uptimeInSeconds = 3661; | ||
|
||
const formattedTime = formatTime(uptimeInSeconds, [ | ||
{ unit: "d", divisor: divisorDay }, | ||
{ unit: "h", divisor: divisorHour }, | ||
{ unit: "m", divisor: divisorMinute }, | ||
]); | ||
|
||
expect(formattedTime).toBe("0d 01:01"); | ||
}); | ||
|
||
it("should format zero uptime", () => { | ||
const uptimeInSeconds = 0; | ||
|
||
const formattedTime = formatTime(uptimeInSeconds, [ | ||
{ unit: "d", divisor: divisorDay }, | ||
{ unit: "h", divisor: divisorHour }, | ||
{ unit: "m", divisor: divisorMinute }, | ||
{ unit: "s", divisor: divisorSecond }, | ||
]); | ||
|
||
expect(formattedTime).toBe("0d 00:00:00"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface TimeFormat { | ||
unit: string; | ||
divisor: number; | ||
} | ||
|
||
export const divisorDay = 24 * 60 * 60; | ||
export const divisorHour = 60 * 60; | ||
export const divisorMinute = 60; | ||
export const divisorSecond = 1; | ||
|
||
export const formatTime = ( | ||
timeInSeconds: number, | ||
formats: TimeFormat[], | ||
): string => | ||
formats.reduce( | ||
(formattedTime: string, { unit, divisor }: TimeFormat, index: number) => { | ||
const timeValue: number = | ||
index === 0 | ||
? Math.floor(timeInSeconds / divisor) | ||
: Math.floor(timeInSeconds / divisor) % 60; | ||
return ( | ||
formattedTime + | ||
(index === 0 | ||
? `${timeValue}${unit} ` | ||
: `${timeValue.toString().padStart(2, "0")}${index < formats.length - 1 ? ":" : ""}`) | ||
); | ||
}, | ||
"", | ||
); |