From a2ee4139ad21836ca6113d7cb2d744e197c212eb Mon Sep 17 00:00:00 2001 From: "Nichols, Kieran" Date: Thu, 9 Nov 2023 10:48:29 -0500 Subject: [PATCH] fix: updated immutability patterns to avoid using the older `JSON.parse(JSON.stringify())` approach in favor of `structureClone()` and standard shallow copying to better handle circular references --- src/lib/autocomplete/autocomplete-foundation.ts | 4 ++-- src/lib/menu/menu-foundation.ts | 4 ---- src/lib/stepper/stepper/stepper-foundation.ts | 2 +- src/lib/table/table-foundation.ts | 6 +----- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/lib/autocomplete/autocomplete-foundation.ts b/src/lib/autocomplete/autocomplete-foundation.ts index 1a37d369c..ba5b55bca 100644 --- a/src/lib/autocomplete/autocomplete-foundation.ts +++ b/src/lib/autocomplete/autocomplete-foundation.ts @@ -803,11 +803,11 @@ export class AutocompleteFoundation extends ListDropdownAwareFoundation implemen if (value == null) { values = []; } else if (Array.isArray(value)) { - values = JSON.parse(JSON.stringify(value)); + values = structuredClone(value); } else if (isString(value)) { values = [value]; } else { - values = [JSON.parse(JSON.stringify(value))]; + values = [structuredClone(value)]; } const hasNewValue = values.length !== this._values.length || values.some(v => !this._values.includes(v)); diff --git a/src/lib/menu/menu-foundation.ts b/src/lib/menu/menu-foundation.ts index 881339620..ae5ad4cd4 100644 --- a/src/lib/menu/menu-foundation.ts +++ b/src/lib/menu/menu-foundation.ts @@ -501,8 +501,6 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation ({ ...o })); if (this._open) { @@ -523,8 +521,6 @@ export class MenuFoundation extends CascadingListDropdownAwareFoundation ({ ...o })); } diff --git a/src/lib/stepper/stepper/stepper-foundation.ts b/src/lib/stepper/stepper/stepper-foundation.ts index a2e46d03d..887cd89dd 100644 --- a/src/lib/stepper/stepper/stepper-foundation.ts +++ b/src/lib/stepper/stepper/stepper-foundation.ts @@ -76,7 +76,7 @@ export class StepperFoundation implements IStepperFoundation { /** The step configurations. */ public get steps(): IStepConfiguration[] { - return JSON.parse(JSON.stringify(this._steps)); + return this._steps.map(s => ({ ...s })); // Shallow clone } public set steps(value: IStepConfiguration[]) { if (Array.isArray(value) && value.length > 0) { diff --git a/src/lib/table/table-foundation.ts b/src/lib/table/table-foundation.ts index f0ad422aa..dbca26d49 100644 --- a/src/lib/table/table-foundation.ts +++ b/src/lib/table/table-foundation.ts @@ -183,14 +183,12 @@ export class TableFoundation implements ITableFoundation { this._renderBody(); } public get data(): any[] { - return JSON.parse(JSON.stringify(this._data)); + return this._data.map(o => ({ ...o })); // Shallow clone } /** The column configuration options. */ public set columnConfigurations(value: IColumnConfiguration[]) { // Intentional shallow copy of member properties. These member objects have properties that are references to functions. - // The typical JSON.parse(JSON.stringify(object)) will not work here. If this becomes an issue we'll add a deepClone - // function to the core library. this._columnConfigurations = value.map(cc => ({ ...cc })); // Update hidden column manager @@ -206,8 +204,6 @@ export class TableFoundation implements ITableFoundation { } public get columnConfigurations(): IColumnConfiguration[] { // Intentional shallow copy of member properties. These member objects have properties that are references to functions. - // The typical JSON.parse(JSON.stringify(object)) will not work here. If this becomes an issue we'll add a deepClone - // function to the core library. return this._columnConfigurations.map(cc => ({ ...cc })); }