Skip to content

Commit 054044c

Browse files
feat: Add option to use short number format (DenverCoder1#745)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 14aaadd commit 054044c

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/card.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,29 @@ function getCardHeight(array $params): int
335335
return max($minimumHeight, intval($params["card_height"] ?? $defaultHeight));
336336
}
337337

338+
/**
339+
* Format number using locale and short number if requested
340+
*
341+
* @param float $num The number to format
342+
* @param string $localeCode Locale code
343+
* @param bool $useShortNumbers Whether to use short numbers
344+
* @return string The formatted number
345+
*/
346+
function formatNumber(float $num, string $localeCode, bool $useShortNumbers): string
347+
{
348+
$numFormatter = new NumberFormatter($localeCode, NumberFormatter::DECIMAL);
349+
$suffix = "";
350+
if ($useShortNumbers) {
351+
$units = ["", "K", "M", "B", "T"];
352+
for ($i = 0; $num >= 1000; $i++) {
353+
$num /= 1000;
354+
}
355+
$suffix = $units[$i];
356+
$num = round($num, 1);
357+
}
358+
return $numFormatter->format($num) . $suffix;
359+
}
360+
338361
/**
339362
* Generate SVG output for a stats array
340363
*
@@ -362,9 +385,6 @@ function generateCard(array $stats, array $params = null): string
362385
// locale date formatter (used only if date_format is not specified)
363386
$dateFormat = $params["date_format"] ?? ($localeTranslations["date_format"] ?? null);
364387

365-
// number formatter
366-
$numFormatter = new NumberFormatter($localeCode, NumberFormatter::DECIMAL);
367-
368388
// read border_radius parameter, default to 4.5 if not set
369389
$borderRadius = $params["border_radius"] ?? 4.5;
370390

@@ -417,13 +437,15 @@ function generateCard(array $stats, array $params = null): string
417437
19.5 + $heightOffset,
418438
];
419439

440+
$useShortNumbers = ($params["short_numbers"] ?? "") === "true";
441+
420442
// total contributions
421-
$totalContributions = $numFormatter->format($stats["totalContributions"]);
443+
$totalContributions = formatNumber($stats["totalContributions"], $localeCode, $useShortNumbers);
422444
$firstContribution = formatDate($stats["firstContribution"], $dateFormat, $localeCode);
423445
$totalContributionsRange = $firstContribution . " - " . $localeTranslations["Present"];
424446

425447
// current streak
426-
$currentStreak = $numFormatter->format($stats["currentStreak"]["length"]);
448+
$currentStreak = formatNumber($stats["currentStreak"]["length"], $localeCode, $useShortNumbers);
427449
$currentStreakStart = formatDate($stats["currentStreak"]["start"], $dateFormat, $localeCode);
428450
$currentStreakEnd = formatDate($stats["currentStreak"]["end"], $dateFormat, $localeCode);
429451
$currentStreakRange = $currentStreakStart;
@@ -432,7 +454,7 @@ function generateCard(array $stats, array $params = null): string
432454
}
433455

434456
// longest streak
435-
$longestStreak = $numFormatter->format($stats["longestStreak"]["length"]);
457+
$longestStreak = formatNumber($stats["longestStreak"]["length"], $localeCode, $useShortNumbers);
436458
$longestStreakStart = formatDate($stats["longestStreak"]["start"], $dateFormat, $localeCode);
437459
$longestStreakEnd = formatDate($stats["longestStreak"]["end"], $dateFormat, $localeCode);
438460
$longestStreakRange = $longestStreakStart;

src/demo/index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ function gtag() {
120120
<?php endforeach; ?>
121121
</select>
122122

123+
<label for="short-numbers">Short Numbers</label>
124+
<select class="param" id="short-numbers" name="short_numbers">
125+
<option>false</option>
126+
<option>true</option>
127+
</select>
128+
123129
<label for="date-format">Date Format</label>
124130
<select class="param" id="date-format" name="date_format">
125131
<option value="">default</option>
@@ -279,4 +285,4 @@ function gtag() {
279285
</a>
280286
</body>
281287

282-
</html>
288+
</html>

src/demo/js/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const preview = {
1919
hide_total_contributions: "false",
2020
hide_current_streak: "false",
2121
hide_longest_streak: "false",
22+
short_numbers: "false",
2223
},
2324

2425
/**

tests/RenderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function testCardRender(): void
4949
$render = generateCard($this->testStats, $this->testParams);
5050
$expected = file_get_contents("tests/expected/test_card.svg");
5151
$this->assertEquals($expected, $render);
52+
53+
// Test short_numbers parameter
54+
$this->testParams["short_numbers"] = "true";
55+
$render = generateCard($this->testStats, $this->testParams);
56+
$this->assertStringContainsString("2K", $render);
5257
}
5358

5459
/**

0 commit comments

Comments
 (0)