Skip to content

Commit

Permalink
修复已知问题
Browse files Browse the repository at this point in the history
【修复】修复负数解析错误的问题
【修复】修复主键丢失问题
  • Loading branch information
DoooReyn committed Dec 23, 2024
1 parent 2d500f6 commit c88049d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
36 changes: 14 additions & 22 deletions Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export interface IRule<RET> {
transformer: (rule: string, params?: string) => string;
}

/**
* 使用正则表达式确保字符串只包含有效的数字字符(包括前置负号和小数点)
* @param input 输入字符串
* @returns
*/
function convertStringToNumber(input: string): number {
if (typeof input == "number") return input;
const ret = Number(input.replace(/[^\d.-]/g, ""));
return isNaN(ret) ? 0 : ret;
}

/**
* 布尔解析器
* @param text 文本
Expand All @@ -27,17 +38,8 @@ function BooleanParser(text: string | number): boolean {
*/
function IntegerParser(text: string): number {
text ??= "0";
if (typeof text === "string") {
text = text.replace(/\D/g, ""); // 去除所有非数字的字符
}
if (text.length == 0) {
text = "0";
}
let ret = parseInt(text);
if (isNaN(ret)) {
throw new Error(`[I] "${text}" Not a number`);
}
return ret;
const ret = convertStringToNumber(text);
return Math.floor(ret);
}

/**
Expand All @@ -48,17 +50,7 @@ function IntegerParser(text: string): number {
*/
function NumberParser(text: string): number {
text ??= "0";
if (typeof text === "string") {
text = text.replace(/[^\d.]/g, ""); // 去除所有非数字和非小数点的字符
}
if (text.length == 0) {
text = "0";
}
let ret = parseFloat(text);
if (isNaN(ret)) {
throw new Error(`[I] "${text}" Not a number`);
}
return ret;
return convertStringToNumber(text);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion XLSXDumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ function parseHSheet(sheet: { name: string; data: any[] }) {
}
}
}
if (primary) {
if (primary != undefined) {
data[primary] = item;
valid = true;
} else {
throw new Error("表格主键错误");
}
}
if (valid) {
Expand Down

0 comments on commit c88049d

Please sign in to comment.