general parser formatting & cleaning

This commit is contained in:
Mottie 2013-09-29 14:37:03 -05:00
parent 214bce4e95
commit c1d102c698
7 changed files with 50 additions and 43 deletions

View File

@ -18,17 +18,17 @@
var result = s.match(iso8601date); var result = s.match(iso8601date);
if (result) { if (result) {
var date = new Date(result[1], 0, 1); var date = new Date(result[1], 0, 1);
if (result[3]) {date.setMonth(result[3] - 1);} if (result[3]) { date.setMonth(result[3] - 1); }
if (result[5]) {date.setDate(result[5]);} if (result[5]) { date.setDate(result[5]); }
if (result[7]) {date.setHours(result[7]);} if (result[7]) { date.setHours(result[7]); }
if (result[8]) {date.setMinutes(result[8]);} if (result[8]) { date.setMinutes(result[8]); }
if (result[10]) {date.setSeconds(result[10]);} if (result[10]) { date.setSeconds(result[10]); }
if (result[12]) {date.setMilliseconds(Number('0.' + result[12]) * 1000);} if (result[12]) { date.setMilliseconds(Number('0.' + result[12]) * 1000); }
return date; return date;
} }
return 0; return 0;
}, },
type : 'numeric' type : 'numeric'
}); });
})(jQuery); })(jQuery);

View File

