mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-17 19:44:45 +08:00
feat: extract parseJSON()
This commit is contained in:
@@ -2,3 +2,5 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
export * from "./time";
|
||||
export * from "./json";
|
||||
export * from "./deep-clone";
|
||||
|
||||
17
web/src/core/utils/json.ts
Normal file
17
web/src/core/utils/json.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { parse } from "best-effort-json-parser";
|
||||
|
||||
export function parseJSON<T>(json: string | null | undefined, fallback: T) {
|
||||
if (!json) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
const raw = json
|
||||
.trim()
|
||||
.replace(/^```json\s*/, "")
|
||||
.replace(/^```\s*/, "")
|
||||
.replace(/\s*```$/, "");
|
||||
return parse(raw) as T;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user