-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSFormatter.ts
26 lines (25 loc) · 968 Bytes
/
TSFormatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Capitalize } from "./cmm";
import { JSONify } from "./JSONFormatter";
import { Ruler } from "./Rule";
/**
* 转换为TS代码
* @param table 表名
* @param headers 表头
* @param data 数据
* @returns
*/
export function TSify(table: string, headers: [string[], string[], boolean[]], data: Record<number | string, any>) {
const ctable = Capitalize(table);
const [header2, header3, passable] = headers;
const interfaces = ["interface ITbl" + ctable + " {"];
for (let i = 0; i < passable.length; i++) {
if (passable[i]) continue;
const type = Ruler.transform(header3[i]);
interfaces.push(` ${header2[i]}: ${type};`);
}
interfaces.push("}");
const types = interfaces.join("\n");
const tables = "export const Tbl" + ctable + ": Record<string, ITbl" + ctable + "> = " + JSONify(data, 0) + " as const;";
const content = ["export " + types, tables].join("\n\n");
return [content, types];
}