Files
plainleaf/common/space_lua/stdlib/template.ts
T
Zef Hemel 6078452a6c Breaking Lua API changes:
* tpl -> template.new
* define_command -> command.define
* define_event_listener -> event.listen
* tag -> index.tag

Updated in the docs
2025-01-19 14:32:11 +01:00

39 lines
1.0 KiB
TypeScript

import {
type ILuaFunction,
jsToLuaValue,
LuaBuiltinFunction,
LuaTable,
} from "$common/space_lua/runtime.ts";
import { interpolateLuaString } from "$common/space_lua/stdlib/space_lua.ts";
export const templateApi = new LuaTable({
each: new LuaBuiltinFunction(
async (sf, tbl: LuaTable | any[], fn: ILuaFunction): Promise<string> => {
const result = [];
if (tbl instanceof LuaTable) {
tbl = tbl.toJSArray();
}
for (const item of tbl) {
result.push(await fn.call(sf, item));
}
return result.join("");
},
),
new: new LuaBuiltinFunction(
(_sf, template: string): ILuaFunction => {
const lines = template.split("\n").map((line) =>
line.replace(/^\s{4}/, "")
);
const processed = lines.join("\n");
return new LuaBuiltinFunction(
async (sf, env: LuaTable | any) => {
if (!(env instanceof LuaTable)) {
env = jsToLuaValue(env);
}
return await interpolateLuaString(sf, processed, env);
},
);
},
),
});