better-auth
Version:
The most comprehensive authentication library for TypeScript.
22 lines (20 loc) • 474 B
JavaScript
function safeJSONParse(data) {
function reviver(_, value) {
if (typeof value === "string") {
const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
if (iso8601Regex.test(value)) {
const date = new Date(value);
if (!isNaN(date.getTime())) {
return date;
}
}
}
return value;
}
try {
return JSON.parse(data, reviver);
} catch {
return null;
}
}
export { safeJSONParse as s };