From 5e2c0b2ef060cc98fe74ee4e78318cf22ab8a1f5 Mon Sep 17 00:00:00 2001 From: "Adrien Minne (adrm)" Date: Wed, 19 Apr 2023 13:45:04 +0000 Subject: [PATCH] [IMP] *: use deepCopy instead of JSON.parse(JSON.stringify) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes odoo/o-spreadsheet#2374 Task: 3272878 X-original-commit: 8dbed8b20c104c40782ad13cce2fdb921d677cb4 Signed-off-by: Minne Adrien (adrm) Signed-off-by: RĂ©mi Rahir (rar) --- src/migrations/data.ts | 4 ++-- src/model.ts | 4 ++-- src/plugins/core/sheet.ts | 3 ++- tests/__mocks__/transport_service.ts | 5 +++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/migrations/data.ts b/src/migrations/data.ts index c352c300e4..7bec8993b5 100644 --- a/src/migrations/data.ts +++ b/src/migrations/data.ts @@ -4,7 +4,7 @@ import { FORBIDDEN_IN_EXCEL_REGEX, FORMULA_REF_IDENTIFIER, } from "../constants"; -import { getItemId, toXC, toZone, UuidGenerator } from "../helpers/index"; +import { deepCopy, getItemId, toXC, toZone, UuidGenerator } from "../helpers/index"; import { StateUpdateMessage } from "../types/collaborative/transport_service"; import { CoreCommand, @@ -46,7 +46,7 @@ export function load(data?: any, verboseImport?: boolean): WorkbookData { } } } - data = JSON.parse(JSON.stringify(data)); + data = deepCopy(data); // apply migrations, if needed if ("version" in data) { diff --git a/src/model.ts b/src/model.ts index 9d9e6be0cd..5f2169bd76 100644 --- a/src/model.ts +++ b/src/model.ts @@ -572,7 +572,7 @@ export class Model extends EventBus implements CommandDispatcher { } } data.revisionId = this.session.getRevisionId() || DEFAULT_REVISION_ID; - data = JSON.parse(JSON.stringify(data)); + data = deepCopy(data); return data; } @@ -602,7 +602,7 @@ export class Model extends EventBus implements CommandDispatcher { handler.exportForExcel(data); } } - data = JSON.parse(JSON.stringify(data)); + data = deepCopy(data); return getXLSX(data); } diff --git a/src/plugins/core/sheet.ts b/src/plugins/core/sheet.ts index 194f3bc99c..652e00b700 100644 --- a/src/plugins/core/sheet.ts +++ b/src/plugins/core/sheet.ts @@ -1,6 +1,7 @@ import { FORBIDDEN_IN_EXCEL_REGEX } from "../../constants"; import { createDefaultRows, + deepCopy, getUnquotedSheetName, groupConsecutive, isDefined, @@ -651,7 +652,7 @@ export class SheetPlugin extends CorePlugin implements SheetState { private duplicateSheet(fromId: UID, toId: UID) { const sheet = this.getSheet(fromId); const toName = this.getDuplicateSheetName(sheet.name); - const newSheet: Sheet = JSON.parse(JSON.stringify(sheet)); + const newSheet: Sheet = deepCopy(sheet); newSheet.id = toId; newSheet.name = toName; for (let col = 0; col <= newSheet.numberOfCols; col++) { diff --git a/tests/__mocks__/transport_service.ts b/tests/__mocks__/transport_service.ts index c835520f63..dfe74b92c5 100644 --- a/tests/__mocks__/transport_service.ts +++ b/tests/__mocks__/transport_service.ts @@ -1,4 +1,5 @@ import { DEFAULT_REVISION_ID } from "../../src/constants"; +import { deepCopy } from "../../src/helpers"; import { UID, WorkbookData } from "../../src/types"; import { CollaborationMessage, @@ -18,7 +19,7 @@ export class MockTransportService implements TransportService