refactor: 清理代码注释并优化组件结构

- 在 useQrCode.ts 中移除多余的注释,简化代码逻辑。
- 在 DataPreview.tsx 和 FieldEditor.tsx 中删除不必要的注释,提升可读性。
- 在 format.ts 和 jsonToToml.ts 中更新注释内容,确保准确性。
This commit is contained in:
雨霖铃
2026-06-19 01:04:15 +08:00
parent cd21d11fa1
commit c69a26b469
7 changed files with 18 additions and 64 deletions
+7 -11
View File
@@ -11,7 +11,7 @@ const isObject = (v: unknown): v is Record<string, unknown> =>
const isArray = (v: unknown): v is unknown[] => Array.isArray(v);
/**
* 健壮的 JSONPath 生成器:支持针对包含点号、空格或特殊字符的键名进行括号转义拦截
* JSONPath 生成器
*/
const buildPath = (parent: string, key: string, isArrayChild: boolean): string => {
if (isArrayChild) {
@@ -47,7 +47,7 @@ const diffNode = (
newValue: right,
path,
isLeaf: !isObject(right) && !isArray(right),
hasDiffInChildren: false, // 自身即是新增,子树无需向下检索
hasDiffInChildren: false,
};
}
@@ -61,7 +61,7 @@ const diffNode = (
newValue: undefined,
path,
isLeaf: !isObject(left) && !isArray(left),
hasDiffInChildren: false, // 自身即是删除,子树无需向下检索
hasDiffInChildren: false,
};
}
@@ -70,7 +70,6 @@ const diffNode = (
const leftArr = isArray(left);
const rightArr = isArray(right);
// 分支 3:双对象深层递归 (容器状态)
if (leftObj && rightObj) {
const keySet = new Set<string>();
const leftKeys = Object.keys(left);
@@ -97,11 +96,10 @@ const diffNode = (
children,
path,
isLeaf: false,
hasDiffInChildren, // 完美注入预计算衍生状态
hasDiffInChildren,
};
}
// 分支 4:双数组深层按序递归 (容器状态)
if (leftArr && rightArr) {
const len = Math.max(left.length, right.length);
const children: DiffNode[] = new Array(len);
@@ -124,11 +122,10 @@ const diffNode = (
children,
path,
isLeaf: false,
hasDiffInChildren, // 完美注入预计算衍生状态
hasDiffInChildren,
};
}
// 分支 5:绝对类型安全防护大闸 (双基本基元比对)
const leftIsContainer = leftObj || leftArr;
const rightIsContainer = rightObj || rightArr;
@@ -160,7 +157,6 @@ const diffNode = (
}
}
// 类型完全发生突变错配,或者基本数值不相等
diffPaths.push(path);
return {
key,
@@ -169,12 +165,12 @@ const diffNode = (
newValue: right,
path,
isLeaf: !leftIsContainer && !rightIsContainer,
hasDiffInChildren: false, // 变动在自身,后代无子树变动
hasDiffInChildren: false,
};
};
/**
* 比较两个 JSON 值的差异,返回安全的差异树及高精度差异路径列表。
* 比较两个 JSON 值的差异
*/
export const diffJson = (left: unknown, right: unknown): DiffResult => {
const diffPaths: string[] = [];
+1 -2
View File
@@ -2,7 +2,7 @@
* 格式化字节大小为易读字符串
*
* @param bytes 字节数
* @returns 格式化后的字符串,例如 "1.5 KB" 或 "100 B"
* @returns 格式化后的字符串
*/
export function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B';
@@ -17,7 +17,6 @@ export function formatBytes(bytes: number): string {
unitIndex++;
}
// KB uses 1 decimal, MB/GB/TB use 2 decimals
const decimals = unitIndex === 0 ? 1 : 2;
return `${size.toFixed(decimals)} ${units[unitIndex]}`;
+1 -1
View File
@@ -204,7 +204,7 @@ export function jsonToToml(text: string): JsonToTomlResult {
// TOML 要求顶层是表
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error('TOML requires the top-level value to be an object');
throw new Error('TOML 要求顶层值必须是一个对象');
}
const lines: string[] = [];