backend/source/helpers.ts

124 lines
2 KiB
TypeScript
Raw Normal View History

2024-09-11 17:24:20 +02:00
/**
*/
2024-09-12 00:03:29 +02:00
namespace _zeitbild.helpers
2024-09-11 17:24:20 +02:00
{
/**
* @todo timezone
*/
function ical_datetime_to_own_datetime(
ical_datetime : lib_plankton.ical.type_datetime
2024-09-26 16:47:38 +02:00
) : lib_plankton.pit.type_datetime
2024-09-11 17:24:20 +02:00
{
return {
"timezone_shift": 0,
"date": {
"year": ical_datetime.date.year,
"month": ical_datetime.date.month,
"day": ical_datetime.date.day,
},
"time": (
(ical_datetime.time === null)
?
null
:
{
"hour": ical_datetime.time.hour,
"minute": ical_datetime.time.minute,
"second": ical_datetime.time.second,
}
)
};
}
/**
* @todo timezone
*/
export function ical_dt_to_own_datetime(
ical_dt: lib_plankton.ical.type_dt
2024-09-26 16:47:38 +02:00
) : lib_plankton.pit.type_datetime
2024-09-11 17:24:20 +02:00
{
return {
"timezone_shift": 0,
"date": ical_dt.value.date,
"time": (
(ical_dt.value.time === null)
?
null
:
{
"hour": ical_dt.value.time.hour,
"minute": ical_dt.value.time.minute,
"second": ical_dt.value.time.second,
}
)
};
}
2024-09-12 16:35:57 +02:00
/**
*/
var _template_cache : Record<string, string> = {};
/**
* @todo caching
*/
export async function template_coin(
name : string,
data : Record<string, string>
) : Promise<string>
{
let content : string;
if (! (name in _template_cache)) {
content = (
(
await lib_plankton.file.read(
lib_plankton.string.coin(
"templates/{{name}}.html.tpl",
{
"name": name,
}
)
)
)
.toString()
);
_template_cache[name] = content;
}
else {
content = _template_cache[name];
}
return Promise.resolve<string>(
lib_plankton.string.coin(
content,
data
)
);
}
/**
* @todo outsource
*/
export async function promise_row<type_result>(
members : Array<
() => Promise<type_result>
>
) : Promise<
Array<
type_result
>
>
{
let results : Array<type_result> = [];
for await (const member of members) {
results.push(await member());
}
return Promise.resolve<Array<type_result>>(results);
}
2024-09-11 17:24:20 +02:00
}