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 4f96cd5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
33 changes: 19 additions & 14 deletions src/Enum/JavascriptConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -31,7 +31,7 @@ public static function new(): self
useImmutability: true,
propertyNameCasing: null,
indentSize: 2,
useExport: self::EXPORT_NONE
export: self::EXPORT_NONE
);
}

Expand All @@ -42,7 +42,7 @@ public function propertyNameCase(Closure $casing = null): self
$this->useImmutability,
$casing,
$this->indentSize,
$this->useExport,
$this->export,
);
}

Expand All @@ -55,7 +55,7 @@ public function useImmutability(): self
true,
$this->propertyNameCasing,
$this->indentSize,
$this->useExport,
$this->export,
),
};
}
Expand All @@ -69,7 +69,7 @@ public function ignoreImmutability(): self
false,
$this->propertyNameCasing,
$this->indentSize,
$this->useExport,
$this->export,
),
};
}
Expand All @@ -83,7 +83,7 @@ public function useSymbol(): self
$this->useImmutability,
$this->propertyNameCasing,
$this->indentSize,
$this->useExport,
$this->export,
),
};
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -154,7 +154,7 @@ public function intendSize(int $indentSize): self
$this->useImmutability,
$this->propertyNameCasing,
$indentSize,
$this->useExport,
$this->export,
),
};
}
Expand All @@ -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,
Expand All @@ -193,11 +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";
}

return $this->useExport.$output.$eol;
if (self::EXPORT_DEFAULT === $this->export) {
return $output.';'.$eol.$this->export.$objectName.';'.$eol;
}

return $this->export.$output.$eol;
}

/**
Expand Down Expand Up @@ -230,7 +234,8 @@ 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;
;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/Enum/JavascriptConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<JS
const StatusCode = Object.freeze({
Ok: Symbol(200),
Redirection: Symbol(302),
NotFound: Symbol(404),
ServerError: Symbol(500),
});
export default StatusCode;
JS;
$actual = JavascriptConverter::new()
->useImmutability()
->useExportDefault()
->useSymbol()
->intendSize(4)
->propertyNameCase(fn (string $name) => $pascalCase(strtolower(str_replace('HTTP_', '', $name))))
->convertToObject(HttpStatusCode::class, 'StatusCode');

self::assertSame($expected, $actual);
}
}
7 changes: 4 additions & 3 deletions src/Enum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,19 @@ $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({
const StatusCode = Object.freeze({
Ok: Symbol(200),
Redirection: Symbol(302),
NotFound: Symbol(404),
ServerError: Symbol(500),
})
});
export default StatusCode;
```

The converter will not store the resulting string into a Javascriot file as this part is
Expand Down

0 comments on commit 4f96cd5

Please sign in to comment.