Skip to content

Instantly share code, notes, and snippets.

@BedrockDigger
Created April 22, 2022 10:26
Show Gist options
  • Save BedrockDigger/4da079b48f98020fd5dc70faa5bf3995 to your computer and use it in GitHub Desktop.
Save BedrockDigger/4da079b48f98020fd5dc70faa5bf3995 to your computer and use it in GitHub Desktop.
Let's leave the no-brainer - translating UML to skeleton class - alone.
const fieldsLiteral = `- name: String
- age: int
- tumId: String`
const funcsLiteral = `+ Person(String, int, String)
+ say(String): Shit`
printAttributes(fieldsLiteral)
printFunctions(funcsLiteral)
function printAttributes(fieldsLiteral) {
let result = ""
for (const attribute of fieldsLiteral.split("\n")) {
result += getAccessModifier(attribute) + getType(attribute) + getName(attribute) + ";\n"
}
console.log(result)
}
function printFunctions(funcsLiteral) {
let result = ""
for (const fn of funcsLiteral.split("\n")) {
result += getAccessModifier(fn) + getType(fn) + getRawSignature(fn) + " {\n\n}\n\n"
}
console.log(result)
}
function getAccessModifier(literal) {
const parts = literal.split(" ")
if (parts[0] == "+") {
return "public"
}
else if (parts[0] == "-") {
return "private"
}
else {
return "";
}
}
function getType(attrLiteral) {
if (attrLiteral.includes("void") || !attrLiteral.includes(":")) {
return " "
}
return " " + attrLiteral.substring(attrLiteral.indexOf(":") + 1).trim() + " "
}
function getName(literal) {
return literal.slice(0, literal.indexOf(":")).split(" ").slice(-1)[0]
}
function getRawSignature(funcLiteral) {
const name = funcLiteral.substring(funcLiteral.indexOf(" ") + 1, funcLiteral.indexOf("("))
let rawSignature = name + "("
const paramTypes = funcLiteral.substring(funcLiteral.indexOf("(") + 1, funcLiteral.indexOf(")")).split(", ")
let count = 0
for (const paramType of paramTypes) {
rawSignature += paramType + " param" + count++ + ", "
}
return rawSignature.substring(0, rawSignature.lastIndexOf(",")) + ")"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment