@@ -9,53 +9,133 @@ const normalizeEOL = (str) => str.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
99const isContentfulRow = ( row ) => row && ! / ^ \s * $ / . test ( row )
1010
1111/**
12- * Parse a TSV file
12+ * Split TSV contents into trimmed cells, one array per contentful row; the first row is the header.
1313 *
1414 * @param {string } contents The contents of a TSV file.
15- * @returns {Map< string, string[]> } The parsed contents of the TSV file.
15+ * @returns {string[][] } The rows of the file.
1616 */
17- export function parseTSV ( contents ) {
18- const columns = new Map ( )
19- const rows = stripBOM ( normalizeEOL ( contents ) )
17+ const splitRows = ( contents ) =>
18+ stripBOM ( normalizeEOL ( contents ) )
2019 . split ( '\n' )
2120 . filter ( isContentfulRow )
2221 . map ( ( str ) => str . split ( '\t' ) . map ( ( cell ) => cell . trim ( ) ) )
23- const headers = rows . length ? rows [ 0 ] : [ ]
2422
25- headers . forEach ( ( x ) => {
26- columns . set ( x , [ ] )
23+ /**
24+ * Build the column map of a table, keeping only the first column of a repeated header name.
25+ *
26+ * A repeated name is an error (specification 3.2.10, TABULAR_INVALID) that {@link findDuplicateHeaders} detects;
27+ * keeping one column per name keeps every column aligned with the rows instead of interleaving two columns.
28+ *
29+ * @param {string[] } headers The column names.
30+ * @param {string[][] } rows The rows; rows[0] is skipped as the header row.
31+ * @returns {Map<string, string[]> } The cells of each column.
32+ */
33+ function buildColumns ( headers , rows ) {
34+ const firstIndex = new Map ( )
35+ headers . forEach ( ( header , index ) => {
36+ if ( ! firstIndex . has ( header ) ) {
37+ firstIndex . set ( header , index )
38+ }
2739 } )
28- for ( let i = 1 ; i < rows . length ; i ++ ) {
29- for ( let j = 0 ; j < headers . length ; j ++ ) {
30- columns . get ( headers [ j ] ) ?. push ( rows [ i ] [ j ] )
40+ const columns = new Map ( )
41+ for ( const [ header , index ] of firstIndex ) {
42+ columns . set (
43+ header ,
44+ rows . slice ( 1 ) . map ( ( row ) => row [ index ] ) ,
45+ )
46+ }
47+ return columns
48+ }
49+
50+ /**
51+ * Return the names that appear more than once in a list of column names.
52+ *
53+ * @param {string[] } headers The column names.
54+ * @returns {string[] } Each repeated name once, in order of its second appearance.
55+ */
56+ function repeatedNames ( headers ) {
57+ const seen = new Set ( )
58+ const repeated = new Set ( )
59+ for ( const header of headers ) {
60+ if ( seen . has ( header ) ) {
61+ repeated . add ( header )
3162 }
63+ seen . add ( header )
3264 }
33- for ( const [ key , value ] of columns ) {
34- columns . set ( key , value )
65+ return [ ...repeated ]
66+ }
67+
68+ /**
69+ * Parse a TSV file
70+ *
71+ * A column name repeated in the header keeps only its first column (see {@link findDuplicateHeaders}).
72+ *
73+ * @param {string } contents The contents of a TSV file.
74+ * @returns {Map<string, string[]> } The parsed contents of the TSV file.
75+ */
76+ export function parseTSV ( contents ) {
77+ const rows = splitRows ( contents )
78+ return buildColumns ( rows . length ? rows [ 0 ] : [ ] , rows )
79+ }
80+
81+ /**
82+ * Find the column names that appear more than once in the header of TSV contents.
83+ *
84+ * @param {string } contents The contents of a TSV file.
85+ * @returns {string[] } Each repeated name once; empty when every name is unique.
86+ */
87+ export function findDuplicateHeaders ( contents ) {
88+ return repeatedNames ( headerCells ( contents ) )
89+ }
90+
91+ /**
92+ * Return the trimmed cells of the first contentful line of TSV contents, without splitting the other lines.
93+ *
94+ * Line breaks are \r\n, \r or \n, as in {@link splitRows}, so the result equals the header row that
95+ * {@link parseTSV} uses.
96+ *
97+ * @param {string } contents The contents of a TSV file.
98+ * @returns {string[] } The header cells; empty when the contents have no contentful line.
99+ */
100+ function headerCells ( contents ) {
101+ const text = stripBOM ( contents )
102+ const lineBreak = / \r \n | \r | \n / g
103+ let start = 0
104+ while ( start <= text . length ) {
105+ lineBreak . lastIndex = start
106+ const match = lineBreak . exec ( text )
107+ const line = text . slice ( start , match ? match . index : text . length )
108+ if ( isContentfulRow ( line ) ) {
109+ return line . split ( '\t' ) . map ( ( cell ) => cell . trim ( ) )
110+ }
111+ if ( ! match ) {
112+ break
113+ }
114+ start = lineBreak . lastIndex
35115 }
36- return columns
116+ return [ ]
37117}
118+
119+ /**
120+ * Find the column names that appear more than once in parsed TSV data in the old BIDS format.
121+ *
122+ * @param {{headers: string[], rows: string[][]} } oldParsedTsv Parsed TSV data using the old format
123+ * @returns {string[] } Each repeated name once; empty when every name is unique.
124+ */
125+ export function findDuplicateParsedHeaders ( oldParsedTsv ) {
126+ return repeatedNames ( oldParsedTsv . headers )
127+ }
128+
38129/**
39130 * Convert parsed TSV file data from the old BIDS format to the new BIDS format.
40131 *
132+ * A column name repeated in the headers keeps only its first column (see {@link findDuplicateParsedHeaders}).
133+ *
41134 * @param {{headers: string[], rows: string[][]} } oldParsedTsv Parsed TSV data using the old format
42135 * @returns {Map<string, string[]> } The parsed contents of the TSV file, using the new format.
43136 */
44137export function convertParsedTSVData ( oldParsedTsv ) {
45- const columns = new Map ( )
46-
47- oldParsedTsv . headers . forEach ( ( x ) => {
48- columns . set ( x , [ ] )
49- } )
50- for ( let i = 1 ; i < oldParsedTsv . rows . length ; i ++ ) {
51- for ( let j = 0 ; j < oldParsedTsv . headers . length ; j ++ ) {
52- columns . get ( oldParsedTsv . headers [ j ] ) ?. push ( oldParsedTsv . rows [ i ] [ j ] )
53- }
54- }
55- for ( const [ key , value ] of columns ) {
56- columns . set ( key , value )
57- }
58- return columns
138+ return buildColumns ( oldParsedTsv . headers , oldParsedTsv . rows )
59139}
60140
61141export default parseTSV
0 commit comments