@@ -3,6 +3,7 @@ import { extraColsMapperFactory, getColumnsInfo } from './helpers';
33import { ElementHandle } from 'puppeteer' ;
44import { InvalidSettingsError } from './errors' ;
55import PipelineExecutor from './pipelineExecutor' ;
6+ import { groupBy } from './aggregations' ;
67export function parseTableFactory ( settings : FullParserSettings ) {
78 const extraColsMapper = extraColsMapperFactory ( settings . extraCols ) ;
89
@@ -16,15 +17,11 @@ export function parseTableFactory(settings: FullParserSettings) {
1617 return table . $$ ( settings . bodyRowsSelector ) ;
1718 } ;
1819
19- const getOutputHeaderRow = ( excludedKeyIndexes : number [ ] , missingColNames : string [ ] ) => {
20+ const getOutputHeaderRow = ( missingColNames : string [ ] ) => {
2021 const headerRowRaw = Object . values ( settings . allowedColNames ) ;
2122 const sortedHeader = extraColsMapper ( headerRowRaw , 'colName' ) ;
2223
23- const headerRow = sortedHeader
24- . filter ( ( _ , index ) => ! excludedKeyIndexes . includes ( index ) )
25- . filter ( ( key ) => ! missingColNames . includes ( key ) ) ;
26-
27- return settings . rowValuesAsArray ? headerRow : headerRow . join ( settings . csvSeparator ) ;
24+ return sortedHeader . filter ( ( key ) => ! missingColNames . includes ( key ) ) ;
2825 } ;
2926
3027 const getRowStructureValidator = ( allowedIndexes : Record < string , number > ) => {
@@ -82,24 +79,29 @@ export function parseTableFactory(settings: FullParserSettings) {
8279 bodyRows . reverse ( ) ;
8380 }
8481
85- const finalRows = new PipelineExecutor <
86- string [ ] [ ] ,
87- typeof settings . rowValuesAsArray extends true ? string [ ] [ ] : string [ ]
88- > ( )
82+ let parsedRows = new PipelineExecutor < string [ ] [ ] , string [ ] [ ] > ( )
8983 . addFilter ( getRowStructureValidator ( indexes . allowed ) )
9084 . addMap ( ( row ) => extraColsMapper ( row , 'data' ) )
9185 . addFilter ( ( row , index , rows ) => settings . rowValidator ( row , getColumnIndex , index , rows ) )
9286 . addMap ( ( row ) => row . map ( ( cell , index ) => settings . colParser ( cell , index , getColumnIndex ) ) )
9387 . addTransform ( ( row ) => settings . rowTransform ( row , getColumnIndex ) )
94- . addMap ( ( row ) => row . filter ( ( _ , index : number ) => ! indexes . excluded . includes ( index ) ) )
95- . addMap ( ( row ) => ( settings . rowValuesAsArray ? row : row . join ( settings . csvSeparator ) ) )
9688 . execute ( await Promise . all ( bodyRows . map ( getRowsData ( indexes . allowed ) ) ) ) ;
9789
90+ if ( settings . groupBy ) {
91+ parsedRows = groupBy ( parsedRows , settings . groupBy , getColumnIndex ) ;
92+ }
93+
9894 if ( addHeader ) {
99- const headerRow = getOutputHeaderRow ( indexes . excluded , missingColNames ) ;
100- finalRows . unshift ( headerRow as string ) ;
95+ const headerRow = getOutputHeaderRow ( missingColNames ) ;
96+ parsedRows . unshift ( headerRow ) ;
10197 }
10298
103- return finalRows ;
99+ return new PipelineExecutor <
100+ string [ ] [ ] ,
101+ typeof settings . rowValuesAsArray extends true ? string [ ] [ ] : string [ ]
102+ > ( )
103+ . addMap ( ( row ) => row . filter ( ( _ , index : number ) => ! indexes . excluded . includes ( index ) ) )
104+ . addMap ( ( row ) => ( settings . rowValuesAsArray ? row : row . join ( settings . csvSeparator ) ) )
105+ . execute ( parsedRows ) ;
104106 } ;
105107}
0 commit comments