Skip to content

Commit

Permalink
重大更新
Browse files Browse the repository at this point in the history
- 支持竖向表格解析
- 数值解析兼容
- 新增清空命令
  • Loading branch information
DoooReyn committed Dec 6, 2024
1 parent 934b6df commit 29f149d
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 36 deletions.
6 changes: 6 additions & 0 deletions Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function BooleanParser(text: string): boolean {
* @returns
*/
function IntegerParser(text: string): number {
if (typeof text === "string") {
text = text.replace(/\D/g, ""); // 去除所有非数字的字符
}
let ret = parseInt(text);
if (isNaN(ret)) {
throw new Error("[I] Not a number");
Expand All @@ -40,6 +43,9 @@ function IntegerParser(text: string): number {
* @returns
*/
function NumberParser(text: string): number {
if (typeof text === "string") {
text = text.replace(/[^\d.]/g, ""); // 去除所有非数字和非小数点的字符
}
let ret = parseFloat(text);
if (isNaN(ret)) {
throw new Error("[N] Not a number");
Expand Down
115 changes: 82 additions & 33 deletions XLSXDumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { CFG } from "./config";
import { JSONify } from "./JSONFormatter";
import { TSify } from "./TSFormatter";
import { BINify } from "./BinFormatter";
import { Exit } from "./cmm";

function ExtraSpace(str: string, len: number = 20) {
return str + " ".repeat(len - str.length);
}
import { ExtraSpace } from "./cmm";

/**
* 合并表头
Expand Down Expand Up @@ -51,13 +47,11 @@ function Passable(header: string[]) {
*/
export function XLSXDumper(filePath: string) {
console.log("准备解析配置:" + filePath);
fs.readFile(filePath, (err, data) => {
if (err) return Exit(-1, err.toString());
const sheets = xlsx.parse(data);
for (let i = 0; i < sheets.length; i++) {
parseSheet(sheets[i]);
}
});
const data = fs.readFileSync(filePath);
const sheets = xlsx.parse(data);
for (let i = 0; i < sheets.length; i++) {
parseSheet(sheets[i]);
}
}

/**
Expand All @@ -66,14 +60,26 @@ export function XLSXDumper(filePath: string) {
*/
function parseSheet(sheet: { name: string; data: any[] }) {
if (sheet.name.startsWith("#")) return;
if (sheet.name.startsWith("@")) {
parseVSheet(sheet); // 竖向表格
} else {
parseHSheet(sheet); // 横向表格
}
}

/**
* 解析横向表格
* @param sheet 表格信息
*/
function parseHSheet(sheet: { name: string; data: any[] }) {
let data: Record<number | string, any> = {};
let valid = false;
let table = sheet.name.split("#")[0];
let header1 = sheet.data[0];
let header2 = sheet.data[1];
let header3 = sheet.data[2];
let passable = Passable(header2);
console.log(" 正在解析表格:" + table);
console.log(" 正在解析横向表格:" + table);
console.log(" 字段:\n" + ZipHeaders(header1, header2, header3));
for (let l = 3; l < sheet.data.length; l++) {
let row = sheet.data[l];
Expand All @@ -94,26 +100,69 @@ function parseSheet(sheet: { name: string; data: any[] }) {
}
}
if (valid) {
const targets = CFG.TARGETS.split(",");
for (let i = 0; i < targets.length; i++) {
const target = targets[i];
// @ts-ignore
const dirname = CFG[target];
switch (target) {
case "JSON":
Save(dirname, ".json", table, JSONify(data));
break;
case "TS":
{
const [ts, dts] = TSify(table, [header2, header3, passable], data);
Save(dirname, ".ts", table, ts);
Save("types", ".d.ts", table, dts);
}
break;
case "BIN":
Save(dirname, ".bin", table, BINify(data));
break;
}
SaveSheet(table, header2, header3, passable, data);
}
}

/**
* 解析横向表格
* @param sheet 表格信息
*/
function parseVSheet(sheet: { name: string; data: any[] }) {
let data: Record<string, any> = {};
let table = sheet.name.split("#")[0].replace("@", "");
console.log(" 正在解析竖向表格:" + table);
const count = sheet.data.length;
if (count == 0) return console.warn("表格为空");
const width = sheet.data[0].length;
if (width == 4) {
const header1 = [];
const header2 = [];
const header3 = [];
const passable = [];
for (let i = 0; i < count; i++) {
let [key, name, identifier, value] = sheet.data[i];
passable[i] = false;
header1.push(key);
header2.push(name);
header3.push(identifier);
data[name] = Ruler.parse(identifier, value);
}
console.log(" 字段:\n" + ZipHeaders(header1, header2, header3));
SaveSheet(table, header2, header3, passable, data);
} else {
console.warn("竖向表格格式错误");
}
}

/**
* 保存表格
* @param table 表名
* @param header2 表头2
* @param header3 表头3
* @param passable 跳过字段
* @param data 数据
*/
function SaveSheet(table: string, header2: string[], header3: string[], passable: boolean[], data: Record<string, any>) {
const targets = CFG.TARGETS.split(",");
for (let i = 0; i < targets.length; i++) {
const target = targets[i];
// @ts-ignore
const dirname = CFG[target];
switch (target) {
case "JSON":
Save(dirname, ".json", table, JSONify(data));
break;
case "TS":
{
const [ts, dts] = TSify(table, [header2, header3, passable], data);
Save(dirname, ".ts", table, ts);
Save("types", ".d.ts", table, dts);
}
break;
case "BIN":
Save(dirname, ".bin", table, BINify(data));
break;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions cmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ export function Exit(code: number, msg?: string) {
}
process.exit(code);
}


/**
* 补齐宽度
* @param str 字符串
* @param len 预定宽度
* @returns
*/
export function ExtraSpace(str: string, len: number = 20) {
len = Math.max(0, len - str.length);
return len == 0 ? str : str + " ".repeat(len);
}
24 changes: 22 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { RSON } from "./rson";
*/
function dump() {
Ruler.initialize();
console.log(Ruler.parse("M=B", "a,1;b,2"));
console.log(Ruler.transform("M=B"));
const dirPath = path.join(__dirname, CFG.TABLE);
const files = fs.readdirSync(dirPath);
for (const file of files) {
Expand Down Expand Up @@ -131,6 +129,23 @@ function extractBin(bin: string) {
}
}

/**
* 清理所有文件
*/
function clear() {
const MINIFIED_AT = path.join(__dirname, CFG.MINIFIED);
const JSON_AT = path.join(__dirname, CFG.JSON);
const BIN_AT = path.join(__dirname, CFG.BIN);
const TS_AT = path.join(__dirname, CFG.TS);
const DTS_AT = path.join(__dirname, "types");
[MINIFIED_AT, JSON_AT, BIN_AT, TS_AT, DTS_AT].forEach((path) => {
if (fs.existsSync(path)) {
fs.rmSync(path, { recursive: true });
fs.mkdirSync(path);
}
});
}

/**
* 主程序
*/
Expand All @@ -149,9 +164,14 @@ switch (exe) {
case "--dump":
dump();
break;
case "-c":
case "--clear":
clear();
break;
case "-h":
case "--help":
default:
console.log("-c/--clear 清理所有文件");
console.log("-d/--dump 导出表格配置");
console.log("-e/--extract <bin> 解析BIN文件");
console.log("-m/--merge 合并所有输出");
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"types": ["node", "./minified/table.d.ts"]
}
}

0 comments on commit 29f149d

Please sign in to comment.