Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/accessDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ export const setDeep = (
}

if (isSet(parent)) {
const oldValue = getNthKey(parent, +lastKey);
const index = +lastKey;
const oldValue = getNthKey(parent, index);
const newValue = mapper(oldValue);
if (oldValue !== newValue) {
parent.delete(oldValue);
parent.add(newValue);
const values = [...parent];
values[index] = newValue;
parent.clear();
values.forEach(value => parent.add(value));
}
}

Expand All @@ -122,10 +125,12 @@ export const setDeep = (
switch (type) {
case 'key': {
const newKey = mapper(keyToRow);
parent.set(newKey, parent.get(keyToRow));

if (newKey !== keyToRow) {
parent.delete(keyToRow);
const entries = [...parent];
parent.clear();
entries.forEach(([k, v]) =>
parent.set(k === keyToRow ? newKey : k, v)
);
}
break;
}
Expand Down
37 changes: 37 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,43 @@ test('regression #245: superjson referential equalities only use the top-most pa
expect(parsed).toEqual(input);
});

test('restores multiple referentially equal values inside the same Set', () => {
const a = { tag: 'a' };
const b = { tag: 'b' };
const input = {
set: new Set([a, b]),
a,
b,
};

const output: any = SuperJSON.deserialize(SuperJSON.serialize(input));

expect([...output.set]).toEqual([a, b]);
expect(output.set.has(output.a)).toBe(true);
expect(output.set.has(output.b)).toBe(true);
expect([...output.set][0]).toBe(output.a);
expect([...output.set][1]).toBe(output.b);
});

test('restores multiple referentially equal keys inside the same Map', () => {
const a = { tag: 'a' };
const b = { tag: 'b' };
const input = {
map: new Map<any, any>([
[a, 1],
[b, 2],
]),
a,
b,
};

const output: any = SuperJSON.deserialize(SuperJSON.serialize(input));

expect([...output.map.keys()]).toEqual([a, b]);
expect(output.map.get(output.a)).toBe(1);
expect(output.map.get(output.b)).toBe(2);
});

test('dedupe=true', () => {
const instance = new SuperJSON({
dedupe: true,
Expand Down