ISSUE-054 - Minor improvements and new tests.

Added limit arg to formatTime().
Wrote unit tests for formatTime().
This commit is contained in:
Kyle Geib committed 2022-02-16 18:58:36 -05:00
1 parent 7507868dce
commit 2a92ee91cb
3 files changed
+72 -8

No files matched your search

@@ -3,6 +3,7 @@ import { broadcastGraphicsSelector } from "../../../../features/broadcast/graphi
import { eventSelector } from "../../../../features/event/eventSelector"; import { eventSelector } from "../../../../features/event/eventSelector";
import { BGTimingTowerModes, Car, CarStatus } from "../../../../types/state"; import { BGTimingTowerModes, Car, CarStatus } from "../../../../types/state";
import { Fraction } from "../../../../types/style";
import { orMatch } from "../../../../utils/common"; import { orMatch } from "../../../../utils/common";
import { timeDiff } from "../../../../utils/event"; import { timeDiff } from "../../../../utils/event";
import { formatTime } from "../../../../utils/styling"; import { formatTime } from "../../../../utils/styling";
@@ -49,16 +50,16 @@ export function TimingTower({
index: number, index: number,
count: number, count: number,
) => { ) => {
const firstRow = index === 0;
const lastRow = index + 1 === count; const lastRow = index + 1 === count;
let rightHalfContent = "";
let xScale: Fraction = 1.0;
let leader = false; let leader = false;
if (forwardCarDistance === -1) { if (forwardCarDistance === -1) {
leader = true; leader = true;
forwardCarDistance = car.distance; forwardCarDistance = car.distance;
} }
let rightHalfContent = "";
let xScale = 1.0;
if (leader) { if (leader) {
rightHalfContent = timingBoard.timingTower.mode === BGTimingTowerModes.Leader rightHalfContent = timingBoard.timingTower.mode === BGTimingTowerModes.Leader
? "Leader" : timingBoard.timingTower.mode === BGTimingTowerModes.Interval ? "Leader" : timingBoard.timingTower.mode === BGTimingTowerModes.Interval
@@ -69,7 +70,7 @@ export function TimingTower({
rightHalfContent = `+${formatTime(timeDiff( rightHalfContent = `+${formatTime(timeDiff(
forwardCarDistance, forwardCarDistance,
car.distance, car.distance,
))}`; ), 60)}`;
xScale = rightHalfContent.includes(".") && rightHalfContent.length > 7 ? 0.75 : 0.9; xScale = rightHalfContent.includes(".") && rightHalfContent.length > 7 ? 0.75 : 0.9;
} }
@@ -80,7 +81,7 @@ export function TimingTower({
return ( return (
<RowContainer <RowContainer
key={car.driver.id} key={car.driver.id}
topGap={car.status === CarStatus.Retired && index === 0} topGap={car.status === CarStatus.Retired && firstRow}
retired={car.status === CarStatus.Retired} retired={car.status === CarStatus.Retired}
wide={timingBoard.timingTower.mode !== BGTimingTowerModes.Minimum} wide={timingBoard.timingTower.mode !== BGTimingTowerModes.Minimum}
> >
+53
View File
@@ -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
View File
@@ -2,6 +2,7 @@ import { css }from "styled-components";
import { clamp } from "lodash"; import { clamp } from "lodash";
import { Corner, Fraction, Placement, Px, TransitionArgs } from "../types/style"; import { Corner, Fraction, Placement, Px, TransitionArgs } from "../types/style";
import { Seconds } from "../types/util";
export function placementStyleRules(pos: Placement) { export function placementStyleRules(pos: Placement) {
return css` 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)); return String(time.toFixed(3));
} }
const minutes = Math.floor(time / 60); const minutes = Math.floor(time / 60);
const seconds = time % 60; const seconds = time % 60;
return `${minutes}:${String(seconds.toFixed(3)).padStart(6, "0")}`; return `${minutes}:${seconds.toFixed(3).padStart(6, "0")}`;
} }