Skip to content

Commit

Permalink
[Bug]: prevent cyclical issues with normalizeObject (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored Jan 31, 2021
1 parent c753cb6 commit e40da03
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/utils/normalize-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export default function normalizeObject<T extends { [key: string]: any }>(
if (Object.prototype.hasOwnProperty.call(next, 'value') && next instanceof Change) {
obj[key] = next.value;
} else {
try {
JSON.stringify(next);
} catch (e) {
break;
}
obj[key] = normalizeObject(next);
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/utils/normalize-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ describe('Unit | Utility | normalize object', () => {

expect(value).toEqual({ details: { name: 'Ivan' } });
});

it('it handles cyclical structures', () => {
let cyclical = { key: { cyclical: {}, name: 'not scoot' } };
cyclical.key = { cyclical, name: 'scoot' };
let obj = { cyclical };
const value = normalizeObject(obj);

expect(value.cyclical.key.name).toEqual('scoot');
});
});

0 comments on commit e40da03

Please sign in to comment.