Skip to content

Commit

Permalink
Merge pull request bids-standard#1928 from nellh/browser-file-fixes
Browse files Browse the repository at this point in the history
fix: Preserve key properties when using spread to copy BIDSFile objects in browser environments
  • Loading branch information
rwblair committed Apr 19, 2024
2 parents 32c5fea + f1b60f3 commit 0e11bdb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
20 changes: 19 additions & 1 deletion bids-validator/src/files/browser.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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"})
})
14 changes: 5 additions & 9 deletions bids-validator/src/files/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions bids-validator/src/schema/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class BIDSContext implements Context {
if (this.extension !== '.tsv') {
return
}

this.columns = await this.file
.text()
.then((text) => parseTSV(text))
Expand Down

0 comments on commit 0e11bdb

Please sign in to comment.