From f1b60f38aff91f59bba7b6e87ba0b51527a3b027 Mon Sep 17 00:00:00 2001 From: Nell Hardcastle Date: Thu, 11 Apr 2024 11:20:40 -0700 Subject: [PATCH] fix: Preserve key properties when using spread to copy BIDSFile objects in browser environments --- bids-validator/src/files/browser.test.ts | 20 +++++++++++++++++++- bids-validator/src/files/browser.ts | 14 +++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bids-validator/src/files/browser.test.ts b/bids-validator/src/files/browser.test.ts index 55478cf12..4e1529ee6 100644 --- a/bids-validator/src/files/browser.test.ts +++ b/bids-validator/src/files/browser.test.ts @@ -1,6 +1,6 @@ import { FileIgnoreRules } from './ignore.ts' import { FileTree } from '../types/filetree.ts' -import { assertEquals } from '../deps/asserts.ts' +import { assertEquals, assertObjectMatch } from '../deps/asserts.ts' import { BIDSFileBrowser, fileListToTree } from './browser.ts' class TestFile extends File { @@ -69,3 +69,21 @@ Deno.test('Browser implementation of FileTree', async (t) => { assertEquals(tree, expectedTree) }) }) + +Deno.test("Spread copies of BIDSFileBrowser contain name and path properties", async () => { + const ignore = new FileIgnoreRules([]) + const files = [ + new TestFile( + ['{}'], + 'dataset_description.json', + 'ds/dataset_description.json', + ), + new TestFile(['flat test dataset'], 'README.md', 'ds/README.md'), + ] + const tree = await fileListToTree(files) + const expectedTree = new FileTree('', '/', undefined) + expectedTree.files = files.map((f) => new BIDSFileBrowser(f, ignore)) + assertEquals(tree, expectedTree) + const spreadFile = {...expectedTree.files[0], evidence: "test evidence"} + assertObjectMatch(spreadFile, {name: "dataset_description.json", path: "/dataset_description.json", evidence: "test evidence"}) +}) \ No newline at end of file diff --git a/bids-validator/src/files/browser.ts b/bids-validator/src/files/browser.ts index 674eb341a..5c21698ae 100644 --- a/bids-validator/src/files/browser.ts +++ b/bids-validator/src/files/browser.ts @@ -9,21 +9,17 @@ import { parse, join, SEPARATOR } from '../deps/path.ts' export class BIDSFileBrowser implements BIDSFile { #ignore: FileIgnoreRules #file: File + name: string + path: string constructor(file: File, ignore: FileIgnoreRules) { this.#file = file this.#ignore = ignore - } - - get name(): string { - return this.#file.name - } - - get path(): string { - // @ts-expect-error webkitRelativePath is defined in the browser + this.name = file.name + // @ts-expect-error webkitRelativePath does exist in the browser const relativePath = this.#file.webkitRelativePath const prefixLength = relativePath.indexOf('/') - return relativePath.substring(prefixLength) + this.path = relativePath.substring(prefixLength) } get size(): number {