ISSUE-054 - Minor improvements and new tests.
Added limit arg to formatTime(). Wrote unit tests for formatTime().
This commit is contained in:
1 parent
7507868dce
commit
2a92ee91cb
3 files changed
+72
-8
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { formatTime } from "./styling";
|
||||
|
||||
describe("styling", () => {
|
||||
describe("formatTime", () => {
|
||||
describe("works correctly with 60 limit", () => {
|
||||
it("should format 65.32210101 to 1:05.322", () => {
|
||||
expect(formatTime(65.32210101, 60)).toBe("1:05.322");
|
||||
});
|
||||
|
||||
it("should format 3.2009101 to 3.201", () => {
|
||||
expect(formatTime(3.2009101, 60)).toBe("3.201");
|
||||
});
|
||||
|
||||
it("should format 50 to 50.000", () => {
|
||||
expect(formatTime(50, 60)).toBe("50.000");
|
||||
});
|
||||
|
||||
it("should format 60 to 1:00.00", () => {
|
||||
expect(formatTime(60, 60)).toBe("1:00.000");
|
||||
});
|
||||
});
|
||||
describe("works correctly with 70 limit", () => {
|
||||
it("should format 65.32210101 to 65.322", () => {
|
||||
expect(formatTime(65.32210101, 70)).toBe("65.322");
|
||||
});
|
||||
|
||||
it("should format 73.2009101 to 1:13.201", () => {
|
||||
expect(formatTime(73.2009101, 70)).toBe("1:13.201");
|
||||
});
|
||||
|
||||
it("should format 50 to 50.000", () => {
|
||||
expect(formatTime(50, 70)).toBe("50.000");
|
||||
});
|
||||
|
||||
it("should format 70 to 1:10.00", () => {
|
||||
expect(formatTime(70, 70)).toBe("1:10.000");
|
||||
});
|
||||
});
|
||||
describe("works correctly with 50 limit", () => {
|
||||
it("should format 65.32210101 to 1:05.322", () => {
|
||||
expect(formatTime(65.32210101, 50)).toBe("1:05.322");
|
||||
});
|
||||
|
||||
it("should format 73.2009101 to 1:13.201", () => {
|
||||
expect(formatTime(73.2009101, 50)).toBe("1:13.201");
|
||||
});
|
||||
|
||||
it("should format 50 to 0:50.00", () => {
|
||||
expect(formatTime(50, 50)).toBe("0:50.000");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
+13
-3
@@ -2,6 +2,7 @@ import { css }from "styled-components";
|
||||
import { clamp } from "lodash";
|
||||
|
||||
import { Corner, Fraction, Placement, Px, TransitionArgs } from "../types/style";
|
||||
import { Seconds } from "../types/util";
|
||||
|
||||
export function placementStyleRules(pos: Placement) {
|
||||
return css`
|
||||
@@ -129,11 +130,20 @@ export function outlineClipPath(
|
||||
}
|
||||
}
|
||||
|
||||
export function formatTime(time: number) {
|
||||
if (time < 60) {
|
||||
/**
|
||||
* Converts amount of seconds that are over the limit to become a new format:
|
||||
* If limit is 60, then 65.322 will become "1:05.322".
|
||||
* You can set the limit to other than 60 to change the time when the new format
|
||||
* is used.
|
||||
* @param time Time to format given in seconds
|
||||
* @param limit Bounds (exclusive) that will keep the format in seconds.
|
||||
* @returns
|
||||
*/
|
||||
export function formatTime(time: Seconds, limit: Seconds) {
|
||||
if (time < limit) {
|
||||
return String(time.toFixed(3));
|
||||
}
|
||||
const minutes = Math.floor(time / 60);
|
||||
const seconds = time % 60;
|
||||
return `${minutes}:${String(seconds.toFixed(3)).padStart(6, "0")}`;
|
||||
return `${minutes}:${seconds.toFixed(3).padStart(6, "0")}`;
|
||||
}
|
||||
Reference in new issue
Block a user