From 2a92ee91cbc0816460e8a12ac9f72ecd1f98e6b9 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Wed, 16 Feb 2022 18:58:36 -0500 Subject: [PATCH] ISSUE-054 - Minor improvements and new tests. Added limit arg to formatTime(). Wrote unit tests for formatTime(). --- .../TimingBoard/TimingTower/TimingTower.tsx | 11 ++-- src/utils/styling.spec.ts | 53 +++++++++++++++++++ src/utils/styling.ts | 16 ++++-- 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/utils/styling.spec.ts diff --git a/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTower.tsx b/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTower.tsx index 7b39bc0..9bbb9df 100644 --- a/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTower.tsx +++ b/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTower.tsx @@ -3,6 +3,7 @@ import { broadcastGraphicsSelector } from "../../../../features/broadcast/graphi import { eventSelector } from "../../../../features/event/eventSelector"; import { BGTimingTowerModes, Car, CarStatus } from "../../../../types/state"; +import { Fraction } from "../../../../types/style"; import { orMatch } from "../../../../utils/common"; import { timeDiff } from "../../../../utils/event"; import { formatTime } from "../../../../utils/styling"; @@ -49,16 +50,16 @@ export function TimingTower({ index: number, count: number, ) => { + const firstRow = index === 0; const lastRow = index + 1 === count; + let rightHalfContent = ""; + let xScale: Fraction = 1.0; let leader = false; if (forwardCarDistance === -1) { leader = true; forwardCarDistance = car.distance; } - - let rightHalfContent = ""; - let xScale = 1.0; if (leader) { rightHalfContent = timingBoard.timingTower.mode === BGTimingTowerModes.Leader ? "Leader" : timingBoard.timingTower.mode === BGTimingTowerModes.Interval @@ -69,7 +70,7 @@ export function TimingTower({ rightHalfContent = `+${formatTime(timeDiff( forwardCarDistance, car.distance, - ))}`; + ), 60)}`; xScale = rightHalfContent.includes(".") && rightHalfContent.length > 7 ? 0.75 : 0.9; } @@ -80,7 +81,7 @@ export function TimingTower({ return ( diff --git a/src/utils/styling.spec.ts b/src/utils/styling.spec.ts new file mode 100644 index 0000000..fb19223 --- /dev/null +++ b/src/utils/styling.spec.ts @@ -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"); + }); + }); + }); +}); diff --git a/src/utils/styling.ts b/src/utils/styling.ts index 3a32db0..5cfe318 100644 --- a/src/utils/styling.ts +++ b/src/utils/styling.ts @@ -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")}`; }