From 6658da22a034371617b3c9981c6b59e722caef07 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Tue, 23 Jan 2024 08:51:08 +0100 Subject: [PATCH] Adding support for allowing trailing comma or not in JavaScript object --- ConvertTest.php | 4 +-- InfoTest.php | 20 -------------- JavaScriptConverter.php | 53 +++++++++++++++++++++++++++++++++++-- JavaScriptConverterTest.php | 10 ++++--- README.md | 11 ++++---- 5 files changed, 64 insertions(+), 34 deletions(-) diff --git a/ConvertTest.php b/ConvertTest.php index 77af1b7..d5198a7 100644 --- a/ConvertTest.php +++ b/ConvertTest.php @@ -18,7 +18,7 @@ public function it_can_get_information_from_a_pure_enumeration(): void Top: Symbol(0), Down: Symbol(1), Left: Symbol(2), - Right: Symbol(3), + Right: Symbol(3) }) JS; @@ -34,7 +34,7 @@ public function it_can_get_information_from_a_backed_enumeration(): void North: "north", South: "south", East: "east", - West: "west", + West: "west" }) JS; diff --git a/InfoTest.php b/InfoTest.php index 828cc81..e70b302 100644 --- a/InfoTest.php +++ b/InfoTest.php @@ -18,16 +18,6 @@ public function it_can_get_information_from_a_pure_enumeration(): void self::assertSame(['Top', 'Down', 'Left', 'Right'], Direction::names()); self::assertSame([], Direction::values()); self::assertNull(Direction::nameOf('Up')); - $expected = <<indentSize, $this->export, $this->valueStartAt, + $this->useTrailingComma, ); } @@ -60,6 +63,7 @@ public function useImmutability(): self $this->indentSize, $this->export, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -75,6 +79,7 @@ public function ignoreImmutability(): self $this->indentSize, $this->export, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -90,6 +95,7 @@ public function useSymbol(): self $this->indentSize, $this->export, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -105,6 +111,7 @@ public function ignoreSymbol(): self $this->indentSize, $this->export, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -120,6 +127,7 @@ public function useExportDefault(): self $this->indentSize, self::EXPORT_DEFAULT, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -135,6 +143,7 @@ public function useExport(): self $this->indentSize, self::EXPORT, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -150,6 +159,7 @@ public function ignoreExport(): self $this->indentSize, self::EXPORT_NONE, $this->valueStartAt, + $this->useTrailingComma, ), }; } @@ -165,6 +175,7 @@ public function valueStartAt(int $valueStartAt): self $this->indentSize, $this->export, $valueStartAt, + $this->useTrailingComma, ), }; } @@ -181,6 +192,39 @@ public function indentSize(int $indentSize): self $indentSize, $this->export, $this->valueStartAt, + $this->useTrailingComma, + ), + }; + } + + public function useTrailingComma(): self + { + return match ($this->useTrailingComma) { + true => $this, + default => new self( + $this->useSymbol, + $this->useImmutability, + $this->propertyNameCasing, + $this->indentSize, + $this->export, + $this->valueStartAt, + true, + ), + }; + } + + public function ignoreTrailingComma(): self + { + return match ($this->useTrailingComma) { + false => $this, + default => new self( + $this->useSymbol, + $this->useImmutability, + $this->propertyNameCasing, + $this->indentSize, + $this->export, + $this->valueStartAt, + false, ), }; } @@ -234,7 +278,12 @@ private function getObjectBody(string $enumClass, string $space, string $eol): s $output[] = $space.$this->formatPropertyName($enum).': '.$this->formatPropertyValue($enum, $offset).','; } - return implode($eol, $output).$eol; + $body = implode($eol, $output); + + return match ($this->useTrailingComma) { + true => $body, + false => substr($body, 0, -1), + }.$eol; } /** diff --git a/JavaScriptConverterTest.php b/JavaScriptConverterTest.php index 4a7f8a5..3c18dd4 100644 --- a/JavaScriptConverterTest.php +++ b/JavaScriptConverterTest.php @@ -34,7 +34,7 @@ public function it_will_convert_to_a_javascript_immutable_object_by_default(): v HTTP_OK: 200, HTTP_REDIRECTION: 302, HTTP_NOT_FOUND: 404, - HTTP_SERVER_ERROR: 500, + HTTP_SERVER_ERROR: 500 }) JS; @@ -57,7 +57,7 @@ public function it_can_convert_to_a_javascript_mutable_object(): void http_ok: Symbol(200), http_redirection: Symbol(302), http_not_found: Symbol(404), - http_server_error: Symbol(500), + http_server_error: Symbol(500) } JS; @@ -153,7 +153,7 @@ public function it_can_convert_to_a_javascript_object_with_export_default_and_a_ Ok: Symbol(200), Redirection: Symbol(302), NotFound: Symbol(404), - ServerError: Symbol(500), + ServerError: Symbol(500) }); export default StatusCode; @@ -181,7 +181,7 @@ public function it_can_convert_to_a_javascript_object_with_export_default_and_no )); $expected = <<useImmutability() @@ -199,10 +199,12 @@ public function it_can_convert_a_pure_enum_with_different_starting_value(): void { $actualStartAtZero = JavaScriptConverter::new() ->indentSize(0) + ->useTrailingComma() ->convertToObject(Dir::class); $actualStartAtFortyTwo = JavaScriptConverter::new() ->indentSize(0) + ->useTrailingComma() ->valueStartAt(42) ->convertToObject(Dir::class); diff --git a/README.md b/README.md index aec573f..f86f2fc 100644 --- a/README.md +++ b/README.md @@ -67,9 +67,7 @@ Gather information regarding the current Enum via **public static methods**. Thi - the type of enum via the `isBacked` and `isPure` method; - the names of each cases with the `names` method; - the possible values for the Enum with the `values` method; -- the `associative` method which returns an associative array contains the string name and their respective values; - the `nameOf` which returns the name associated with a specific `value` -- the `toJavaScript` which returns a Javascript structure equivalent to the current Enum.(see `JavaScriptConverter` for more details) ```php useImmutability() ->useExportDefault() + ->useTrailingComma() ->useSymbol() ->indentSize(4) ->propertyNameCase( @@ -366,7 +365,7 @@ will produce the following javascript code snippet: const Color = Object.freeze({ Red: Symbol(0), Blue: Symbol(1), - Green: Symbol(2), + Green: Symbol(2) }) ```