From f3d415002afdcc7f81bb0e64e82bdfb46bcc205c Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 20 Jan 2024 08:28:01 +0100 Subject: [PATCH] Bug fix conversion --- src/Enum/JavascriptConverter.php | 8 +++++++- src/Enum/README.md | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Enum/JavascriptConverter.php b/src/Enum/JavascriptConverter.php index 557f568..3c4d9e6 100644 --- a/src/Enum/JavascriptConverter.php +++ b/src/Enum/JavascriptConverter.php @@ -197,7 +197,12 @@ public function convertToObject(string $enumClass, ?string $objectName = null): $output = "const $objectName = $output"; } - return $this->useExport.$output.$eol; + $export = self::EXPORT_NONE; + if (self::EXPORT_NONE !== $this->useExport) { + $export = self::EXPORT_SIMPLE; + } + + return $export.$output.$eol; } /** @@ -231,6 +236,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; + ; } /** diff --git a/src/Enum/README.md b/src/Enum/README.md index 03da627..da48e94 100644 --- a/src/Enum/README.md +++ b/src/Enum/README.md @@ -275,13 +275,13 @@ $converter = JavascriptConverter::new() fn (string $name) => Str::of($name)->replace('HTTP_', '')->lower()->studly()->toString() ); -echo $converter->convertToObject(HttpStatusCode::class); +echo $converter->convertToObject(HttpStatusCode::class, 'StatusCode'); ``` will return the following Javascript code: ```javascript -export default Object.freeze({ +export const StatusCode = Object.freeze({ Ok: Symbol(200), Redirection: Symbol(302), NotFound: Symbol(404), @@ -289,6 +289,9 @@ export default Object.freeze({ }) ``` +> [!NOTE] +> __default__ is silently ignore as it is an invalid construct with `const`. + The converter will not store the resulting string into a Javascriot file as this part is left to the discretion of the implementor. There are several ways to do so: