From eebf7baae64ff281e172cc2da53728497dbf8541 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Tue, 10 Dec 2024 11:41:24 +0200 Subject: [PATCH 1/4] Add the option to use short form for the Total Contributions count Add the option to display the Total Contributions count in short form. * Add a new parameter `short_total_contributions` to the `generateCard` function in `src/card.php` with a default value of `false`. * Implement a `shortNumber` function in `src/card.php` to convert large numbers into short form. * Update the `generateCard` function in `src/card.php` to use the `shortNumber` function if `short_total_contributions` is `true`. * Use the `shortNumber` function for current streak and longest streak if `short_total_contributions` is `true`. * Add a new input field for `short_total_contributions` in the form in `src/demo/index.php`. * Add a test case for the new `short_total_contributions` parameter in `tests/RenderTest.php`. --- src/card.php | 27 ++++++++++++++++++++++++--- src/demo/index.php | 8 +++++++- tests/RenderTest.php | 5 +++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/card.php b/src/card.php index 3e4cf61d..b0922631 100644 --- a/src/card.php +++ b/src/card.php @@ -335,6 +335,21 @@ function getCardHeight(array $params): int return max($minimumHeight, intval($params["card_height"] ?? $defaultHeight)); } +/** + * Convert large numbers into short form + * + * @param float $num The number to convert + * @return string The number in short form + */ +function shortNumber(float $num): string +{ + $units = ['', 'K', 'M', 'B', 'T']; + for ($i = 0; $num >= 1000; $i++) { + $num /= 1000; + } + return round($num, 1) . $units[$i]; +} + /** * Generate SVG output for a stats array * @@ -418,12 +433,16 @@ function generateCard(array $stats, array $params = null): string ]; // total contributions - $totalContributions = $numFormatter->format($stats["totalContributions"]); + $totalContributions = $params["short_total_contributions"] === "true" + ? shortNumber($stats["totalContributions"]) + : $numFormatter->format($stats["totalContributions"]); $firstContribution = formatDate($stats["firstContribution"], $dateFormat, $localeCode); $totalContributionsRange = $firstContribution . " - " . $localeTranslations["Present"]; // current streak - $currentStreak = $numFormatter->format($stats["currentStreak"]["length"]); + $currentStreak = $params["short_total_contributions"] === "true" + ? shortNumber($stats["currentStreak"]["length"]) + : $numFormatter->format($stats["currentStreak"]["length"]); $currentStreakStart = formatDate($stats["currentStreak"]["start"], $dateFormat, $localeCode); $currentStreakEnd = formatDate($stats["currentStreak"]["end"], $dateFormat, $localeCode); $currentStreakRange = $currentStreakStart; @@ -432,7 +451,9 @@ function generateCard(array $stats, array $params = null): string } // longest streak - $longestStreak = $numFormatter->format($stats["longestStreak"]["length"]); + $longestStreak = $params["short_total_contributions"] === "true" + ? shortNumber($stats["longestStreak"]["length"]) + : $numFormatter->format($stats["longestStreak"]["length"]); $longestStreakStart = formatDate($stats["longestStreak"]["start"], $dateFormat, $localeCode); $longestStreakEnd = formatDate($stats["longestStreak"]["end"], $dateFormat, $localeCode); $longestStreakRange = $longestStreakStart; diff --git a/src/demo/index.php b/src/demo/index.php index 4a0df44f..ed68ee2f 100644 --- a/src/demo/index.php +++ b/src/demo/index.php @@ -173,6 +173,12 @@ function gtag() { + + + + + + - - -