Skip to content

Commit 3b225de

Browse files
authored
fix(serialize): Map serialization with object keys (#139)
1 parent 6968b45 commit 3b225de

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/serialize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ const Serializer = /*@__PURE__*/ (function () {
121121
throw new Error(`Cannot serialize ${type}`);
122122
}
123123

124-
serializeObjectEntries(type: string, entries: Iterable<[string, any]>) {
124+
serializeObjectEntries(type: string, entries: Iterable<[any, any]>) {
125125
const sortedEntries = Array.from(entries).sort((a, b) =>
126126
this.compare(a[0], b[0]),
127127
);
128128
let content = `${type}{`;
129129
for (let i = 0; i < sortedEntries.length; i++) {
130130
const [key, value] = sortedEntries[i];
131-
content += `${key}:${this.serialize(value)}`;
131+
content += `${this.serialize(key, true)}:${this.serialize(value)}`;
132132
if (i < sortedEntries.length - 1) {
133133
content += ",";
134134
}

test/serialize.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ describe("serialize", () => {
9191

9292
it("map", () => {
9393
const map = new Map();
94+
map.set(1, 4);
95+
map.set(2, 3);
9496
map.set("z", 2);
9597
map.set("a", "1");
96-
expect(serialize(map)).toMatchInlineSnapshot(`"Map{a:'1',z:2}"`);
98+
map.set({ x: 42 }, "3");
99+
100+
expect(serialize(map)).toMatchInlineSnapshot(
101+
`"Map{{x:42}:'3',1:4,2:3,a:'1',z:2}"`,
102+
);
97103
});
98104
});
99105

@@ -372,6 +378,12 @@ describe("serialize", () => {
372378
expect(serialize(map)).toMatchInlineSnapshot(`"Map{key:#0}"`);
373379
});
374380

381+
it("handles circular references within keys of Map objects", () => {
382+
const map = new Map();
383+
map.set(map, "value");
384+
expect(serialize(map)).toMatchInlineSnapshot(`"Map{#0:'value'}"`);
385+
});
386+
375387
it("handles circular references within Set objects", () => {
376388
const set = new Set();
377389
set.add(set);

0 commit comments

Comments
 (0)