mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-25 05:14:36 +00:00
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:
committed by
GitHub
parent
a2bb059c81
commit
a4209912a7
@@ -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".
|
||||
|
||||
@@ -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) + "%";
|
||||
}
|
||||
|
||||
@@ -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) + "%";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user