Skip to content

Commit

Permalink
Feat: Abbreviation (#88)
Browse files Browse the repository at this point in the history
* Feat: Abbreviation

---------

Co-authored-by: Anton Komarev
  • Loading branch information
Trkzi-Omar committed Jan 7, 2024
1 parent 8c49635 commit 1c1f245
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -72,6 +73,7 @@
$count,
$badgeMessageBackgroundFill,
$badgeStyle,
$isCountAbbreviated,
);
}
} catch (InvalidPathException $exception) {
Expand Down
27 changes: 24 additions & 3 deletions src/BadgeImageRendererService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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,
Expand Down Expand Up @@ -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];
}
}
12 changes: 11 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ 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;
$this->badgeLabel = $badgeLabel;
$this->badgeColor = $badgeColor;
$this->badgeStyle = $badgeStyle;
$this->baseCount = $baseCount;
$this->isCountAbbreviated = $isCountAbbreviated;
}

public static function of(
Expand All @@ -58,6 +62,7 @@ public static function of(
$get['color'] ?? null,
$get['style'] ?? null,
$get['base'] ?? null,
boolval($get['abbreviated'] ?? false),
);
}

Expand Down Expand Up @@ -90,4 +95,9 @@ public function baseCount(): ?string
{
return $this->baseCount;
}

public function isCountAbbreviated(): bool
{
return $this->isCountAbbreviated;
}
}

0 comments on commit 1c1f245

Please sign in to comment.