From e40da033a91ef33b0cbb433ce8a5217226d80089 Mon Sep 17 00:00:00 2001 From: Scott Newcomer Date: Sat, 30 Jan 2021 22:43:46 -0600 Subject: [PATCH] [Bug]: prevent cyclical issues with normalizeObject (#99) --- src/utils/normalize-object.ts | 5 +++++ test/utils/normalize-object.test.ts | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/utils/normalize-object.ts b/src/utils/normalize-object.ts index 260b064..ec20168 100644 --- a/src/utils/normalize-object.ts +++ b/src/utils/normalize-object.ts @@ -42,6 +42,11 @@ export default function normalizeObject( 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); } } diff --git a/test/utils/normalize-object.test.ts b/test/utils/normalize-object.test.ts index f809618..faa5fb4 100644 --- a/test/utils/normalize-object.test.ts +++ b/test/utils/normalize-object.test.ts @@ -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'); + }); });