Skip to content

Commit 7285d36

Browse files
committed
warn & pass through non objects in event-to-object
1 parent ea40d18 commit 7285d36

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/js/packages/event-to-object/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ export default function convert(
77
classObject: { [key: string]: any },
88
maxDepth: number = 10,
99
): object {
10-
const visited = new WeakSet<any>();
11-
visited.add(classObject);
10+
// Immediately return `classObject` if given an unexpected (non-object) input
11+
if (!classObject || typeof classObject !== "object") {
12+
console.warn(
13+
"eventToObject: Expected an object input, received:",
14+
classObject,
15+
);
16+
return classObject;
17+
}
1218

1319
// Begin conversion
20+
const visited = new WeakSet<any>();
21+
visited.add(classObject);
1422
const convertedObj: { [key: string]: any } = {};
1523
for (const key in classObject) {
1624
// Skip keys that cannot be converted

src/js/packages/event-to-object/tests/event-to-object.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,10 @@ test("handles recursive HTML node structures", () => {
670670
expect(converted.children[0].parentNode).toBeUndefined();
671671
}
672672
});
673+
674+
test("pass-through on unexpected non-object inputs", () => {
675+
expect(convert(null as any)).toEqual(null);
676+
expect(convert(undefined as any)).toEqual(undefined);
677+
expect(convert(42 as any)).toEqual(42);
678+
expect(convert("test" as any)).toEqual("test");
679+
});

0 commit comments

Comments
 (0)