@ -5,13 +5,14 @@
;(function($){ ;(function($){
"use strict"; "use strict";
$.tablesorter.dates = $.extend({}, $.tablesorter.dates, { var ts = $.tablesorter;
ts.dates = $.extend({}, ts.dates, {
// *** modify this array to change match the language *** // *** modify this array to change match the language ***
monthCased : [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] monthCased : [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
}); });
$.tablesorter.dates.monthLower = $.tablesorter.dates.monthCased.join(',').toLocaleLowerCase().split(','); ts.dates.monthLower = ts.dates.monthCased.join(',').toLocaleLowerCase().split(',');
$.tablesorter.addParser({ ts.addParser({
id: "month", id: "month",
is: function(){ is: function(){
return false; return false;
@ -19,7 +20,7 @@
format: function(s, table) { format: function(s, table) {
var j = -1, c = table.config; var j = -1, c = table.config;
s = c.ignoreCase ? s.toLocaleLowerCase() : s; s = c.ignoreCase ? s.toLocaleLowerCase() : s;
$.each($.tablesorter.dates[ 'month' + (c.ignoreCase ? 'Lower' : 'Cased') ], function(i,v){ $.each(ts.dates[ 'month' + (c.ignoreCase ? 'Lower' : 'Cased') ], function(i,v){
if (j < 0 && s.match(v)) { j = i; } if (j < 0 && s.match(v)) { j = i; }
}); });
// return s (original string) if there isn't a match // return s (original string) if there isn't a match

View File

@ -5,22 +5,27 @@
;(function($){ ;(function($){
"use strict"; "use strict";
var ts = $.tablesorter,
// Make the date be within +/- range of the 2 digit year // Make the date be within +/- range of the 2 digit year
// so if the current year is 2020, and the 2 digit year is 80 (2080 - 2020 > 50), it becomes 1980 // so if the current year is 2020, and the 2 digit year is 80 (2080 - 2020 > 50), it becomes 1980
// if the 2 digit year is 50 (2050 - 2020 < 50), then it becomes 2050. // if the 2 digit year is 50 (2050 - 2020 < 50), then it becomes 2050.
var range = 50, range = 50;
// ************ ts.dates = $.extend({}, ts.dates, {
regxxxxyy = /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/, regxxxxyy: /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/,
regyyxxxx = /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/, regyyxxxx: /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/
formatDate = function(s, regex, format){ });
ts.formatDate = function(s, regex, format){
s = s s = s
// replace separators // replace separators
.replace(/\s+/g," ").replace(/[-.,]/g, "/") .replace(/\s+/g," ").replace(/[-.,]/g, "/")
// reformat xx/xx/xx to mm/dd/19yy; // reformat xx/xx/xx to mm/dd/19yy;
.replace(regex, format); .replace(regex, format);
var d = new Date(s), y = d.getFullYear(), var d = new Date(s),
now = new Date().getFullYear(); y = d.getFullYear(),
now = new Date().getFullYear();
// if date > 50 years old (set range), add 100 years // if date > 50 years old (set range), add 100 years
// this will work when people start using "50" and mean "2050" // this will work when people start using "50" and mean "2050"
while (now - y > range) { while (now - y > range) {
@ -36,7 +41,7 @@
}, },
format: function(s) { format: function(s) {
// reformat dd/mm/yy to mm/dd/19yy; // reformat dd/mm/yy to mm/dd/19yy;
return formatDate(s, regxxxxyy, "$2/$1/19$3"); return ts.formatDate(s, ts.dates.regxxxxyy, "$2/$1/19$3");
}, },
type: "numeric" type: "numeric"
}); });
@ -48,7 +53,7 @@
}, },
format: function(s) { format: function(s) {
// reformat mm/dd/yy to mm/dd/19yy // reformat mm/dd/yy to mm/dd/19yy
return formatDate(s, regxxxxyy, "$1/$2/19$3"); return ts.formatDate(s, ts.dates.regxxxxyy, "$1/$2/19$3");
}, },
type: "numeric" type: "numeric"
}); });
@ -60,7 +65,7 @@
}, },
format: function(s) { format: function(s) {
// reformat yy/mm/dd to mm/dd/19yy // reformat yy/mm/dd to mm/dd/19yy
return formatDate(s, regyyxxxx, "$2/$3/19$1"); return ts.formatDate(s, ts.dates.regyyxxxx, "$2/$3/19$1");
}, },
type: "numeric" type: "numeric"
}); });

View File

@ -5,13 +5,14 @@
;(function($){ ;(function($){
"use strict"; "use strict";
$.tablesorter.dates = $.extend({}, $.tablesorter.dates, { var ts = $.tablesorter;
ts.dates = $.extend({}, ts.dates, {
// *** modify this array to change match the language *** // *** modify this array to change match the language ***
weekdayCased : [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ] weekdayCased : [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]
}); });
$.tablesorter.dates.weekdayLower = $.tablesorter.dates.weekdayCased.join(',').toLocaleLowerCase().split(','); ts.dates.weekdayLower = ts.dates.weekdayCased.join(',').toLocaleLowerCase().split(',');
$.tablesorter.addParser({ ts.addParser({
id: "weekday", id: "weekday",
is: function(){ is: function(){
return false; return false;
@ -19,7 +20,7 @@
format: function(s, table) { format: function(s, table) {
var j = -1, c = table.config; var j = -1, c = table.config;
s = c.ignoreCase ? s.toLocaleLowerCase() : s; s = c.ignoreCase ? s.toLocaleLowerCase() : s;
$.each($.tablesorter.dates[ 'weekday' + (c.ignoreCase ? 'Lower' : 'Cased') ], function(i,v){ $.each(ts.dates[ 'weekday' + (c.ignoreCase ? 'Lower' : 'Cased') ], function(i,v){
if (j < 0 && s.match(v)) { j = i; } if (j < 0 && s.match(v)) { j = i; }
}); });
// return s (original string) if there isn't a match // return s (original string) if there isn't a match

View File

@ -14,7 +14,7 @@
return false; return false;
}, },
format: function(s) { format: function(s) {
return Date.create ? Date.create(s).getTime() || s : s; return Date.create ? Date.create(s).getTime() || s : new Date(s).getTime() || s;
}, },
type: "numeric" type: "numeric"
}); });

View File

@ -1,20 +1,21 @@
/*! Distance parser /*! Distance parser
* This parser will parser numbers like 5'10" (5 foot 10 inches) * This parser will parser numbers like 5'10" (5 foot 10 inches)
* and 31½ into sortable values. * and 31½ into sortable values.
* Demo: http://jsfiddle.net/Mottie/abkNM/154/ * Demo: http://jsfiddle.net/Mottie/abkNM/154/
*/ */
/*global jQuery: false */ /*global jQuery: false */
;(function($){ ;(function($){
"use strict"; "use strict";
var symbolRegex = /[\u215b\u215c\u215d\u215e\u00bc\u00bd\u00be]/g, var ts = $.tablesorter;
processFractions = function(n, table){ ts.symbolRegex = /[\u215b\u215c\u215d\u215e\u00bc\u00bd\u00be]/g;
ts.processFractions = function(n, table) {
if (n) { if (n) {
var t, p = 0; var t, p = 0;
n = $.trim(n.replace(/\"/,'')); n = $.trim(n.replace(/\"/,''));
// look for a space in the first part of the number: "10 3/4" and save the "10" // look for a space in the first part of the number: "10 3/4" and save the "10"
if (/\s/.test(n)) { if (/\s/.test(n)) {
p = $.tablesorter.formatFloat(n.split(' ')[0], table); p = ts.formatFloat(n.split(' ')[0], table);
// remove stuff to the left of the space // remove stuff to the left of the space
n = $.trim(n.substring(n.indexOf(' '), n.length)); n = $.trim(n.substring(n.indexOf(' '), n.length));
} }
@ -24,8 +25,8 @@
// turn 3/4 into .75; make sure we don't divide by zero // turn 3/4 into .75; make sure we don't divide by zero
n = p + parseInt(t[0], 10) / parseInt(t[1] || 1, 10); n = p + parseInt(t[0], 10) / parseInt(t[1] || 1, 10);
// look for fraction symbols // look for fraction symbols
} else if (symbolRegex.test(n)) { } else if (ts.symbolRegex.test(n)) {
n = p + n.replace(symbolRegex, function(m){ n = p + n.replace(ts.symbolRegex, function(m){
return { return {
'\u215b' : '.125', // 1/8 '\u215b' : '.125', // 1/8
'\u215c' : '.375', // 3/8 '\u215c' : '.375', // 3/8
@ -49,12 +50,11 @@
}, },
format: function(s, table) { format: function(s, table) {
if (s === '') { return ''; } if (s === '') { return ''; }
// look for feet symbol = ' // look for feet symbol = '
// very generic test to catch 1.1', 1 1/2' and 1½' // very generic test to catch 1.1', 1 1/2' and 1½'
var d = (/^\s*\S*(\s+\S+)?\s*\'/.test(s)) ? s.split("'") : [0,s], var d = (/^\s*\S*(\s+\S+)?\s*\'/.test(s)) ? s.split("'") : [0,s],
f = processFractions(d[0], table), // feet f = ts.processFractions(d[0], table), // feet
i = processFractions(d[1], table); // inches i = ts.processFractions(d[1], table); // inches
return (/[\'\"]/).test(s) ? parseFloat(f) + (parseFloat(i)/12 || 0) : parseFloat(f) + parseFloat(i); return (/[\'\"]/).test(s) ? parseFloat(f) + (parseFloat(i)/12 || 0) : parseFloat(f) + parseFloat(i);
}, },
type: 'numeric' type: 'numeric'

View File

@ -47,7 +47,7 @@
b, t, b, t,
// process number here to get a numerical format (us or eu) // process number here to get a numerical format (us or eu)
n = $.tablesorter.formatFloat(s.replace(/[^\w,. \-()]/g, ""), table), n = $.tablesorter.formatFloat(s.replace(/[^\w,. \-()]/g, ""), table),
$t = $(table).find('thead').children().children('[data-column="' + cellIndex + '"]'), $t = table.config.$headers.filter('[data-column="' + cellIndex + '"]'),
m = $t.data('metric'); m = $t.data('metric');
if (!m) { if (!m) {
// stored values // stored values