From 4c8e201755627ccbe0411323f9abadc455792f04 Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Sun, 19 Feb 2012 19:56:56 +0000 Subject: [PATCH] Add support for sorting fractions in jquery.tablesorter Fixes bug 15404 --- RELEASE-NOTES-1.20 | 1 + resources/jquery/jquery.tablesorter.js | 21 +++++++++- .../jquery/jquery.tablesorter.test.js | 41 +++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES-1.20 b/RELEASE-NOTES-1.20 index 1889c55599..65317f312c 100644 --- a/RELEASE-NOTES-1.20 +++ b/RELEASE-NOTES-1.20 @@ -21,6 +21,7 @@ production. contentSub, ... The same div often also contains the class mw-content-ltr/rtl. * (bug 34475) Add support for IP/CIDR notation to tablesorter * (bug 27619) Remove preference option to display broken links as link? +* (bug 15404) Add support for sorting fractions in jquery.tablesorter === Bug fixes in 1.20 === * (bug 30245) Use the correct way to construct a log page title. diff --git a/resources/jquery/jquery.tablesorter.js b/resources/jquery/jquery.tablesorter.js index ca9971b2b9..6ffddf2ca9 100644 --- a/resources/jquery/jquery.tablesorter.js +++ b/resources/jquery/jquery.tablesorter.js @@ -402,12 +402,13 @@ digits.push( $.escapeRE( localised[i] ) ); } } - var digitClass = '[' + digits.join( '', digits ) + ']'; + ts.digitClass = '[' + digits.join( '', digits ) + ']'; // We allow a trailing percent sign, which we just strip. This works fine // if percents and regular numbers aren't being mixed. ts.numberRegex = new RegExp("^(" + "[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?" + // Fortran-style scientific - "|" + "[-+\u2212]?" + digitClass + "+[\\s\\xa0]*%?" + // Generic localised + "|" + "[-+\u2212]?" + ts.digitClass + "+[\\s\\xa0]*%?" + // Generic localised + "|([-+\u2212]?" + ts.digitClass + "+[\\s\\xa0]+)*" + ts.digitClass + "+[\\s\\xa0]*[\\/][\\s\\xa0]*" + ts.digitClass + "+" + // Fractions ")$", "i"); } @@ -502,6 +503,9 @@ ], time: [ new RegExp( /^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/) + ], + fractions: [ + new RegExp( "^(?:([-+\u2212]?" + ts.digitClass + "+)[\\s\\xa0]+)*(" + ts.digitClass + "+)[\\s\\xa0]*[\\/][\\s\\xa0]*(" + ts.digitClass + "+)" ) ] }; } @@ -909,6 +913,19 @@ return $.tablesorter.numberRegex.test( $.trim( s )); }, format: function( s ) { + var values = ts.rgx.fractions[0].exec($.trim(s)); + if( values != null ) { + // A fraction + var retVal = 0; + var decimal = $.tablesorter.formatDigit(values[2]) / $.tablesorter.formatDigit(values[3]); + if( values[1] != undefined ) { + retVal = $.tablesorter.formatDigit(values[1]); + } + if( !isNaN(decimal) && isFinite(decimal) ) { + retVal += decimal; + } + return retVal; + } return $.tablesorter.formatDigit(s); }, type: 'numeric' diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js index ec34102749..a76af5e984 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js @@ -575,6 +575,47 @@ tableTest( 'bug 8115: sort numbers with commas (descending)', ); // TODO add numbers sorting tests for bug 8115 with a different language +var fractions = [ + [ '56' ], + [ '1 3/8' ], + [ '4 7/8' ], + [ '2,000 1/6' ], + [ '4 1/8' ], + [ '-4 1/8' ], + [ '−5 1/8' ], + [ '56 45/500' ], + [ '56 100/500' ], + [ '100 / 500' ] +]; +var fractionsAsc = [ + [ '−5 1/8' ], + [ '-4 1/8' ], + [ '100 / 500' ], + [ '1 3/8' ], + [ '4 1/8' ], + [ '4 7/8' ], + [ '56' ], + [ '56 45/500' ], + [ '56 100/500' ], + [ '2,000 1/6' ] +]; + +tableTest( 'sort fractional numbers in all sorts and forms (ascending)', + ['Fractional numbers'], fractions, fractionsAsc, + function( $table ) { + $table.tablesorter(); + $table.find( '.headerSort:eq(0)' ).click(); + } +); + +tableTest( 'sort fractional numbers in all sorts and forms (descending)', + ['Fractional numbers'], fractions, reversed(fractionsAsc), + function( $table ) { + $table.tablesorter(); + $table.find( '.headerSort:eq(0)' ).click().click(); + } +); + test( 'bug 32888 - Tables inside a tableheader cell', function() { expect(2); -- 2.20.1