Skip to content

Commit 48b170a

Browse files
committed
add JavaScript interface
1 parent 0e60b62 commit 48b170a

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Planned available languages:
7272

7373
- [x] python
7474
- [x] python3 (thanks [@dancikmad](https://github.com/dancikmad))
75-
- [ ] javascript
75+
- [x] javascript
7676
- [ ] typescript
7777
- [x] c
7878
- [ ] cpp

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "leetcode-project-generator"
9-
version = "1.3.1"
9+
version = "1.4.0"
1010
authors = [
1111
{ name = "Konrad Guzek", email = "[email protected]" },
1212
]

src/lpg/interfaces/file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
from .lang.base import BaseLanguageInterface
88
from .lang.c import CLanguageInterface
9+
from .lang.javascript import JavaScriptLanguageInterface
910
from .lang.python import PythonLanguageInterface
1011
from .lang.python3 import Python3LanguageInterface
1112

1213
LANGUAGE_INTERFACES: dict[str, BaseLanguageInterface] = {
1314
"c": CLanguageInterface(),
15+
"javascript": JavaScriptLanguageInterface(),
1416
"python": PythonLanguageInterface(),
1517
"python3": Python3LanguageInterface(),
1618
}

src/lpg/interfaces/lang/c.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "solution.h"
1313
1414
int main() {{
15-
{param_declarations};
15+
{params_setup};
1616
{result_var_declaration}{name}({params_call});
1717
printf("{OUTPUT_RESULT_PREFIX} %d\\n", {result_var});
1818
return 0;
@@ -42,9 +42,7 @@ class CLanguageInterface(BaseLanguageInterface):
4242
def prepare_project_files(self, template: str):
4343

4444
params = self.groups["params"].split(", ")
45-
self.groups["param_declarations"] = self.groups["params"].replace(
46-
", ", ";\n "
47-
)
45+
self.groups["params_setup"] = self.groups["params"].replace(", ", ";\n ")
4846
self.groups["params_call"] = ", ".join(param.split()[-1] for param in params)
4947

5048
headers = ""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Project generator for the JavaScript language."""
2+
3+
import re
4+
5+
from .base import BaseLanguageInterface
6+
7+
FUNCTION_SIGNATURE_PATTERN = re.compile(
8+
r"^var (?P<name>\w+) =(?P<async> async)? function\((?P<params>[^)]*?)(?:\.\.\.\w+)?\) \{$",
9+
flags=re.MULTILINE,
10+
)
11+
12+
TEST_FILE_TEMPLATE = """\
13+
import solution from "./solution.js";
14+
{params_setup}
15+
const result = {await}solution({params});
16+
console.log("{OUTPUT_RESULT_PREFIX}", result);
17+
"""
18+
19+
20+
class JavaScriptLanguageInterface(BaseLanguageInterface):
21+
"""Implementation of the JavaScript language project template interface."""
22+
23+
function_signature_pattern = FUNCTION_SIGNATURE_PATTERN
24+
compile_command = None
25+
test_command = ["node", "test.js"]
26+
default_output = "undefined"
27+
28+
def prepare_project_files(self, template: str):
29+
self.groups["params_setup"] = ""
30+
for param in self.groups["params"].split(", "):
31+
if param.strip():
32+
self.groups["params_setup"] += f"\nconst {param} = undefined;"
33+
self.groups["await"] = "await " if self.groups["async"] else ""
34+
return {
35+
"solution.js": f"{template}\nexport default {self.groups['name']};",
36+
"test.js": TEST_FILE_TEMPLATE.format(**self.groups),
37+
}

0 commit comments

Comments
 (0)