@zero-library/common 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1545,7 +1545,7 @@ interface SafeParseJsonOptions {
1545
1545
  * @param options - 解析配置
1546
1546
  * @returns 解析成功时返回对象;失败时返回原字符串或 `null`
1547
1547
  */
1548
- declare function safeParseJson<T = any>(raw: string, options?: SafeParseJsonOptions): T | string | null;
1548
+ declare function safeParseJson<T = any>(raw?: string, options?: SafeParseJsonOptions): T | string | null;
1549
1549
 
1550
1550
  type DateType = string | number | Date | dayjs.Dayjs | null;
1551
1551
  /** 默认日期时间格式:年-月-日 时:分:秒 */
package/dist/index.d.ts CHANGED
@@ -1545,7 +1545,7 @@ interface SafeParseJsonOptions {
1545
1545
  * @param options - 解析配置
1546
1546
  * @returns 解析成功时返回对象;失败时返回原字符串或 `null`
1547
1547
  */
1548
- declare function safeParseJson<T = any>(raw: string, options?: SafeParseJsonOptions): T | string | null;
1548
+ declare function safeParseJson<T = any>(raw?: string, options?: SafeParseJsonOptions): T | string | null;
1549
1549
 
1550
1550
  type DateType = string | number | Date | dayjs.Dayjs | null;
1551
1551
  /** 默认日期时间格式:年-月-日 时:分:秒 */
package/dist/index.esm.js CHANGED
@@ -880,18 +880,24 @@ var findTreeNodesByIds = (tree, ids, formatFn, options = {}) => {
880
880
  dfs(tree);
881
881
  return result;
882
882
  };
883
- function safeParseJson(raw, options = {}) {
883
+ function looksLikeStructuredJson(raw) {
884
+ const normalized = raw.trim();
885
+ if (!normalized) return false;
886
+ return normalized.startsWith("{") || normalized.startsWith("[");
887
+ }
888
+ function safeParseJson(raw = "", options = {}) {
884
889
  const shouldRepair = options.repair ?? false;
885
890
  const fallbackMode = options.fallback ?? "raw";
886
891
  const getFallbackValue = () => fallbackMode === "raw" ? raw : null;
887
892
  try {
888
893
  return JSON.parse(raw);
889
894
  } catch {
890
- if (!shouldRepair) return getFallbackValue();
895
+ if (!shouldRepair || !looksLikeStructuredJson(raw)) return getFallbackValue();
891
896
  }
892
897
  try {
893
898
  return JSON.parse(jsonrepair(raw));
894
899
  } catch {
900
+ console.error("jsonrepair error:", raw);
895
901
  return getFallbackValue();
896
902
  }
897
903
  }