Feat: Abbreviation (#88)

* Feat: Abbreviation

---------

Co-authored-by: Anton Komarev
This commit is contained in:
Omar Trkzi
2024-01-07 06:40:12 +01:00
committed by GitHub
parent 8c49635ac2
commit 1c1f245266
4 changed files with 47 additions and 4 deletions

View File

@@ -117,6 +117,16 @@ to ensure the 1000 views are accounted for:
![](https://komarev.com/ghpvc/?username=your-github-username&base=1000)
```
### Abbreviation
You can set the `abbreviated` parameter to `true` if you would like the counter to be abbreviated.
For example, a counter with 12345 views, will be displayed as 12K.
```markdown
![](https://komarev.com/ghpvc/?username=your-github-username&abbreviated=true)
```
## FAQ
### Can I see detailed statistics?

View File

@@ -36,6 +36,7 @@ $isGitHubUserAgent = strpos($request->userAgent(), 'github-camo') === 0;
$badgeLabel = $request->badgeLabel() ?? 'Profile views';
$badgeMessageBackgroundFill = $request->badgeColor() ?? 'blue';
$baseCount = $request->baseCount() ?? '0';
$isCountAbbreviated = $request->isCountAbbreviated();
$badgeStyle = $request->badgeStyle() ?? 'flat';
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'], true)) {
$badgeStyle = 'flat';
@@ -72,6 +73,7 @@ try {
$count,
$badgeMessageBackgroundFill,
$badgeStyle,
$isCountAbbreviated,
);
}
} catch (InvalidPathException $exception) {

View File

@@ -24,6 +24,8 @@ final class BadgeImageRendererService
{
private Poser $poser;
private const ABBREVIATIONS = ['', 'K', 'M', 'B', 'T', 'Qa', 'Qi'];
public function __construct()
{
$this->poser = new Poser([
@@ -38,9 +40,10 @@ final class BadgeImageRendererService
string $label,
Count $count,
string $messageBackgroundFill,
string $badgeStyle
string $badgeStyle,
bool $isCountAbbreviated
): string {
$message = $this->formatNumber($count->toInt());
$message = $this->formatNumber($count->toInt(), $isCountAbbreviated);
return $this->renderBadge(
$label,
@@ -90,11 +93,29 @@ final class BadgeImageRendererService
* method has big integer format limitation.
*/
private function formatNumber(
int $number
int $number,
bool $isCountAbbreviated
): string {
if ($isCountAbbreviated) {
return $this->formatAbbreviatedNumber($number);
}
$reversedString = strrev(strval($number));
$formattedNumber = implode(',', str_split($reversedString, 3));
return strrev($formattedNumber);
}
public function formatAbbreviatedNumber(
int $number
): string {
$abbreviationIndex = 0;
while ($number >= 1000) {
$number /= 1000;
$abbreviationIndex++;
}
return round($number, 1) . self::ABBREVIATIONS[$abbreviationIndex];
}
}

View File

@@ -27,13 +27,16 @@ final class Request
private ?string $baseCount;
private bool $isCountAbbreviated;
public function __construct(
string $userAgent,
string $username,
?string $badgeLabel,
?string $badgeColor,
?string $badgeStyle,
?string $baseCount
?string $baseCount,
bool $isCountAbbreviated
) {
$this->userAgent = $userAgent;
$this->username = $username;
@@ -41,6 +44,7 @@ final class Request
$this->badgeColor = $badgeColor;
$this->badgeStyle = $badgeStyle;
$this->baseCount = $baseCount;
$this->isCountAbbreviated = $isCountAbbreviated;
}
public static function of(
@@ -58,6 +62,7 @@ final class Request
$get['color'] ?? null,
$get['style'] ?? null,
$get['base'] ?? null,
boolval($get['abbreviated'] ?? false),
);
}
@@ -90,4 +95,9 @@ final class Request
{
return $this->baseCount;
}
public function isCountAbbreviated(): bool
{
return $this->isCountAbbreviated;
}
}