From fc91dd3171bf8880eae949278aa3f389360d71c7 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Mon, 5 Oct 2020 15:28:29 +0000 Subject: [PATCH] fix(core): update --- ts/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ts/index.ts b/ts/index.ts index 63b7e65..2bdc446 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -49,8 +49,17 @@ export class Smartjson { */ public foldToObject() { const newFoldedObject: { [key: string]: any } = {}; + const trackMap = []; for (const keyName of this.saveableProperties) { - newFoldedObject[keyName] = plugins.lodashCloneDeep(this[keyName]); + let value = this[keyName]; + if (value instanceof Smartjson) { + if (trackMap.includes(value)) { + throw new Error('cycle detected'); + } + trackMap.push(value); + value = value.foldToObject(); + } + newFoldedObject[keyName] = plugins.lodashCloneDeep(value); } return newFoldedObject; }