Skip to content

Commit d778802

Browse files
authored
Merge pull request #2054 from effigies/feat/file-relative
feat: Add `exists(..., 'file')` for file-relative paths
2 parents 3a7335d + 5ece19e commit d778802

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

bids-validator/src/schema/expressionLanguage.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ Deno.test('test expression functions', async (t) => {
9191
assert(count(['a', 'b', 'a', 'b'], 'a') === 2)
9292
assert(count(['a', 'b', 'a', 'b'], 'c') === 0)
9393
})
94+
95+
const exists = expressionFunctions.exists.bind(context)
9496
await t.step('exists(..., "dataset") function', () => {
95-
const exists = expressionFunctions.exists.bind(context)
9697
assert(exists([], 'dataset') === 0)
9798
assert(
9899
exists(['sub-01/ses-01/anat/sub-01_ses-01_T1w.nii.gz'], 'dataset') === 1,
@@ -105,7 +106,6 @@ Deno.test('test expression functions', async (t) => {
105106
)
106107
})
107108
await t.step('exists(..., "subject") function', () => {
108-
const exists = expressionFunctions.exists.bind(context)
109109
assert(exists([], 'subject') === 0)
110110
assert(exists(['ses-01/anat/sub-01_ses-01_T1w.nii.gz'], 'subject') === 1)
111111
assert(
@@ -115,15 +115,19 @@ Deno.test('test expression functions', async (t) => {
115115
) === 1,
116116
)
117117
})
118+
await t.step('exists(..., "file") function', () => {
119+
assert(exists([], 'file') === 0)
120+
assert(exists(['sub-01_ses-01_T1w.nii.gz'], 'file') === 1)
121+
assert(exists(['sub-01_ses-01_T1w.nii.gz', 'sub-01_ses-01_T1w.json'], 'file') === 2)
122+
assert(exists(['sub-01_ses-01_T1w.nii.gz', 'ses-01_T1w.json'], 'file') === 1)
123+
})
118124
await t.step('exists(..., "stimuli") function', () => {
119-
const exists = expressionFunctions.exists.bind(context)
120125
assert(exists([], 'stimuli') === 0)
121126
assert(exists(['stimfile1.png'], 'stimuli') === 1)
122127
assert(exists(['stimfile1.png', 'stimfile2.png'], 'stimuli') === 2)
123128
assert(exists(['X.png', 'Y.png'], 'stimuli') === 0)
124129
})
125130
await t.step('exists(..., "bids-uri") function', () => {
126-
const exists = expressionFunctions.exists.bind(context)
127131
assert(exists([], 'subject') === 0)
128132
assert(
129133
exists(
@@ -140,6 +144,7 @@ Deno.test('test expression functions', async (t) => {
140144
// Not yet implemented; currently returns length of array
141145
// assert(exists(['bids::sub-01/ses-01/anat/sub-01_ses-01_T1w.nii.gz', 'bids::T2w.json'], 'bids-uri') === 1)
142146
})
147+
143148
await t.step('substr function', () => {
144149
const substr = expressionFunctions.substr
145150
assert(substr('abc', 0, 1) === 'a')

bids-validator/src/schema/expressionLanguage.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
function exists(list: string[], rule: string = 'dataset'): number {
1+
import { BIDSContext } from './context.ts'
2+
3+
function exists(this: BIDSContext, list: string[], rule: string = 'dataset'): number {
24
if (list == null) {
35
return 0
46
}
57

68
const prefix: string[] = []
9+
const fileTree = rule == 'file' ? this.file.parent : this.fileTree
710

811
// Stimuli and subject-relative paths get prefixes
912
if (rule == 'stimuli') {
1013
prefix.push('stimuli')
1114
} else if (rule == 'subject') {
12-
// @ts-expect-error
1315
prefix.push('sub-' + this.entities.sub)
1416
}
1517

@@ -28,8 +30,7 @@ function exists(list: string[], rule: string = 'dataset'): number {
2830
// dataset, subject and stimuli
2931
return list.filter((x) => {
3032
const parts = prefix.concat(x.split('/'))
31-
// @ts-expect-error
32-
return this.fileTree.contains(parts)
33+
return fileTree.contains(parts)
3334
}).length
3435
}
3536
}

0 commit comments

Comments
 (0)