Skip to content

Commit

Permalink
Bug fix conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 20, 2024
1 parent fe13253 commit f3d4150
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Enum/JavascriptConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Enum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,23 @@ $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),
ServerError: Symbol(500),
})
```

> [!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:

Expand Down

0 comments on commit f3d4150

Please sign in to comment.