@@ -55,57 +55,61 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5555 }
5656
5757 function inferSortClasses ( tableRows , columnIndex , column , th ) {
58- const runtimeRegex = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
59- const fileSizeRegex = / ^ ( [ . 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;
60- // Don't infer dates with delimiter "."; as could capture semantic version numbers.
61- const dmyRegex = / ^ ( \d \d ? ) [ / - ] ( \d \d ? ) [ / - ] ( ( \d \d ) ? \d \d ) / ;
62- const ymdRegex = / ^ ( \d \d \d \d ) [ / - ] ( \d \d ? ) [ / - ] ( \d \d ? ) / ;
63- // const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
64- const numericRegex =
65- / ^ - ? (?: \d { 1 , 3 } (?: [ ' , ] \d { 3 } ) * (?: \. \d + ) ? | \d + (?: \. \d + ) ? (?: [ ' , ] \d { 3 } ) * ?) $ / ;
66- const inferableClasses = {
67- runtime : { regexp : runtimeRegex , class : "runtime-sort" , count : 0 } ,
68- filesize : { regexp : fileSizeRegex , class : "file-size-sort" , count : 0 } ,
69- dmyDates : { regexp : dmyRegex , class : "dates-dmy-sort" , count : 0 } ,
70- ymdDates : { regexp : ymdRegex , class : "dates-ymd-sort" , count : 0 } ,
71- numericRegex : { regexp : numericRegex , class : "numeric-sort" , count : 0 } ,
72- } ;
73- let classNameAdded = false ;
74- let regexNotFoundCount = 0 ;
75- const threshold = Math . ceil ( tableRows . length / 2 ) ;
76- for ( let tr of tableRows ) {
77- if ( regexNotFoundCount >= threshold ) {
78- break ;
79- }
80- const tableColumn = tr
81- . querySelectorAll ( "td" )
82- . item (
83- column . span [ columnIndex ] === 1
84- ? column . spanSum [ columnIndex ] - 1
85- : column . spanSum [ columnIndex ] - column . span [ columnIndex ]
86- ) ;
87- let foundMatch = false ;
88- for ( let key of Object . keys ( inferableClasses ) ) {
89- let classRegexp = inferableClasses [ key ] . regexp ;
90- if ( tableColumn ?. innerText !== undefined ) {
91- if ( tableColumn . innerText . match ( classRegexp ) ) {
92- foundMatch = true ;
93- inferableClasses [ key ] . count ++ ;
58+ try {
59+ const runtimeRegex = / ^ ( \d + h ) ? \s ? ( \d + m ) ? \s ? ( \d + s ) ? $ / i;
60+ const fileSizeRegex = / ^ ( [ . 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;
61+ // Don't infer dates with delimiter "."; as could capture semantic version numbers.
62+ const dmyRegex = / ^ ( \d \d ? ) [ / - ] ( \d \d ? ) [ / - ] ( ( \d \d ) ? \d \d ) / ;
63+ const ymdRegex = / ^ ( \d \d \d \d ) [ / - ] ( \d \d ? ) [ / - ] ( \d \d ? ) / ;
64+ // const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
65+ const numericRegex =
66+ / ^ - ? (?: \d { 1 , 3 } (?: [ ' , ] \d { 3 } ) * (?: \. \d + ) ? | \d + (?: \. \d + ) ? (?: [ ' , ] \d { 3 } ) * ?) $ / ;
67+ const inferableClasses = {
68+ runtime : { regexp : runtimeRegex , class : "runtime-sort" , count : 0 } ,
69+ filesize : { regexp : fileSizeRegex , class : "file-size-sort" , count : 0 } ,
70+ dmyDates : { regexp : dmyRegex , class : "dates-dmy-sort" , count : 0 } ,
71+ ymdDates : { regexp : ymdRegex , class : "dates-ymd-sort" , count : 0 } ,
72+ numericRegex : { regexp : numericRegex , class : "numeric-sort" , count : 0 } ,
73+ } ;
74+ let classNameAdded = false ;
75+ let regexNotFoundCount = 0 ;
76+ const threshold = Math . ceil ( tableRows . length / 2 ) ;
77+ for ( let tr of tableRows ) {
78+ if ( regexNotFoundCount >= threshold ) {
79+ break ;
80+ }
81+ const tableColumn = tr
82+ . querySelectorAll ( "td" )
83+ . item (
84+ column . span [ columnIndex ] === 1
85+ ? column . spanSum [ columnIndex ] - 1
86+ : column . spanSum [ columnIndex ] - column . span [ columnIndex ]
87+ ) ;
88+ let foundMatch = false ;
89+ for ( let key of Object . keys ( inferableClasses ) ) {
90+ let classRegexp = inferableClasses [ key ] . regexp ;
91+ if ( tableColumn . innerText !== undefined ) {
92+ if ( tableColumn . innerText . match ( classRegexp ) ) {
93+ foundMatch = true ;
94+ inferableClasses [ key ] . count ++ ;
95+ }
96+ }
97+ if ( inferableClasses [ key ] . count >= threshold ) {
98+ th . classList . add ( inferableClasses [ key ] . class ) ;
99+ classNameAdded = true ;
100+ break ;
94101 }
95102 }
96- if ( inferableClasses [ key ] . count >= threshold ) {
97- th . classList . add ( inferableClasses [ key ] . class ) ;
98- classNameAdded = true ;
103+ if ( classNameAdded ) {
99104 break ;
100105 }
106+ if ( ! foundMatch ) {
107+ regexNotFoundCount ++ ;
108+ continue ;
109+ }
101110 }
102- if ( classNameAdded ) {
103- break ;
104- }
105- if ( ! foundMatch ) {
106- regexNotFoundCount ++ ;
107- continue ;
108- }
111+ } catch ( e ) {
112+ console . log ( e ) ;
109113 }
110114 }
111115
0 commit comments