From 1c1f2452668be241683cf53c1f2c2701a179a54d Mon Sep 17 00:00:00 2001 From: Omar Trkzi <81875005+Trkzi-Omar@users.noreply.github.com> Date: Sun, 7 Jan 2024 06:40:12 +0100 Subject: [PATCH] Feat: Abbreviation (#88) * Feat: Abbreviation --------- Co-authored-by: Anton Komarev --- README.md | 10 ++++++++++ public/index.php | 2 ++ src/BadgeImageRendererService.php | 27 ++++++++++++++++++++++++--- src/Request.php | 12 +++++++++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b23b6e7..1dfafaa 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/public/index.php b/public/index.php index 0d10a97..3efe6f7 100644 --- a/public/index.php +++ b/public/index.php @@ -36,6 +36,7 @@ $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 @@ $count, $badgeMessageBackgroundFill, $badgeStyle, + $isCountAbbreviated, ); } } catch (InvalidPathException $exception) { diff --git a/src/BadgeImageRendererService.php b/src/BadgeImageRendererService.php index f4ad326..5f198d1 100644 --- a/src/BadgeImageRendererService.php +++ b/src/BadgeImageRendererService.php @@ -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 @@ public function renderBadgeWithCount( 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 @@ private function renderBadge( * 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]; + } } diff --git a/src/Request.php b/src/Request.php index d704543..eddcf5f 100644 --- a/src/Request.php +++ b/src/Request.php @@ -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 @@ public function __construct( $this->badgeColor = $badgeColor; $this->badgeStyle = $badgeStyle; $this->baseCount = $baseCount; + $this->isCountAbbreviated = $isCountAbbreviated; } public static function of( @@ -58,6 +62,7 @@ public static function of( $get['color'] ?? null, $get['style'] ?? null, $get['base'] ?? null, + boolval($get['abbreviated'] ?? false), ); } @@ -90,4 +95,9 @@ public function baseCount(): ?string { return $this->baseCount; } + + public function isCountAbbreviated(): bool + { + return $this->isCountAbbreviated; + } }