Skip to content

Commit

Permalink
Merge pull request #125 from hed-standard/curly-braces
Browse files Browse the repository at this point in the history
Implement support for curly braces
  • Loading branch information
happy5214 authored Dec 15, 2023
2 parents 9a70218 + 6cad002 commit 0e08352
Show file tree
Hide file tree
Showing 36 changed files with 2,935 additions and 1,553 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 17.x, 18.x]
node-version: [18.x, 20.x, 21.x]

steps:
- name: Check out the code
Expand Down
14 changes: 4 additions & 10 deletions bids/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
BidsDataset,
BidsEventFile,
BidsTabularFile,
BidsHedIssue,
BidsTsvFile,
BidsIssue,
BidsJsonFile,
BidsSidecar,
} from './types'
import { validateBidsDataset } from './validate'
import { BidsJsonFile, BidsSidecar } from './types/json'
import { BidsEventFile, BidsTabularFile, BidsTsvFile } from './types/tsv'
import { BidsDataset } from './types/dataset'
import { BidsHedIssue, BidsIssue } from './types/issues'

export {
BidsDataset,
Expand Down
9 changes: 8 additions & 1 deletion bids/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { asArray } from '../utils/array'

const alphanumericRegExp = new RegExp('^[a-zA-Z0-9]+$')

/**
* Build a HED schema collection based on the defined BIDS schemas.
*
* @param {BidsDataset} dataset The BIDS dataset being validated.
* @param {object} schemaDefinition The version spec for the schema to be loaded.
* @returns {Promise<[Schemas,Issue[]]>|Promise<never>} A Promise with the schema collection and any issues found, or an issue list upon failure.
*/
export function buildBidsSchemas(dataset, schemaDefinition) {
let schemasSpec
let issues
Expand All @@ -25,7 +32,7 @@ export function buildBidsSchemas(dataset, schemaDefinition) {
}

export function validateSchemasSpec(schemasSpec) {
// ToDO: implement
// TODO: implement
if (schemasSpec instanceof SchemasSpec) {
return [schemasSpec, []]
} else if (schemasSpec instanceof Map) {
Expand Down
53 changes: 42 additions & 11 deletions bids/tsvParser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Module for parsing TSV files.
*
* Copied from https://github.com/bids-standard/bids-validator/blob/6fc6d152b52266934575442e61f1477ba18f42ec/bids-validator/validators/tsv/tsvParser.js
* Adapted from https://github.com/bids-standard/bids-validator/blob/6fc6d152b52266934575442e61f1477ba18f42ec/bids-validator/validators/tsv/tsvParser.js
* and https://github.com/bids-standard/bids-validator/blob/a5c63b445e3103bcc0843deac192033a9f0b4c5b/bids-validator/src/files/tsv.ts
*/

const stripBOM = (str) => str.replace(/^\uFEFF/, '')
Expand All @@ -12,20 +13,50 @@ const isContentfulRow = (row) => row && !/^\s*$/.test(row)
* Parse a TSV file.
*
* @param {string} contents The contents of a TSV file.
* @returns {{headers: string[], rows: string[][]}} The parsed contents of the TSV file.
* @returns {Map<string, string[]>} The parsed contents of the TSV file.
*/
function parseTSV(contents) {
const content = {
headers: [],
rows: [],
}
contents = stripBOM(contents)
content.rows = normalizeEOL(contents)
export function parseTSV(contents) {
const columns = new Map()
const rows = stripBOM(normalizeEOL(contents))
.split('\n')
.filter(isContentfulRow)
.map((str) => str.split('\t'))
content.headers = content.rows.length ? content.rows[0] : []
return content
const headers = rows.length ? rows[0] : []

headers.forEach((x) => {
columns.set(x, [])
})
for (let i = 1; i < rows.length; i++) {
for (let j = 0; j < headers.length; j++) {
columns.get(headers[j])?.push(rows[i][j])
}
}
for (const [key, value] of columns) {
columns.set(key, value)
}
return columns
}
/**
* Convert parsed TSV file data from the old BIDS format to the new BIDS format.
*
* @param {{headers: string[], rows: string[][]}} oldParsedTsv Parsed TSV data using the old format
* @returns {Map<string, string[]>} The parsed contents of the TSV file, using the new format.
*/
export function convertParsedTSVData(oldParsedTsv) {
const columns = new Map()

oldParsedTsv.headers.forEach((x) => {
columns.set(x, [])
})
for (let i = 1; i < oldParsedTsv.rows.length; i++) {
for (let j = 0; j < oldParsedTsv.headers.length; j++) {
columns.get(oldParsedTsv.headers[j])?.push(oldParsedTsv.rows[i][j])
}
}
for (const [key, value] of columns) {
columns.set(key, value)
}
return columns
}

export default parseTSV
Loading

0 comments on commit 0e08352

Please sign in to comment.