First Commit

This commit is contained in:
Fabian
2019-09-18 21:54:28 +02:00
commit 429ba7e291
27 changed files with 5815 additions and 0 deletions

28
src/web/helper/table.ts Normal file
View File

@ -0,0 +1,28 @@
import { Context } from "koa";
import getTemplate from "./hb";
export default function getTable(title: string, data: any[], ctx: Context) {
let table: string[][] = [];
if (data.length > 0) {
if (typeof data[0] !== "object") {
table = [["value"], ...data.map(value => [value.toString()])];
} else {
if (Array.isArray(data[0])) {
table = data.map(row => row.map(col => col.toString()));
} else {
let fields = new Set<string>();
data.forEach(val => Object.keys(val).forEach(key => fields.add(key)))
let f = Array.from(fields.keys());
table = [f, ...data.map(value => f.map(key => value[key]))];
}
}
}
ctx.body = getTemplate("tables")({
title,
table,
empty: table.length <= 0
});
}