Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
- 优化合并所有声明
- 添加合并测试说明
  • Loading branch information
DoooReyn committed Dec 5, 2024
1 parent 09dcd67 commit 3607b69
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ dist
.yarn/install-state.gz
.pnp.*

bin/
json/
minified/
types/
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,27 @@ console.log(transformed); // {0: number, length: 5}

![表名](screenshot/sheet-name.png)

## 示例

### 导出示例

- json 导出示例</br>
![json导出示例](screenshot/dump-json.png)
![json导出示例](screenshot/dump-json.png)
- dts 导出示例</br>
![.d.ts导出示例](screenshot/dump-dts.png)
- ts 导出示例</br>
![ts导出示例](screenshot/dump-ts.png)

### 合并后文件大小对比

> 本次测试:角色表包含 2000 条数据。
![合并后文件大小对比](screenshot/merge-file-size.png)

其中:

- TS 文件最大,因为它包含了类型声明;
- JSON 文件次之,但和 TS 的差距不明显,因为它不包含类型声明,且表格的样本较小;
- BIN 文件最小,因为它使用了文本压缩算法对 JSON 数据进行了二次压缩(最终大小仅为 JSON 的 7%)。

开发者可以根据需要选择合适的格式。或者使用自定义的压缩算法,本项目中使用的是 zlib 压缩算法(由 [zipson](https://github.com/jgranstrom/zipson)[pako](https://github.com/nodeca/pako) 组合实现),开发者也可以使用其他压缩算法,例如:gzip、brotli、lz4 等。
2 changes: 1 addition & 1 deletion TSFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function TSify(table: string, headers: [string[], string[], boolean[]], d
}
interfaces.push("}");
const types = interfaces.join("\n");
const tables = "export const Tbl" + ctable + ": Record<string, ITbl" + ctable + "> = " + JSONify(data, 4) + " as const;";
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];
}
Binary file modified bin/role.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const CFG = {
/** 合并目录 */
MINIFIED: "minified",
/** 合并名称 */
MERGE_AS: "table.min"
MERGE_AS: "table"
} as const;
5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ function mergeAllDTS() {
const all = [];
const dirPath = path.join(__dirname, "types");
const files = fs.readdirSync(dirPath);
const tables = [" type Tables = {"];
for (const file of files) {
if (file.endsWith(".d.ts")) {
const name = path.basename(file, ".d.ts");
tables.push(" " + name + ": ITbl" + Capitalize(name) + ";");
let dts = fs.readFileSync(path.join(dirPath, file), "utf-8");
dts = dts
.split("\n")
Expand All @@ -95,6 +98,8 @@ function mergeAllDTS() {
all.push(dts);
}
}
tables.push(" }");
all.push(tables.join("\n"));
const MINIFIED_AT = path.join(__dirname, CFG.MINIFIED);
const TS_AT = path.join(MINIFIED_AT, CFG.MERGE_AS + ".d.ts");
if (!fs.existsSync(MINIFIED_AT)) {
Expand Down
Binary file added screenshot/merge-file-size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified table/test.xlsx
Binary file not shown.
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
"noPropertyAccessFromIndexSignature": false,
"types": ["node", "./minified/table.d.ts"]
}
}

0 comments on commit 3607b69

Please sign in to comment.