Skip to content

Commit

Permalink
fix: config
Browse files Browse the repository at this point in the history
ts writter : allow configs
  • Loading branch information
nikuscs committed Jul 27, 2024
1 parent 9c718a5 commit c887114
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ composer require flavorly/laravel-helpers
You can publish the config file with:

```bash
php artisan vendor:publish --tag="laravel-helpers-config"
php artisan vendor:publish --tag="helpers-config"
```

## Testing
Expand Down
7 changes: 7 additions & 0 deletions config/helpers.php → config/laravel-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@
'storage_scale' => 10,
'rounding_mode' => \Brick\Math\RoundingMode::DOWN,
],

'typescript' => [
'replace' => [
'Flavorly.InertiaFlash.' => '',
'Flavorly.LaravelHelpers.' => '',
]
]
];
6 changes: 3 additions & 3 deletions src/Helpers/Math/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public static function of(
return new Math(
$number,
// @phpstan-ignore-next-line
$scale ?? (int) config('helpers.math.scale', 10),
$scale ?? (int) config('laravel-helpers.math.scale', 10),
// @phpstan-ignore-next-line
$storageScale ?? (int) config('helpers.math.scale', 10),
$storageScale ?? (int) config('laravel-helpers.math.scale', 10),
// @phpstan-ignore-next-line
$roundingMode ?? config('helpers.math.rounding_mode', RoundingMode::DOWN)
$roundingMode ?? config('laravel-helpers.math.rounding_mode', RoundingMode::DOWN)
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Helpers/TypeDefinitionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class TypeDefinitionWriter extends BaseWriter
{
public function format(TypesCollection $collection): string
{
$replacements = config('laravel-helpers.typescript.replace', []);

return Str::of(parent::format($collection))
->replaceMatches('/(\w+)\??: ([\w\.<>]+) \| null;/', function ($matches) {
return sprintf('%s?: %s', $matches[1], $matches[2]);
})
->replace(search: 'Flavorly.InertiaFlash.', replace: '')
->replace(search: 'Flavorly.LaravelHelpers.', replace: '')
// @phpstan-ignore-next-line
->replace(search: array_keys($replacements), replace: array_values($replacements))
->replace(search: 'App.Data', replace: 'Data')
->replace(search: 'App.Enums', replace: 'Enums')
->replace(search: 'App.Http.Requests', replace: 'Requests')
Expand Down
4 changes: 2 additions & 2 deletions src/LaravelHelpersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class LaravelHelpersServiceProvider extends PackageServiceProvider
public function configurePackage(Package $package): void
{
$package
->name('laravel-helpers')
->hasConfigFile();
->name('helpers')
->hasConfigFile('laravel-helpers');
}

public function bootingPackage(): void
Expand Down
14 changes: 7 additions & 7 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Flavorly\LaravelHelpers\Helpers\Math\Math;

beforeEach(function () {
config()->set('helpers.math.scale', 2);
config()->set('helpers.math.storage_scale', 10);
config()->set('helpers.math.rounding_mode', RoundingMode::DOWN);
config()->set('laravel-helpers.math.scale', 2);
config()->set('laravel-helpers.math.storage_scale', 10);
config()->set('laravel-helpers.math.rounding_mode', RoundingMode::DOWN);
});

it('performs basic sum operations', function ($initial, $addend, $expected, $scale = null) {
Expand Down Expand Up @@ -430,17 +430,17 @@

it('respects config scale and rounding mode', function () {
// Mock config values
config(['helpers.math.scale' => 4]);
config(['helpers.math.rounding_mode' => RoundingMode::UP]);
config(['laravel-helpers.math.scale' => 4]);
config(['laravel-helpers.math.rounding_mode' => RoundingMode::UP]);

$math = Math::of(1.23456789);

expect($math->toFloat())->toBe(1.2346);
expect($math->roundingMode)->toBe(RoundingMode::UP);

// Reset config to default
config(['helpers.math.scale' => 2]);
config(['helpers.math.rounding_mode' => RoundingMode::DOWN]);
config(['laravel-helpers.math.scale' => 2]);
config(['laravel-helpers.math.rounding_mode' => RoundingMode::DOWN]);

$defaultMath = Math::of(1.23456789);

Expand Down

0 comments on commit c887114

Please sign in to comment.