Skip to content

Commit

Permalink
Adding support for allowing trailing comma or not in JavaScript object
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 23, 2024
1 parent 1e44a3d commit 314e899
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Enum/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
20 changes: 0 additions & 20 deletions src/Enum/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<JS
const Direction = Object.freeze({
Top: Symbol(0),
Down: Symbol(1),
Left: Symbol(2),
Right: Symbol(3),
})
JS;
self::assertSame($expected, Direction::toJavaScript());
}

#[Test]
Expand All @@ -39,16 +29,6 @@ public function it_can_get_information_from_a_backed_enumeration(): void
self::assertSame(['North', 'South', 'East', 'West'], Cardinal::names());
self::assertSame(['north', 'south', 'east', 'west'], Cardinal::values());
self::assertSame('West', Cardinal::nameOf('west'));
$expected = <<<JS
const Cardinal = Object.freeze({
North: "north",
South: "south",
East: "east",
West: "west",
})
JS;
self::assertSame($expected, Cardinal::toJavaScript());
}
}

Expand Down
53 changes: 51 additions & 2 deletions src/Enum/JavaScriptConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ private function __construct(
private readonly ?Closure $propertyNameCasing,
private readonly int $indentSize,
private readonly string $export,
private readonly int $valueStartAt
private readonly int $valueStartAt,
private readonly bool $useTrailingComma,
) {
}

Expand All @@ -34,6 +35,7 @@ public static function new(): self
indentSize: 2,
export: self::EXPORT_NONE,
valueStartAt: 0,
useTrailingComma: false,
);
}

Expand All @@ -46,6 +48,7 @@ public function propertyNameCase(Closure $casing = null): self
$this->indentSize,
$this->export,
$this->valueStartAt,
$this->useTrailingComma,
);
}

Expand All @@ -60,6 +63,7 @@ public function useImmutability(): self
$this->indentSize,
$this->export,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -75,6 +79,7 @@ public function ignoreImmutability(): self
$this->indentSize,
$this->export,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -90,6 +95,7 @@ public function useSymbol(): self
$this->indentSize,
$this->export,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -105,6 +111,7 @@ public function ignoreSymbol(): self
$this->indentSize,
$this->export,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -120,6 +127,7 @@ public function useExportDefault(): self
$this->indentSize,
self::EXPORT_DEFAULT,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -135,6 +143,7 @@ public function useExport(): self
$this->indentSize,
self::EXPORT,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -150,6 +159,7 @@ public function ignoreExport(): self
$this->indentSize,
self::EXPORT_NONE,
$this->valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -165,6 +175,7 @@ public function valueStartAt(int $valueStartAt): self
$this->indentSize,
$this->export,
$valueStartAt,
$this->useTrailingComma,
),
};
}
Expand All @@ -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,
),
};
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Enum/JavaScriptConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -181,7 +181,7 @@ public function it_can_convert_to_a_javascript_object_with_export_default_and_no
));

$expected = <<<JS
export default Object.freeze({Ok: Symbol(200),Redirection: Symbol(302),NotFound: Symbol(404),ServerError: Symbol(500),})
export default Object.freeze({Ok: Symbol(200),Redirection: Symbol(302),NotFound: Symbol(404),ServerError: Symbol(500)})
JS;
$actual = JavaScriptConverter::new()
->useImmutability()
Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/Enum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const HttpStatusCode = Object.freeze({
HTTP_OK: 200,
HTTP_REDIRECTION: 302,
HTTP_NOT_FOUND: 404,
HTTP_SERVER_ERROR: 500,
HTTP_SERVER_ERROR: 500
})
```

Expand Down Expand Up @@ -314,6 +314,7 @@ use Illuminate\Support\Str;
$converter = JavaScriptConverter::new()
->useImmutability()
->useExportDefault()
->useTrailingComma()
->useSymbol()
->indentSize(4)
->propertyNameCase(
Expand Down Expand Up @@ -366,7 +367,7 @@ will produce the following javascript code snippet:
const Color = Object.freeze({
Red: Symbol(0),
Blue: Symbol(1),
Green: Symbol(2),
Green: Symbol(2)
})
```

Expand Down

0 comments on commit 314e899

Please sign in to comment.