Skip to content

Commit

Permalink
fix: add checks for undefined types before instance checks in objectP…
Browse files Browse the repository at this point in the history
…lain function
  • Loading branch information
holtwick committed Nov 8, 2024
1 parent dc07390 commit 8a939bf
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/common/data/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,24 @@ export function objectPlain(obj: any, opt?: {
if (isPrimitive(obj))
return obj

if (obj instanceof Date) {
if (typeof Date !== 'undefined' && obj instanceof Date) {
return {
__class: 'Date',
value: obj.toISOString(),
}
}

if (obj instanceof RegExp) {
if (typeof RegExp !== 'undefined' && obj instanceof RegExp) {
return {
__class: 'RegExp',
source: obj.toString(),
}
}

if (obj instanceof Map)
if (typeof Map !== 'undefined' && obj instanceof Map)
obj = Object.fromEntries(obj)

if (obj instanceof Set || isBinaryArray(obj))
if (typeof Set !== 'undefined' && obj instanceof Set || isBinaryArray(obj))
obj = Array.from(obj as any)

if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent) {
Expand All @@ -141,7 +141,7 @@ export function objectPlain(obj: any, opt?: {
}
}

if (obj instanceof Event) {
if (typeof Event !== 'undefined' && obj instanceof Event) {
return {
__class: 'Event',
type: obj.type,
Expand All @@ -150,17 +150,17 @@ export function objectPlain(obj: any, opt?: {
cancelable: obj.cancelable,
defaultPrevented: obj.defaultPrevented,
composed: obj.composed,
timeStamp: obj.timeStamp,
timeStamp: obj.timeStamp,
}
}

if (obj instanceof Error) {
if (typeof Error !== 'undefined' && obj instanceof Error) {
return {
__class: 'Error',
name: obj.name,
message: obj.message,
stack: errorTrace ? obj.stack : undefined,
cause: (obj as any).cause ? String((obj as any).cause) : undefined,
cause: (obj as any).cause ? String((obj as any).cause) : undefined,
}
}
// return `${obj.name || 'Error'}: ${obj.message}${errorTrace ? `\n${obj.stack}` : ''}`
Expand Down

0 comments on commit 8a939bf

Please sign in to comment.