File tree Expand file tree Collapse file tree 5 files changed +43
-6
lines changed Expand file tree Collapse file tree 5 files changed +43
-6
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66
77[project ]
88name = " leetcode-project-generator"
9- version = " 1.3.1 "
9+ version = " 1.4.0 "
1010authors = [
1111 {
name =
" Konrad Guzek" ,
email =
" [email protected] " },
1212]
Original file line number Diff line number Diff line change 66
77from .lang .base import BaseLanguageInterface
88from .lang .c import CLanguageInterface
9+ from .lang .javascript import JavaScriptLanguageInterface
910from .lang .python import PythonLanguageInterface
1011from .lang .python3 import Python3LanguageInterface
1112
1213LANGUAGE_INTERFACES : dict [str , BaseLanguageInterface ] = {
1314 "c" : CLanguageInterface (),
15+ "javascript" : JavaScriptLanguageInterface (),
1416 "python" : PythonLanguageInterface (),
1517 "python3" : Python3LanguageInterface (),
1618}
Original file line number Diff line number Diff line change 1212#include "solution.h"
1313
1414int 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 = ""
Original file line number Diff line number Diff line change 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"\n const { param } = undefined;"
33+ self .groups ["await" ] = "await " if self .groups ["async" ] else ""
34+ return {
35+ "solution.js" : f"{ template } \n export default { self .groups ['name' ]} ;" ,
36+ "test.js" : TEST_FILE_TEMPLATE .format (** self .groups ),
37+ }
You can’t perform that action at this time.
0 commit comments