From bb0b14ce8a72c0a3178afb00b78e5cfd5b07eb2a Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 20 Jan 2024 20:57:05 +0100 Subject: [PATCH] Bug fix conversion with export default --- src/Enum/JavascriptConverter.php | 33 ++++++++++++++-------------- src/Enum/JavascriptConverterTest.php | 32 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/Enum/JavascriptConverter.php b/src/Enum/JavascriptConverter.php index 3c4d9e6..fc00a65 100644 --- a/src/Enum/JavascriptConverter.php +++ b/src/Enum/JavascriptConverter.php @@ -20,7 +20,7 @@ private function __construct( private readonly bool $useImmutability, private readonly ?Closure $propertyNameCasing, private readonly int $indentSize, - private readonly string $useExport, + private readonly string $export, ) { } @@ -31,7 +31,7 @@ public static function new(): self useImmutability: true, propertyNameCasing: null, indentSize: 2, - useExport: self::EXPORT_NONE + export: self::EXPORT_NONE ); } @@ -42,7 +42,7 @@ public function propertyNameCase(Closure $casing = null): self $this->useImmutability, $casing, $this->indentSize, - $this->useExport, + $this->export, ); } @@ -55,7 +55,7 @@ public function useImmutability(): self true, $this->propertyNameCasing, $this->indentSize, - $this->useExport, + $this->export, ), }; } @@ -69,7 +69,7 @@ public function ignoreImmutability(): self false, $this->propertyNameCasing, $this->indentSize, - $this->useExport, + $this->export, ), }; } @@ -83,7 +83,7 @@ public function useSymbol(): self $this->useImmutability, $this->propertyNameCasing, $this->indentSize, - $this->useExport, + $this->export, ), }; } @@ -97,14 +97,14 @@ public function ignoreSymbol(): self $this->useImmutability, $this->propertyNameCasing, $this->indentSize, - $this->useExport, + $this->export, ), }; } public function useExportDefault(): self { - return match ($this->useExport) { + return match ($this->export) { self::EXPORT_DEFAULT => $this, default => new self( $this->useSymbol, @@ -118,7 +118,7 @@ public function useExportDefault(): self public function useExport(): self { - return match ($this->useExport) { + return match ($this->export) { self::EXPORT_SIMPLE => $this, default => new self( $this->useSymbol, @@ -132,7 +132,7 @@ public function useExport(): self public function ignoreExport(): self { - return match ($this->useExport) { + return match ($this->export) { self::EXPORT_NONE => $this, default => new self( $this->useSymbol, @@ -154,7 +154,7 @@ public function intendSize(int $indentSize): self $this->useImmutability, $this->propertyNameCasing, $indentSize, - $this->useExport, + $this->export, ), }; } @@ -181,7 +181,6 @@ public function convertToObject(string $enumClass, ?string $objectName = null): $eol = "\n"; } - $objectName = $this->sanitizeName($objectName, $enumClass); $output = array_reduce( $enumClass::cases(), fn (string $output, BackedEnum $enum): string => $output.$space.$this->formatPropertyName($enum).': '.$this->formatPropertyValue($enum).','.$eol, @@ -193,16 +192,16 @@ public function convertToObject(string $enumClass, ?string $objectName = null): $output = "Object.freeze($output)"; } + $objectName = $this->sanitizeName($objectName, $enumClass); if (null !== $objectName) { $output = "const $objectName = $output"; } - $export = self::EXPORT_NONE; - if (self::EXPORT_NONE !== $this->useExport) { - $export = self::EXPORT_SIMPLE; + if (self::EXPORT_DEFAULT === $this->export) { + return $output.';'.$eol.$this->export.$objectName.';'.$eol; } - return $export.$output.$eol; + return $this->export.$output.$eol; } /** @@ -235,7 +234,7 @@ public function convertToClass(string $enumClass, string $className = ''): strin $output = 'class '.$className.' {'.$eol.$output.$eol.$space.'constructor(name) {'.$eol.$space.$space.'this.name = name'.$eol.$space.'}'.$eol.'}'; - return $this->useExport.$output.$eol; + return $this->export.$output.$eol; ; } diff --git a/src/Enum/JavascriptConverterTest.php b/src/Enum/JavascriptConverterTest.php index a851df9..565f594 100644 --- a/src/Enum/JavascriptConverterTest.php +++ b/src/Enum/JavascriptConverterTest.php @@ -144,4 +144,36 @@ public function it_can_convert_to_a_javascript_class(): void $converter->convertToClass(HttpStatusCode::class, 'Foobar') ); } + + #[Test] + public function it_can_convert_to_a_javascript_object_with_export_default(): void + { + $pascalCase = fn (string $word): string => implode('', array_map( + ucfirst(...), + explode( + ' ', + strtolower(str_replace(['_', '-'], [' ', ' '], $word)) + ) + )); + + $expected = <<useImmutability() + ->useExportDefault() + ->useSymbol() + ->intendSize(4) + ->propertyNameCase(fn (string $name) => $pascalCase(strtolower(str_replace('HTTP_', '', $name)))) + ->convertToObject(HttpStatusCode::class, 'StatusCode'); + + self::assertSame($expected, $actual); + } }