Skip to content

feat: Add option to use short number format #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,29 @@ function getCardHeight(array $params): int
return max($minimumHeight, intval($params["card_height"] ?? $defaultHeight));
}

/**
* Format number using locale and short number if requested
*
* @param float $num The number to format
* @param string $localeCode Locale code
* @param bool $useShortNumbers Whether to use short numbers
* @return string The formatted number
*/
function formatNumber(float $num, string $localeCode, bool $useShortNumbers): string
{
$numFormatter = new NumberFormatter($localeCode, NumberFormatter::DECIMAL);
$suffix = "";
if ($useShortNumbers) {
$units = ["", "K", "M", "B", "T"];
for ($i = 0; $num >= 1000; $i++) {
$num /= 1000;
}
$suffix = $units[$i];
$num = round($num, 1);
}
return $numFormatter->format($num) . $suffix;
}

/**
* Generate SVG output for a stats array
*
Expand Down Expand Up @@ -362,9 +385,6 @@ function generateCard(array $stats, array $params = null): string
// locale date formatter (used only if date_format is not specified)
$dateFormat = $params["date_format"] ?? ($localeTranslations["date_format"] ?? null);

// number formatter
$numFormatter = new NumberFormatter($localeCode, NumberFormatter::DECIMAL);

// read border_radius parameter, default to 4.5 if not set
$borderRadius = $params["border_radius"] ?? 4.5;

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

$useShortNumbers = ($params["short_numbers"] ?? "") === "true";

// total contributions
$totalContributions = $numFormatter->format($stats["totalContributions"]);
$totalContributions = formatNumber($stats["totalContributions"], $localeCode, $useShortNumbers);
$firstContribution = formatDate($stats["firstContribution"], $dateFormat, $localeCode);
$totalContributionsRange = $firstContribution . " - " . $localeTranslations["Present"];

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

// longest streak
$longestStreak = $numFormatter->format($stats["longestStreak"]["length"]);
$longestStreak = formatNumber($stats["longestStreak"]["length"], $localeCode, $useShortNumbers);
$longestStreakStart = formatDate($stats["longestStreak"]["start"], $dateFormat, $localeCode);
$longestStreakEnd = formatDate($stats["longestStreak"]["end"], $dateFormat, $localeCode);
$longestStreakRange = $longestStreakStart;
Expand Down
8 changes: 7 additions & 1 deletion src/demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ function gtag() {
<?php endforeach; ?>
</select>

<label for="short-numbers">Short Numbers</label>
<select class="param" id="short-numbers" name="short_numbers">
<option>false</option>
<option>true</option>
</select>

<label for="date-format">Date Format</label>
<select class="param" id="date-format" name="date_format">
<option value="">default</option>
Expand Down Expand Up @@ -279,4 +285,4 @@ function gtag() {
</a>
</body>

</html>
</html>
1 change: 1 addition & 0 deletions src/demo/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const preview = {
hide_total_contributions: "false",
hide_current_streak: "false",
hide_longest_streak: "false",
short_numbers: "false",
},

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function testCardRender(): void
$render = generateCard($this->testStats, $this->testParams);
$expected = file_get_contents("tests/expected/test_card.svg");
$this->assertEquals($expected, $render);

// Test short_numbers parameter
$this->testParams["short_numbers"] = "true";
$render = generateCard($this->testStats, $this->testParams);
$this->assertStringContainsString("2K", $render);
}

/**
Expand Down
Loading