numbers inside parenthesis are now treated as a negative

This commit is contained in:
Rob Garrison 2012-04-16 16:21:56 -05:00
parent 1f7df64e9b
commit 49c67f4c8e
6 changed files with 55 additions and 17 deletions

View File

@ -35,6 +35,14 @@ tablesorter can successfully parse and sort many types of data including linked
View the [complete listing here](https://github.com/Mottie/tablesorter/wiki/Change). View the [complete listing here](https://github.com/Mottie/tablesorter/wiki/Change).
#### Version 2.1.12 (4/16/2012)
* Modified digit parser to assume numbers wrapped in parenthesis are negative numbers.
* Updated the [Dealing with Digits](example-option-digits.html) demo.
* Enhancement from [issue #47](https://github.com/Mottie/tablesorter/issues/47), thanks to [timkingman](https://github.com/timkingman) for sharing the code!
* Updated "digit" parser to remove extraneous characters before parsing. This change makes the "digit" parser essentially work the same as the "currency" parser.
* Updated some regex to increase parsing speed. See [this jsperf](http://jsperf.com/replace-string-vs-regex/6).
#### Version 2.1.11 (4/12/2012) #### Version 2.1.11 (4/12/2012)
* Added `emptyToBottom` option which tells tablesorter how you want it to sort empty table cells. Enhancement from [issue #](). * Added `emptyToBottom` option which tells tablesorter how you want it to sort empty table cells. Enhancement from [issue #]().

View File

@ -72,7 +72,7 @@
<td>Clark</td> <td>Clark</td>
<td>Kent</td> <td>Kent</td>
<td>18</td> <td>18</td>
<td>15.89</td> <td>2.89</td>
<td>44.2%</td> <td>44.2%</td>
<td>-15</td> <td>-15</td>
</tr> </tr>
@ -90,10 +90,37 @@
<td>Bruce</td> <td>Bruce</td>
<td>Evans</td> <td>Evans</td>
<td>56</td> <td>56</td>
<td>153.19</td> <td>$153.19</td>
<td>23%</td> <td>23%</td>
<td>+9</td> <td>+9</td>
</tr> </tr>
<tr>
<td>A256</td>
<td>John</td>
<td>Clark</td>
<td>44</td>
<td>($19.89)</td>
<td>25.1%</td>
<td>(5)</td>
</tr>
<tr>
<td>A23</td>
<td>Elvis</td>
<td>Presley</td>
<td>24</td>
<td>($9.99)</td>
<td>50%</td>
<td>(22)</td>
</tr>
<tr>
<td>A10</td>
<td>Frank</td>
<td>Carter</td>
<td>40</td>
<td>-12.99</td>
<td>20%</td>
<td>(6)</td>
</tr>
</tbody> </tbody>
</table></div> </table></div>

View File

@ -673,7 +673,7 @@
<tr><th><code class="hilight">sorter: false</code></th><td>disable sort for this column.</td></tr> <tr><th><code class="hilight">sorter: false</code></th><td>disable sort for this column.</td></tr>
<tr><th><code class="hilight">sorter: "text"</code></th><td>Sort alpha-numerically.</td></tr> <tr><th><code class="hilight">sorter: "text"</code></th><td>Sort alpha-numerically.</td></tr>
<tr><th><code class="hilight">sorter: "digit"</code></th><td>Sort numerically.</td></tr> <tr><th><code class="hilight">sorter: "digit"</code></th><td>Sort numerically.</td></tr>
<tr><th><code class="hilight">sorter: "currency"</code></th><td>Sort by currency value (supports "&#163;$&#8364;").</td></tr> <tr><th><code class="hilight">sorter: "currency"</code></th><td>Sort by currency value (supports "&#163;$&#8364;&#x00a4;&#x00a5;&#x00a2;").</td></tr>
<tr><th><code class="hilight">sorter: "ipAddress"</code></th><td>Sort by IP Address.</td></tr> <tr><th><code class="hilight">sorter: "ipAddress"</code></th><td>Sort by IP Address.</td></tr>
<tr><th><code class="hilight">sorter: "url"</code></th><td>Sort by url.</td></tr> <tr><th><code class="hilight">sorter: "url"</code></th><td>Sort by url.</td></tr>
<tr><th><code class="hilight">sorter: "isoDate"</code></th><td>Sort by ISO date (YYYY-MM-DD or YYYY/MM/DD).</td></tr> <tr><th><code class="hilight">sorter: "isoDate"</code></th><td>Sort by ISO date (YYYY-MM-DD or YYYY/MM/DD).</td></tr>

View File

@ -1,5 +1,5 @@
/*! /*!
* TableSorter 2.1.11 - Client-side table sorting with ease! * TableSorter 2.1.12 - Client-side table sorting with ease!
* @requires jQuery v1.2.6+ * @requires jQuery v1.2.6+
* *
* Copyright (c) 2007 Christian Bach * Copyright (c) 2007 Christian Bach
@ -18,7 +18,7 @@
$.extend({ $.extend({
tablesorter: new function(){ tablesorter: new function(){
this.version = "2.1.11"; this.version = "2.1.12";
var parsers = [], widgets = [], tbl; var parsers = [], widgets = [], tbl;
this.defaults = { this.defaults = {
@ -768,13 +768,16 @@
// French Format = 1 234 567,89 -> 1234567.89 // French Format = 1 234 567,89 -> 1234567.89
s = s.replace(/[\s|\.]/g,'').replace(/,/g,'.'); s = s.replace(/[\s|\.]/g,'').replace(/,/g,'.');
} }
if(/^\s*\([.\d]+\)/.test(s)) {
s = s.replace(/^\s*\(/,'-').replace(/\)/,'');
}
var i = parseFloat(s); var i = parseFloat(s);
// return the text instead of zero // return the text instead of zero
return isNaN(i) ? $.trim(s) : i; return isNaN(i) ? $.trim(s) : i;
}; };
this.isDigit = function(s) { this.isDigit = function(s) {
// replace all unwanted chars and match. // replace all unwanted chars and match.
return (/^[\-+]?\d*$/).test($.trim(s.replace(/[,.'\s]/g, ''))); return (/^[\-+(]?\d*[)]?$/).test($.trim(s.replace(/[,.'\s]/g, '')));
}; };
this.clearTableBody = function (table) { this.clearTableBody = function (table) {
$(table.tBodies[0]).empty(); $(table.tBodies[0]).empty();
@ -808,7 +811,7 @@
return $.tablesorter.isDigit(s); return $.tablesorter.isDigit(s);
}, },
format: function(s){ format: function(s){
return $.tablesorter.formatFloat(s); return $.tablesorter.formatFloat(s.replace(/[^0-9,. \-()]/g, ""));
}, },
type: "numeric" type: "numeric"
}); });
@ -816,10 +819,10 @@
ts.addParser({ ts.addParser({
id: "currency", id: "currency",
is: function(s){ is: function(s){
return (/^[\u00a3$\u20ac\u00a4\u00a5\u00a2?.]/).test(s); // £$€¤¥¢?. return (/^\(?[\u00a3$\u20ac\u00a4\u00a5\u00a2?.]/).test(s); // £$€¤¥¢?.
}, },
format: function(s){ format: function(s){
return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9,. \-]/g), "")); return $.tablesorter.formatFloat(s.replace(/[^0-9,. \-()]/g, ""));
}, },
type: "numeric" type: "numeric"
}); });
@ -852,7 +855,7 @@
return (/^(https?|ftp|file):\/\/$/).test(s); return (/^(https?|ftp|file):\/\/$/).test(s);
}, },
format: function(s) { format: function(s) {
return $.trim(s.replace(new RegExp(/(https?|ftp|file):\/\//), '')); return $.trim(s.replace(/(https?|ftp|file):\/\//, ''));
}, },
type: "text" type: "text"
}); });
@ -863,7 +866,7 @@
return (/^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/).test(s); return (/^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/).test(s);
}, },
format: function(s) { format: function(s) {
return $.tablesorter.formatFloat((s !== "") ? new Date(s.replace(new RegExp(/-/g), "/")).getTime() : ""); return $.tablesorter.formatFloat((s !== "") ? new Date(s.replace(/-/g, "/")).getTime() : "");
}, },
type: "numeric" type: "numeric"
}); });
@ -871,10 +874,10 @@
ts.addParser({ ts.addParser({
id: "percent", id: "percent",
is: function(s) { is: function(s) {
return (/\%$/).test($.trim(s)); return (/\%\)?$/).test($.trim(s));
}, },
format: function(s) { format: function(s) {
return $.tablesorter.formatFloat(s.replace(new RegExp(/%/g), "")); return $.tablesorter.formatFloat(s.replace(/%/g, ""));
}, },
type: "numeric" type: "numeric"
}); });
@ -882,7 +885,7 @@
ts.addParser({ ts.addParser({
id: "usLongDate", id: "usLongDate",
is: function(s) { is: function(s) {
return s.match(new RegExp(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/)); return s.match(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/);
}, },
format: function(s) { format: function(s) {
return $.tablesorter.formatFloat(new Date(s).getTime()); return $.tablesorter.formatFloat(new Date(s).getTime());

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "tablesorter", "name": "tablesorter",
"version": "2.1.11", "version": "2.1.12",
"title": "tablesorter", "title": "tablesorter",
"author": { "author": {
"name": "Christian Bach", "name": "Christian Bach",