@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.cjs.js +8 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +8 -2
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs.js
CHANGED
|
@@ -928,18 +928,24 @@ var findTreeNodesByIds = (tree, ids, formatFn, options = {}) => {
|
|
|
928
928
|
dfs(tree);
|
|
929
929
|
return result;
|
|
930
930
|
};
|
|
931
|
-
function
|
|
931
|
+
function looksLikeStructuredJson(raw) {
|
|
932
|
+
const normalized = raw.trim();
|
|
933
|
+
if (!normalized) return false;
|
|
934
|
+
return normalized.startsWith("{") || normalized.startsWith("[");
|
|
935
|
+
}
|
|
936
|
+
function safeParseJson(raw = "", options = {}) {
|
|
932
937
|
const shouldRepair = options.repair ?? false;
|
|
933
938
|
const fallbackMode = options.fallback ?? "raw";
|
|
934
939
|
const getFallbackValue = () => fallbackMode === "raw" ? raw : null;
|
|
935
940
|
try {
|
|
936
941
|
return JSON.parse(raw);
|
|
937
942
|
} catch {
|
|
938
|
-
if (!shouldRepair) return getFallbackValue();
|
|
943
|
+
if (!shouldRepair || !looksLikeStructuredJson(raw)) return getFallbackValue();
|
|
939
944
|
}
|
|
940
945
|
try {
|
|
941
946
|
return JSON.parse(jsonrepair.jsonrepair(raw));
|
|
942
947
|
} catch {
|
|
948
|
+
console.error("jsonrepair error:", raw);
|
|
943
949
|
return getFallbackValue();
|
|
944
950
|
}
|
|
945
951
|
}
|