silverbullet/common/space_lua/stdlib/table.ts

37 lines
941 B
TypeScript
Raw Normal View History

2024-10-10 02:35:07 +08:00
import {
2024-10-11 21:34:27 +08:00
type ILuaFunction,
LuaBuiltinFunction,
LuaTable,
2024-10-10 02:35:07 +08:00
} from "$common/space_lua/runtime.ts";
export const tableApi = new LuaTable({
2024-10-11 21:34:27 +08:00
concat: new LuaBuiltinFunction(
(tbl: LuaTable, sep?: string, i?: number, j?: number) => {
sep = sep ?? "";
i = i ?? 1;
j = j ?? tbl.length;
const result = [];
for (let k = i; k <= j; k++) {
result.push(tbl.get(k));
}
return result.join(sep);
},
),
insert: new LuaBuiltinFunction(
(tbl: LuaTable, posOrValue: number | any, value?: any) => {
if (value === undefined) {
value = posOrValue;
posOrValue = tbl.length + 1;
}
tbl.insert(posOrValue, value);
},
),
remove: new LuaBuiltinFunction((tbl: LuaTable, pos?: number) => {
pos = pos ?? tbl.length;
tbl.remove(pos);
}),
sort: new LuaBuiltinFunction((tbl: LuaTable, comp?: ILuaFunction) => {
return tbl.sort(comp);
}),
2024-10-10 02:35:07 +08:00
});