From 65c4f959e807d182f16c8dfd2893e094c1cb8692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=40Siemienik=20Pawe=C5=82?= Date: Mon, 20 Nov 2023 23:14:55 +0100 Subject: [PATCH 1/4] Using Proxy to prevent against modification the original view model. Fixes https://github.com/Siemienik/XToolset/issues/137 Fixes https://github.com/Siemienik/XToolset/issues/229 --- packages/xlsx-renderer/src/Renderer.ts | 14 +++-------- packages/xlsx-renderer/src/ViewModel.ts | 21 ++++++++++++++++ .../tests/spec/cell-value-type.test.ts | 25 +++++++++++++++++++ 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 packages/xlsx-renderer/tests/spec/cell-value-type.test.ts diff --git a/packages/xlsx-renderer/src/Renderer.ts b/packages/xlsx-renderer/src/Renderer.ts index 5fb357c..b4c5a0a 100644 --- a/packages/xlsx-renderer/src/Renderer.ts +++ b/packages/xlsx-renderer/src/Renderer.ts @@ -2,6 +2,7 @@ import { Workbook } from 'exceljs'; import { Scope } from './Scope'; import { CellTemplatePool } from './CellTemplatePool'; +import { createVmProxyHandler } from './ViewModel'; export class Renderer { constructor(private cellTemplatePool: CellTemplatePool = new CellTemplatePool()) {} @@ -10,10 +11,7 @@ export class Renderer { const template = await templateFactory(); const output = await templateFactory(); - // todo Temporary fixation for VM mutating problem, @see https://github.com/Siemienik/XToolset/issues/137 - const vmCopy = JSON.parse(JSON.stringify(vm)); - - const scope = new Scope(template, output, vmCopy); + const scope = new Scope(template, output, new Proxy(vm, createVmProxyHandler())); while (!scope.isFinished()) { this.cellTemplatePool.match(scope.getCurrentTemplateCell()).apply(scope); @@ -23,20 +21,16 @@ export class Renderer { } public async renderFromFile(templatePath: string, viewModel: unknown): Promise { - const result = await this.render(async () => { + return this.render(async () => { const template = new Workbook(); return await template.xlsx.readFile(templatePath); }, viewModel); - - return await result; } public async renderFromArrayBuffer(templateArrayBuffer: ArrayBuffer, viewModel: unknown): Promise { - const result = await this.render(async () => { + return this.render(async () => { const template = new Workbook(); return await template.xlsx.load(templateArrayBuffer); }, viewModel); - - return await result; } } diff --git a/packages/xlsx-renderer/src/ViewModel.ts b/packages/xlsx-renderer/src/ViewModel.ts index e5671f0..09481fd 100644 --- a/packages/xlsx-renderer/src/ViewModel.ts +++ b/packages/xlsx-renderer/src/ViewModel.ts @@ -1 +1,22 @@ export type ViewModel = any; + +export const createVmProxyHandler = ()=> { + const data: Record = {}; + + return { + get(target: any, p: PropertyKey): any { + if (typeof p !== 'string' && typeof p !== 'number') { + return; + } + return p in data ? data[p] : target[p] + }, + set(target: unknown, p: PropertyKey, value: unknown): boolean { + if (typeof p !== 'string' && typeof p !== 'number') { + return false; + } + data[p] = value + + return true + }, + } +} diff --git a/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts b/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts new file mode 100644 index 0000000..e8a956d --- /dev/null +++ b/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts @@ -0,0 +1,25 @@ +import * as chai from 'chai'; +import { CellValue, Workbook } from 'exceljs'; +import { ValueType } from 'exceljs'; +import { Renderer } from '../../src/Renderer'; + +const WS_NAME = 'test ws'; + +describe('BaseCell unit tests', () => { + const factory = async ():Promise =>{ + const template = new Workbook(); + template.addWorksheet(WS_NAME).addRow(['## testVar', "#! FINISH"]); + + return template; + } + + it('DateTime', async () => { + const viewModel = { + testVar: new Date(2023, 11, 17, 21, 37) + } + const renderer = new Renderer(); + const output = await renderer.render(factory, viewModel) + + chai.expect(output.getWorksheet(WS_NAME)?.getCell('A1').type).equals(ValueType.Date); + }); +}); From f56860111486ae84fe9cfa6823cb366614763417 Mon Sep 17 00:00:00 2001 From: "[CI] Linter" Date: Mon, 20 Nov 2023 22:19:29 +0000 Subject: [PATCH 2/4] [CI] Linter: markdown formatted --- package-lock.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7aeac2a..525e7e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -222,14 +222,14 @@ "dev": true }, "run-con": { - "version": "1.2.11", - "resolved": "https://registry.npmjs.org/run-con/-/run-con-1.2.11.tgz", - "integrity": "sha512-NEMGsUT+cglWkzEr4IFK21P4Jca45HqiAbIIZIBdX5+UZTB24Mb/21iNGgz9xZa8tL6vbW7CXmq7MFN42+VjNQ==", + "version": "1.2.12", + "resolved": "https://registry.npmjs.org/run-con/-/run-con-1.2.12.tgz", + "integrity": "sha512-5257ILMYIF4RztL9uoZ7V9Q97zHtNHn5bN3NobeAnzB1P3ASLgg8qocM2u+R18ttp+VEM78N2LK8XcNVtnSRrg==", "dev": true, "requires": { "deep-extend": "^0.6.0", "ini": "~3.0.0", - "minimist": "^1.2.6", + "minimist": "^1.2.8", "strip-json-comments": "~3.1.1" } }, From d821a8b3a8e7e9b49b69c1ba44b6140cf81fb9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=40Siemienik=20Pawe=C5=82?= Date: Mon, 20 Nov 2023 23:58:17 +0100 Subject: [PATCH 3/4] cast to string, as Symbol is not predicted in that usecase --- packages/xlsx-renderer/src/ViewModel.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/xlsx-renderer/src/ViewModel.ts b/packages/xlsx-renderer/src/ViewModel.ts index 09481fd..ed4c4ec 100644 --- a/packages/xlsx-renderer/src/ViewModel.ts +++ b/packages/xlsx-renderer/src/ViewModel.ts @@ -5,16 +5,10 @@ export const createVmProxyHandler = ()=> { return { get(target: any, p: PropertyKey): any { - if (typeof p !== 'string' && typeof p !== 'number') { - return; - } - return p in data ? data[p] : target[p] + return p in data ? data[p as string] : target[p] }, - set(target: unknown, p: PropertyKey, value: unknown): boolean { - if (typeof p !== 'string' && typeof p !== 'number') { - return false; - } - data[p] = value + set(target: any, p: PropertyKey, value: unknown): boolean { + data[p as string] = value return true }, From f98db5ab7a1577a974f215ae83788ecae79ba57d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=40Siemienik=20Pawe=C5=82?= Date: Tue, 21 Nov 2023 00:00:21 +0100 Subject: [PATCH 4/4] clear code-smells --- packages/xlsx-renderer/tests/spec/cell-value-type.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts b/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts index e8a956d..905659a 100644 --- a/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts +++ b/packages/xlsx-renderer/tests/spec/cell-value-type.test.ts @@ -1,6 +1,5 @@ import * as chai from 'chai'; -import { CellValue, Workbook } from 'exceljs'; -import { ValueType } from 'exceljs'; +import { ValueType, Workbook } from 'exceljs'; import { Renderer } from '../../src/Renderer'; const WS_NAME = 'test ws';