Skip to content

Commit

Permalink
优化部分已知问题
Browse files Browse the repository at this point in the history
- 优化布尔解析器、数值解析器、字符串解析器
- 修复空字段不导出问题
  • Loading branch information
DoooReyn committed Dec 7, 2024
1 parent a78f25a commit 05ffebc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
11 changes: 8 additions & 3 deletions Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export interface IRule<RET> {
* @example 1 => true
* @returns
*/
function BooleanParser(text: string): boolean {
return text === "1";
function BooleanParser(text: string | number): boolean {
return [1, "1", "T", "true", "TRUE"].includes(text);
}

/**
Expand All @@ -26,6 +26,7 @@ function BooleanParser(text: string): boolean {
* @returns
*/
function IntegerParser(text: string): number {
text ??= "0";
if (typeof text === "string") {
text = text.replace(/\D/g, ""); // 去除所有非数字的字符
}
Expand All @@ -43,6 +44,7 @@ function IntegerParser(text: string): number {
* @returns
*/
function NumberParser(text: string): number {
text ??= "0";
if (typeof text === "string") {
text = text.replace(/[^\d.]/g, ""); // 去除所有非数字和非小数点的字符
}
Expand All @@ -60,7 +62,7 @@ function NumberParser(text: string): number {
* @returns
*/
function StringParser(text: string): string {
return text;
return text ?? "";
}

/**
Expand All @@ -76,6 +78,9 @@ function PickParser(text: string, params?: string): number {
params!.split(",").forEach((v, i) => {
values[v] = i;
});
if (text == undefined) {
throw new Error("[OP] Not a valid option");
}
let ret = values[text];
if (ret === undefined) {
throw new Error("[OP] Not a valid option");
Expand Down
5 changes: 3 additions & 2 deletions XLSXDumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ function parseHSheet(sheet: { name: string; data: any[] }) {
let header2 = sheet.data[1];
let header3 = sheet.data[2];
let passable = Passable(header2);
let rowLen = passable.length;
console.log(" 正在解析横向表格:" + table);
console.log(" 字段:\n" + ZipHeaders(header1, header2, header3));
for (let l = 3; l < sheet.data.length; l++) {
let row = sheet.data[l];
if (row.length == 0) continue;
let item: Record<string, any> = {};
let primary: string | number | undefined = undefined;
for (let i = 0; i < row.length; i++) {
for (let i = 0; i < rowLen; i++) {
if (!passable[i]) {
item[header2[i]] = Ruler.parse(header3[i], row[i]);
if (primary == undefined) {
Expand All @@ -105,7 +106,7 @@ function parseHSheet(sheet: { name: string; data: any[] }) {
}

/**
* 解析横向表格
* 解析竖向表格
* @param sheet 表格信息
*/
function parseVSheet(sheet: { name: string; data: any[] }) {
Expand Down
5 changes: 5 additions & 0 deletions 安装.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
powershell -c "irm bun.sh/install.ps1 | iex"

bun install

pause
1 change: 1 addition & 0 deletions 清理.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun run index.ts -c
7 changes: 7 additions & 0 deletions 运行.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
REM Build
bun run index.ts -d

REM Merge
bun run index.ts -m

pause

0 comments on commit 05ffebc

Please sign in to comment.