r111884 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111883‎ | r111884 | r111885 >
Date:19:56, 19 February 2012
Author:hartman
Status:reverted (Comments)
Tags:
Comment:
Add support for sorting fractions in jquery.tablesorter

Fixes bug 15404
Modified paths:
  • /trunk/phase3/RELEASE-NOTES-1.20 (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.tablesorter.js (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js
@@ -575,6 +575,47 @@
576576 );
577577 // TODO add numbers sorting tests for bug 8115 with a different language
578578
 579+var fractions = [
 580+ [ '56' ],
 581+ [ '1 3/8' ],
 582+ [ '4 7/8' ],
 583+ [ '2,000 1/6' ],
 584+ [ '4 1/8' ],
 585+ [ '-4 1/8' ],
 586+ [ '−5 1/8' ],
 587+ [ '56 45/500' ],
 588+ [ '56 100/500' ],
 589+ [ '100 / 500' ]
 590+];
 591+var fractionsAsc = [
 592+ [ '−5 1/8' ],
 593+ [ '-4 1/8' ],
 594+ [ '100 / 500' ],
 595+ [ '1 3/8' ],
 596+ [ '4 1/8' ],
 597+ [ '4 7/8' ],
 598+ [ '56' ],
 599+ [ '56 45/500' ],
 600+ [ '56 100/500' ],
 601+ [ '2,000 1/6' ]
 602+];
 603+
 604+tableTest( 'sort fractional numbers in all sorts and forms (ascending)',
 605+ ['Fractional numbers'], fractions, fractionsAsc,
 606+ function( $table ) {
 607+ $table.tablesorter();
 608+ $table.find( '.headerSort:eq(0)' ).click();
 609+ }
 610+);
 611+
 612+tableTest( 'sort fractional numbers in all sorts and forms (descending)',
 613+ ['Fractional numbers'], fractions, reversed(fractionsAsc),
 614+ function( $table ) {
 615+ $table.tablesorter();
 616+ $table.find( '.headerSort:eq(0)' ).click().click();
 617+ }
 618+);
 619+
579620 test( 'bug 32888 - Tables inside a tableheader cell', function() {
580621 expect(2);
581622
Index: trunk/phase3/RELEASE-NOTES-1.20
@@ -21,6 +21,7 @@
2222 contentSub, ... The same div often also contains the class mw-content-ltr/rtl.
2323 * (bug 34475) Add support for IP/CIDR notation to tablesorter
2424 * (bug 27619) Remove preference option to display broken links as link?
 25+* (bug 15404) Add support for sorting fractions in jquery.tablesorter
2526
2627 === Bug fixes in 1.20 ===
2728 * (bug 30245) Use the correct way to construct a log page title.
Index: trunk/phase3/resources/jquery/jquery.tablesorter.js
@@ -402,12 +402,13 @@
403403 digits.push( $.escapeRE( localised[i] ) );
404404 }
405405 }
406 - var digitClass = '[' + digits.join( '', digits ) + ']';
 406+ ts.digitClass = '[' + digits.join( '', digits ) + ']';
407407
408408 // We allow a trailing percent sign, which we just strip. This works fine
409409 // if percents and regular numbers aren't being mixed.
410410 ts.numberRegex = new RegExp("^(" + "[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?" + // Fortran-style scientific
411 - "|" + "[-+\u2212]?" + digitClass + "+[\\s\\xa0]*%?" + // Generic localised
 411+ "|" + "[-+\u2212]?" + ts.digitClass + "+[\\s\\xa0]*%?" + // Generic localised
 412+ "|([-+\u2212]?" + ts.digitClass + "+[\\s\\xa0]+)*" + ts.digitClass + "+[\\s\\xa0]*[\\/][\\s\\xa0]*" + ts.digitClass + "+" + // Fractions
412413 ")$", "i");
413414 }
414415
@@ -502,6 +503,9 @@
503504 ],
504505 time: [
505506 new RegExp( /^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/)
 507+ ],
 508+ fractions: [
 509+ new RegExp( "^(?:([-+\u2212]?" + ts.digitClass + "+)[\\s\\xa0]+)*(" + ts.digitClass + "+)[\\s\\xa0]*[\\/][\\s\\xa0]*(" + ts.digitClass + "+)" )
506510 ]
507511 };
508512 }
@@ -909,6 +913,19 @@
910914 return $.tablesorter.numberRegex.test( $.trim( s ));
911915 },
912916 format: function( s ) {
 917+ var values = ts.rgx.fractions[0].exec($.trim(s));
 918+ if( values != null ) {
 919+ // A fraction
 920+ var retVal = 0;
 921+ var decimal = $.tablesorter.formatDigit(values[2]) / $.tablesorter.formatDigit(values[3]);
 922+ if( values[1] != undefined ) {
 923+ retVal = $.tablesorter.formatDigit(values[1]);
 924+ }
 925+ if( !isNaN(decimal) && isFinite(decimal) ) {
 926+ retVal += decimal;
 927+ }
 928+ return retVal;
 929+ }
913930 return $.tablesorter.formatDigit(s);
914931 },
915932 type: 'numeric'

Follow-up revisions

RevisionCommit summaryAuthorDate
r113122rv table sorting of IP and fraction...hashar10:17, 6 March 2012

Comments

#Comment by Nikerabbit (talk | contribs)   07:27, 20 February 2012

Only English, right?

#Comment by TheDJ (talk | contribs)   07:42, 20 February 2012

Well in theory it supports at least the variants with different thousands separators). In more theory, it supports other types of fractions (different digits then 0-9), as long as they are of the format: "a b/c". It is why I changed ts.digitClass to be accessible from other places in the TS code.

If you really have a different format, then we need more info on that, and then it might be better to add specific parsers just for those languages.

#Comment by Kaldari (talk | contribs)   21:31, 2 March 2012

Please add some comments to your format function so that people can tell what is going on without having to parse it out. For example "// Converting fractional value to decimal value". Also it looks like there is no protection against divide by zero errors.

Status & tagging log