-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from hed-standard/bids-tabular-file
Implement validation for non-event TSV files
- Loading branch information
Showing
7 changed files
with
228 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Module for parsing TSV files. | ||
* | ||
* Copied from https://github.com/bids-standard/bids-validator/blob/6fc6d152b52266934575442e61f1477ba18f42ec/bids-validator/validators/tsv/tsvParser.js | ||
*/ | ||
|
||
const stripBOM = (str) => str.replace(/^\uFEFF/, '') | ||
const normalizeEOL = (str) => str.replace(/\r\n/g, '\n').replace(/\r/g, '\n') | ||
const isContentfulRow = (row) => row && !/^\s*$/.test(row) | ||
|
||
/** | ||
* Parse a TSV file. | ||
* | ||
* @param {string} contents The contents of a TSV file. | ||
* @return {{headers: string[], rows: string[][]}} The parsed contents of the TSV file. | ||
*/ | ||
function parseTSV(contents) { | ||
const content = { | ||
headers: [], | ||
rows: [], | ||
} | ||
contents = stripBOM(contents) | ||
content.rows = normalizeEOL(contents) | ||
.split('\n') | ||
.filter(isContentfulRow) | ||
.map((str) => str.split('\t')) | ||
content.headers = content.rows.length ? content.rows[0] : [] | ||
return content | ||
} | ||
|
||
export default parseTSV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.