sindri/source/outputs/typescript.ts
2023-05-11 12:59:02 +02:00

74 lines
1.8 KiB
TypeScript

const output_typescript : type_output = {
"render": function (input_data) {
return (
input_data["domains"]
.map(
(domain) => lib_plankton.string.coin(
"type type_{{name}} = {\n{{fields}}\n};\nlet collection_{{name}} : {{collection_type}} = {{collection_value}};\n",
{
"name": domain.name,
"fields": (
domain.data_fields
.map(
(data_field) => lib_plankton.string.coin(
"\t{{name}} : {{type}};{{macro_comment}}",
{
"name": data_field.name,
"type": lib_plankton.string.coin(
(data_field.nullable ? "(null | {{core}})" : "{{core}}"),
{
"core": {
"boolean": "boolean",
"integer": "number",
"float": "number",
"string_short": "string",
"string_medium": "string",
"string_long": "string",
}[data_field["type"]],
}
),
"macro_comment": (
(data_field.description !== null)
? lib_plankton.string.coin(
" // {{comment}}",
{
"comment": data_field.description,
}
)
: ""
),
}
)
)
// .map(x => ("\t" + x))
.join("\n")
),
"collection_type": (
(domain.key_field === null)
? lib_plankton.string.coin(
"Array<type_{{name}}>",
{
"name": domain.name,
}
)
: lib_plankton.string.coin(
"Record<number, type_{{name}}>",
{
"name": domain.name,
}
)
),
"collection_value": (
(domain.key_field === null)
? "[]"
: "{}"
),
}
)
)
.map(x => (x + "\n"))
.join("\n")
);
},
};