@@ -59,10 +59,64 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5959 }
6060 }
6161
62+ function addInferredClass ( th , columnLength , currentCount , classToAdd ) {
63+ const threshold = columnLength / 2 ;
64+ if ( currentCount >= threshold ) {
65+ th . classList . add ( classToAdd ) ;
66+ }
67+ }
68+
69+ function inferSortClasses ( tableRows , tableHeadHeaders ) {
70+ for ( let [ columnIndex , th ] of tableHeadHeaders . entries ( ) ) {
71+ const regexMinutesAndSeconds = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
72+ const regexFileSizeSort = / ^ ( [ . 0 - 9 ] + ) \s ? ( B | K B | K i B | M B | M i B | G B | G i B | T B | T i B ) / i;
73+ let runtimeSortCounter = 0 ,
74+ fileSizeSortCounter = 0 ;
75+
76+ let tableColumnLength = th . parentElement . childElementCount ;
77+ for ( let tr of tableRows ) {
78+ let runtimeSortMatch , fileSizeSortMatch ;
79+ const tableColumn = tr . querySelectorAll ( "td" ) . item ( columnIndex ) ;
80+ if ( tableColumn . innerText ) {
81+ runtimeSortMatch = tableColumn . innerText . match (
82+ regexMinutesAndSeconds
83+ ) ;
84+ fileSizeSortMatch = tableColumn . innerText . match ( regexFileSizeSort ) ;
85+ }
86+ if ( runtimeSortMatch ) {
87+ runtimeSortCounter ++ ;
88+ }
89+ if ( fileSizeSortMatch ) {
90+ fileSizeSortCounter ++ ;
91+ }
92+ }
93+ // TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters
94+ addInferredClass (
95+ th ,
96+ tableColumnLength ,
97+ runtimeSortCounter ,
98+ "runtime-sort"
99+ ) ;
100+ addInferredClass (
101+ th ,
102+ tableColumnLength ,
103+ fileSizeSortCounter ,
104+ "file-size-sort"
105+ ) ;
106+ }
107+ }
108+
62109 function makeTableSortable ( sortableTable ) {
63110 const tableBody = getTableBody ( sortableTable ) ;
64111 const tableHead = sortableTable . querySelector ( "thead" ) ;
65112 const tableHeadHeaders = tableHead . querySelectorAll ( "th" ) ;
113+ const tableRows = tableBody . querySelectorAll ( "tr" ) ;
114+
115+ const isNoSortClassInference =
116+ sortableTable . classList . contains ( "no-class-infer" ) ;
117+ if ( ! isNoSortClassInference ) {
118+ inferSortClasses ( tableRows , tableHeadHeaders ) ;
119+ }
66120
67121 for ( let [ columnIndex , th ] of tableHeadHeaders . entries ( ) ) {
68122 if ( ! th . classList . contains ( "disable-sort" ) ) {
@@ -124,24 +178,26 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
124178
125179 function sortByRuntime ( tableRows , columnData ) {
126180 for ( let [ i , tr ] of tableRows . entries ( ) ) {
127- const regexMinutesAndSeconds = / ^ ( \d + m ) \s ? ( \d + s ) $ / i;
181+ const regexMinutesAndSeconds = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
128182 let columnOfTd = tr
129183 . querySelectorAll ( "td" )
130184 . item ( columnIndex ) . textContent ;
131185 let match = columnOfTd . match ( regexMinutesAndSeconds ) ;
132- let minutesInSeconds ,
133- seconds ,
134- timeinSeconds = [ 0 , 0 , 0 ] ;
186+ let [ minutesInSeconds , hours , seconds , timeinSeconds ] = [ 0 , 0 , 0 , 0 ] ;
135187 if ( match ) {
136- const regexMinutes = match [ 1 ] ;
188+ const regexHours = match [ 1 ] ;
189+ if ( regexHours ) {
190+ hours = Number ( regexHours . replace ( "h" , "" ) ) * 60 * 60 ;
191+ }
192+ const regexMinutes = match [ 2 ] ;
137193 if ( regexMinutes ) {
138194 minutesInSeconds = Number ( regexMinutes . replace ( "m" , "" ) ) * 60 ;
139195 }
140- const regexSeconds = match [ 2 ] ;
196+ const regexSeconds = match [ 3 ] ;
141197 if ( regexSeconds ) {
142198 seconds = Number ( regexSeconds . replace ( "s" , "" ) ) ;
143199 }
144- timeinSeconds = minutesInSeconds + seconds ;
200+ timeinSeconds = hours + minutesInSeconds + seconds ;
145201 }
146202 columnData . push ( `${ timeinSeconds } #${ i } ` ) ;
147203 columnIndexAndTableRow [ columnData [ i ] ] = tr . innerHTML ;
0 commit comments