Skip to content

Commit

Permalink
short number format
Browse files Browse the repository at this point in the history
  • Loading branch information
DenverCoder1 committed Dec 10, 2024
1 parent a25fb36 commit 5cd233b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
32 changes: 18 additions & 14 deletions src/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,20 @@ function getCardHeight(array $params): int
* Convert large numbers into short form
*
* @param float $num The number to convert
* @param NumberFormatter $numFormatter Number formatter
* @return string The number in short form
*/
function shortNumber(float $num): string
function shortNumber(float $num, NumberFormatter $numFormatter = null): string
{
$units = ["", "K", "M", "B", "T"];
for ($i = 0; $num >= 1000; $i++) {
$num /= 1000;
}
return round($num, 1) . $units[$i];
$numeric = round($num, 1);
if ($numFormatter) {
$numeric = $numFormatter->format($numeric);
}
return $numeric . $units[$i];
}

/**
Expand Down Expand Up @@ -432,19 +437,19 @@ function generateCard(array $stats, array $params = null): string
19.5 + $heightOffset,
];

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

// total contributions
$totalContributions =
$params["short_total_contributions"] === "true"
? shortNumber($stats["totalContributions"])
: $numFormatter->format($stats["totalContributions"]);
$totalContributions = $useShortNumbers
? shortNumber($stats["totalContributions"], $numFormatter)
: $numFormatter->format($stats["totalContributions"]);
$firstContribution = formatDate($stats["firstContribution"], $dateFormat, $localeCode);
$totalContributionsRange = $firstContribution . " - " . $localeTranslations["Present"];

// current streak
$currentStreak =
$params["short_total_contributions"] === "true"
? shortNumber($stats["currentStreak"]["length"])
: $numFormatter->format($stats["currentStreak"]["length"]);
$currentStreak = $useShortNumbers
? shortNumber($stats["currentStreak"]["length"], $numFormatter)
: $numFormatter->format($stats["currentStreak"]["length"]);
$currentStreakStart = formatDate($stats["currentStreak"]["start"], $dateFormat, $localeCode);
$currentStreakEnd = formatDate($stats["currentStreak"]["end"], $dateFormat, $localeCode);
$currentStreakRange = $currentStreakStart;
Expand All @@ -453,10 +458,9 @@ function generateCard(array $stats, array $params = null): string
}

// longest streak
$longestStreak =
$params["short_total_contributions"] === "true"
? shortNumber($stats["longestStreak"]["length"])
: $numFormatter->format($stats["longestStreak"]["length"]);
$longestStreak = $useShortNumbers
? shortNumber($stats["longestStreak"]["length"], $numFormatter)
: $numFormatter->format($stats["longestStreak"]["length"]);
$longestStreakStart = formatDate($stats["longestStreak"]["start"], $dateFormat, $localeCode);
$longestStreakEnd = formatDate($stats["longestStreak"]["end"], $dateFormat, $localeCode);
$longestStreakRange = $longestStreakStart;
Expand Down
12 changes: 6 additions & 6 deletions 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 @@ -173,12 +179,6 @@ function gtag() {
<label for="card-width">Card Height</label>
<input class="param" type="number" id="card-width" name="card_height" placeholder="195" value="195" step="1" min="170" />

<label for="short-total-contributions">Short Total Contributions</label>
<select class="param" id="short-total-contributions" name="short_total_contributions">
<option>false</option>
<option>true</option>
</select>

<label for="type">Output Type</label>
<select class="param" id="type" name="type">
<option value="svg">SVG</option>
Expand Down
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
4 changes: 2 additions & 2 deletions tests/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function testCardRender(): void
$expected = file_get_contents("tests/expected/test_card.svg");
$this->assertEquals($expected, $render);

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

0 comments on commit 5cd233b

Please sign in to comment.