Skip to content

Commit

Permalink
Merge pull request #78 from pelmered/feature/improve-short-formatter
Browse files Browse the repository at this point in the history
Add hideCurrencySymbol method on column and entry
  • Loading branch information
pelmered authored Dec 5, 2024
2 parents 1ea3b62 + 6fc5346 commit f9a653d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ MoneyColumn::make('price')
MoneyColumn::make('price')
->short(), // Short format, e.g. $1.23M instead of $1,234,567.89

MoneyColumn::make('price')
->short()
->hideCurrencySymbol(), // Short format without currency symbol, e.g. 1.23M instead of $1.23M

MoneyColumn::make('price')
->decimals(4)
->short(), // $1.2345M
Expand Down
12 changes: 11 additions & 1 deletion src/Infolists/Components/MoneyEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MoneyEntry extends TextEntry
{
use HasMoneyAttributes;

protected bool $showCurrencySymbol = true;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -34,10 +36,18 @@ public function short(): static
$state,
$component->getCurrency(),
$component->getLocale(),
decimals: $this->getDecimals()
decimals: $this->getDecimals(),
showCurrencySymbol: $component->showCurrencySymbol,
);
});

return $this;
}

public function hideCurrencySymbol(bool $hideCurrencySymbol = true): static
{
$this->showCurrencySymbol = ! $hideCurrencySymbol;

return $this;

Check notice on line 51 in src/Infolists/Components/MoneyEntry.php

View check run for this annotation

OtterWise Otto / Code Coverage

src/Infolists/Components/MoneyEntry.php:49

Lines 49 - 51 are not covered by tests.
}
}
8 changes: 7 additions & 1 deletion src/MoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function formatAsDecimal(
}

public static function numberFormat(
null|int|string $value,
null|int|float|string $value,
Currency $currency,
string $locale,
int $decimals = 2,
Expand All @@ -68,6 +68,12 @@ public static function formatShort(

// No need to abbreviate if the value is less than 1000
if ($value < 100000) {
if (! $showCurrencySymbol) {
dump($value);

return static::numberFormat((int) $value / 100, $currency, $locale, decimals: $decimals);
}

return static::format($value, $currency, $locale, $decimals);
}

Expand Down
12 changes: 11 additions & 1 deletion src/Tables/Columns/MoneyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MoneyColumn extends TextColumn
{
use HasMoneyAttributes;

protected bool $showCurrencySymbol = true;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -34,10 +36,18 @@ public function short(): static
$state,
$component->getCurrency(),
$component->getLocale(),
decimals: $this->getDecimals()
decimals: $this->getDecimals(),
showCurrencySymbol: $component->showCurrencySymbol,
);
});

return $this;
}

public function hideCurrencySymbol(bool $hideCurrencySymbol = true): static
{
$this->showCurrencySymbol = ! $hideCurrencySymbol;

return $this;
}
}
8 changes: 8 additions & 0 deletions tests/MoneyColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@
expect($column->formatState(23523562))->toEqual(replaceNonBreakingSpaces('235,24K kr'));
//expect($column->formatState(23523562))->toEqual(replaceNonBreakingSpaces('235,24K kr'));
});

it('formats money column state to short format with sek and hide currency symbol', function () {
$column = MoneyColumn::make('price')->currency('SEK')->locale('sv_SE')->short()->hideCurrencySymbol();
expect($column->formatState(651))->toEqual(replaceNonBreakingSpaces('6,51'));
expect($column->formatState(235235))->toEqual(replaceNonBreakingSpaces('2,35K'));
expect($column->formatState(23523562))->toEqual(replaceNonBreakingSpaces('235,24K'));
expect($column->formatState(2352356254))->toEqual(replaceNonBreakingSpaces('23,52M'));
});

0 comments on commit f9a653d

Please sign in to comment.