From a4209912a72581761abefd86b3fefcd410c90c4a Mon Sep 17 00:00:00 2001 From: Mattia Migliorini Date: Sat, 10 Jan 2026 19:01:06 +0100 Subject: [PATCH] Consolidate formatPercentage to Utils (#2852) ## Description: Removes code duplication by consolidating the `formatPercentage` utility function to the `Utils.ts` file, removing the two identical implementations in `TeamStats.ts` and `Leaderboard.ts`. This is an improvement upon #2838 which is the PR that aligned the two mentioned implementations. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: deshack_82603 --- src/client/Utils.ts | 6 ++++++ src/client/graphics/layers/Leaderboard.ts | 8 +------- src/client/graphics/layers/TeamStats.ts | 13 ++++++------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/client/Utils.ts b/src/client/Utils.ts index 63668316c..9bded69e2 100644 --- a/src/client/Utils.ts +++ b/src/client/Utils.ts @@ -61,6 +61,12 @@ export function renderNumber( } } +export function formatPercentage(value: number): string { + const perc = value * 100; + if (Number.isNaN(perc)) return "0%"; + return perc.toFixed(1) + "%"; +} + /** * Formats a keyboard key code for user-friendly display. * Handles empty values, spaces, and normalizes key codes like "Digit1" and "KeyA". diff --git a/src/client/graphics/layers/Leaderboard.ts b/src/client/graphics/layers/Leaderboard.ts index 3204d1e56..5dd8793f3 100644 --- a/src/client/graphics/layers/Leaderboard.ts +++ b/src/client/graphics/layers/Leaderboard.ts @@ -4,7 +4,7 @@ import { repeat } from "lit/directives/repeat.js"; import { renderTroops, translateText } from "../../../client/Utils"; import { EventBus, GameEvent } from "../../../core/EventBus"; import { GameView, PlayerView, UnitView } from "../../../core/game/GameView"; -import { renderNumber } from "../../Utils"; +import { formatPercentage, renderNumber } from "../../Utils"; import { Layer } from "./Layer"; interface Entry { @@ -274,9 +274,3 @@ export class Leaderboard extends LitElement implements Layer { `; } } - -function formatPercentage(value: number): string { - const perc = value * 100; - if (Number.isNaN(perc)) return "0%"; - return perc.toFixed(1) + "%"; -} diff --git a/src/client/graphics/layers/TeamStats.ts b/src/client/graphics/layers/TeamStats.ts index 8e0babec9..1841a0636 100644 --- a/src/client/graphics/layers/TeamStats.ts +++ b/src/client/graphics/layers/TeamStats.ts @@ -3,7 +3,12 @@ import { customElement, property } from "lit/decorators.js"; import { EventBus } from "../../../core/EventBus"; import { GameMode, Team, UnitType } from "../../../core/game/Game"; import { GameView, PlayerView } from "../../../core/game/GameView"; -import { renderNumber, renderTroops, translateText } from "../../Utils"; +import { + formatPercentage, + renderNumber, + renderTroops, + translateText, +} from "../../Utils"; import { Layer } from "./Layer"; interface TeamEntry { @@ -243,9 +248,3 @@ export class TeamStats extends LitElement implements Layer { `; } } - -function formatPercentage(value: number): string { - const perc = value * 100; - if (Number.isNaN(perc)) return "0%"; - return perc.toFixed(1) + "%"; -}