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
This commit is contained in:
Mattia Migliorini
2026-01-10 19:01:06 +01:00
committed by GitHub
parent a2bb059c81
commit a4209912a7
3 changed files with 13 additions and 14 deletions
+6
View File
@@ -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".
+1 -7
View File
@@ -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) + "%";
}
+6 -7
View File
@@ -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) + "%";
}