feat: better class generation.
This commit is contained in:
parent
593a8d6a7f
commit
3be55dc53e
7 changed files with 63 additions and 20 deletions
|
|
@ -2,6 +2,29 @@
|
|||
module.exports = {
|
||||
ArrayList: {
|
||||
type: "class",
|
||||
methods: [{
|
||||
name: "add",
|
||||
args: "item: T",
|
||||
return: "void",
|
||||
}, {
|
||||
name: "remove",
|
||||
args: "item: T",
|
||||
return: "T | undefined",
|
||||
}, {
|
||||
name: "removeAt",
|
||||
args: "idx: number",
|
||||
return: "T | undefined",
|
||||
}],
|
||||
getters: [{
|
||||
name: "length",
|
||||
return: "number",
|
||||
prop_name: "_length",
|
||||
}],
|
||||
properties: [{
|
||||
name: "_length",
|
||||
type: "number",
|
||||
scope: "private",
|
||||
}]
|
||||
},
|
||||
SinglyLinkedList: {
|
||||
type: "class",
|
||||
|
|
|
|||
|
|
@ -29,11 +29,32 @@ const day_path = path.join(src_path, day_name);
|
|||
try { fs.unlinkSync(day_path); } catch (e) { }
|
||||
try { fs.mkdirSync(day_path); } catch (e) { }
|
||||
|
||||
function generate_method(method) {
|
||||
return `${method.name}(${method.args}): ${method.return || "void"} {
|
||||
|
||||
}`;
|
||||
}
|
||||
|
||||
function generate_property(prop) {
|
||||
return `${prop.scope} ${prop.name}: ${prop.type};`
|
||||
}
|
||||
|
||||
function generate_getter(getter) {
|
||||
return `${getter.name}(): ${getter.return} {
|
||||
return this.${getter.prop_name};
|
||||
}`
|
||||
}
|
||||
|
||||
function create_class(name, item) {
|
||||
fs.writeFileSync(path.join(day_path, `${name}.ts`), `export default class ${name}<T> {
|
||||
constructor() {
|
||||
${(item.properties || []).map(generate_property).join("\n ")}
|
||||
|
||||
${(item.getters || []).map(generate_getter).join("\n ")}
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
${(item.methods || []).map(generate_method).join("\n ")}
|
||||
}`);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue