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 01e8143 commit 6658da2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 34 deletions.
4 changes: 2 additions & 2 deletions 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 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 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 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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
Expand All @@ -81,7 +79,6 @@ HttpMethod::names(); // returns a list of all the names in the enumeration
HttpMethod::values(); // returns a list of all the names in the enumeration
HttpMethod::nameOf(404); // returns the name associated with the given value
// or null if it does not exist for the submitted value.
HttpMethod::toJavaScript(); // returns a Javascript structure equivalent
```

You need the `Bakame\Aide\Enum\Info` trait to expose the new API.
Expand Down Expand Up @@ -183,7 +180,7 @@ Javascript equivalent structure.
<?php

HttpMethod::toAssociative(); // returns tha associative array
HttpMethod::toJavaScript(); // returns a Javascript structure equivalent code
HttpMethod::toJavaScript(); // returns a Javascript structure equivalent code as string
```

You need the `Bakame\Aide\Enum\Convert` trait to expose the new API.
Expand Down Expand Up @@ -271,7 +268,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 @@ -303,6 +300,7 @@ Of course there are ways to improve the output depending on your use case you ca
- change the class name or add and/or change the object variable name;
- use `Symbol` when declaring the object property value;
- define indentation spaces and thus end of line;
- define the presence or absence of a trailing comma in the object representation;

Here's a more advance usage of the converter to highlight how you can configure it.

Expand All @@ -314,6 +312,7 @@ use Illuminate\Support\Str;
$converter = JavaScriptConverter::new()
->useImmutability()
->useExportDefault()
->useTrailingComma()
->useSymbol()
->indentSize(4)
->propertyNameCase(
Expand Down Expand Up @@ -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)
})
```

Expand Down

0 comments on commit 6658da2

Please sign in to comment.