From 3cd9882fc754419ccf870a9bf5f6e6f478c24a3b Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Mon, 13 Mar 2017 23:30:50 -0500 Subject: [PATCH] Parser: Add leading zero parser --- docs/example-parsers-leading-zeros.html | 121 ++++++++++++++++++++++++ docs/index.html | 1 + js/parsers/parser-leading-zeros.js | 33 +++++++ 3 files changed, 155 insertions(+) create mode 100644 docs/example-parsers-leading-zeros.html create mode 100644 js/parsers/parser-leading-zeros.js diff --git a/docs/example-parsers-leading-zeros.html b/docs/example-parsers-leading-zeros.html new file mode 100644 index 00000000..acfafeb5 --- /dev/null +++ b/docs/example-parsers-leading-zeros.html @@ -0,0 +1,121 @@ + + + + + jQuery tablesorter - Leading Zeros Parser + + + + + + + + + + + + + + + + + + +
+ +

+ NOTE! +

+ + +
  • Click on the "Toggle parsed values" button to see the values as saved to the cache.
  • +
  • The small number the leading zeros parser subtracts from the cell value has a precision of 10 digits (1e-10). If you are using higher precision values, then copy this parser and increase the precision as needed.
  • + + +

    Demo

    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Number sortNatural text sortPlain text sortLeading Zeros parser
    001001001001
    1111
    2222
    10101010
    01010101
    1.011.011.011.01
    000001000001000001000001
    00001000010000100001
    020020020020
    0001000100010001
    3333
    10101010
    12121212
    15151515
    02020202
    5555
    25252525
    21212121
    + +

    Page Header

    +
    +
    <link href="css/theme.blue.css" rel="stylesheet">
    +<script src="js/jquery.min.js"></script>
    +<script src="js/jquery.tablesorter.js"></script>
    +<script src="js/parser-leading-zeros.js"></script>
    +
    + +

    Javascript

    +
    +
    
    +	
    + +

    HTML

    +
    +
    
    +	
    + +
    + + + + + diff --git a/docs/index.html b/docs/index.html index a638638c..99bd6156 100644 --- a/docs/index.html +++ b/docs/index.html @@ -541,6 +541,7 @@
  • Ignore leading articles parser (Ignore "A", "An" and "The" in titles) (v2.8).
  • Input/select parsers (used by Grouping rows widget) (v2.8; v2.25.9).
  • jQuery Globalize (number & date parsers; v2.22.0; v2.25.0).
  • +
  • Leading zeros parser (v2.28.6).
  • Metric parser (v2.8).
  • Named Numbers parser (v2.18.0; v2.22.0).
  • Network (IPv4, IPv6 and MAC address parser (v2.12; v2.22.0; NOTE "ipAddress" parser moved to this file in v2.18.0).
  • diff --git a/js/parsers/parser-leading-zeros.js b/js/parsers/parser-leading-zeros.js new file mode 100644 index 00000000..28386faa --- /dev/null +++ b/js/parsers/parser-leading-zeros.js @@ -0,0 +1,33 @@ +/*! Parser: leading zeros - updated 3/13/2017 (v2.28.6) */ +/* jshint jquery:true, unused:false */ +;( function( $ ) { + 'use strict'; + + var ts = $.tablesorter, + // modify this value to increase precision as needed + precision = 1e-10; + + ts.addParser({ + id: 'leadingZeros', + is: function() { + return false; + }, + format: function( s, table ) { + var val = ( s || '' ).replace( ts.regex.nondigit, '' ), + number = ts.formatFloat( val, table ), + str = number.toString(); + if ( + !isNaN( number ) && + number == val && // jshint ignore:line + val.length !== str.length + ) { + // subtract a decimal equivalent of the string length + // so "0001" sorts before "01" + number -= precision * ( s.length - str.length ); + } + return number; + }, + type: 'number' + }); + +})( jQuery );