User:PanSola/sortable mod.js/sort types

/* * This is a "nightly" version. * For stable version, see MediaWiki:sortable_mod.js/abstractions */ /*

Autodetect Ordering
*/ var autoDetectSortTypes = [sortType_numeric, sortType_currency, sortType_date, sortType_unit_num, sortType_num_unit, sortType_case_insensitive];

/*

Sort Type Abstractions
*/

function sortType_cons( name, guesserFn, keyFn, sorter){ return new Array( name, guessFn, keyFn, sorter ); }

function sortType_getName (sortType) { return sortType[0]; }

function sortType_getGuessFn (sortType) { return sortType[1]; }

function sortType_getKeyFn (sortType){ return sortType[2]; }

function sortType_getSorter (sortType) { return sortType[3]; }

/*

Standard sorting functions
*/

/* Pairs of functions that sorts in different directions. */ function makeSorterPairs (myFunction) { return cons(myFunction, inv(myFunction)); }

/*

ts_sort_default
*/ var ts_sort_default = makeSorterPairs(function (a,b) {	aa = rr_getSortKey(a);	bb = rr_getSortKey(b);	if (aa==bb) return 0;	if (aa<bb) return -1;	return 1; })

/*

Standard Types
*/

/*

case_insensitive and numeric
*/

sortType_case_insensitive = sortType_cons('case_insensitive', 	function(itm){return true;}, 	function (a) {return a.toLowerCase;},	ts_sort_default);

sortType_numeric = sortType_cons('numeric',	function(itm){		return (itm.match(/^[\d\.\,\-\+]+\%?$/) || itm.match(/^[\d\.\,\-\+]+[eE][\d\-\+]+\%?$/) || itm.match(/^[\d\.\,\-\+]+e[\d\-\+]+\u00d710[\d\-\+]+\%?$/) ) },	key_numeric,	ts_sort_default);

function key_numeric (a) { if (!a) a=0; a = parseFloat(a); a = (isNaN(a) ? 0 : a); return a; }

/*

date
*/

sortType_date = sortType_cons('date',	function(itm){		return (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/) || itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/) || itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/) ) },	key_date,	ts_sort_default);

function key_date(date) { // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX dt = "00000000"; if (date.length == 11) { monthstr = date.substr(3,3); monthstr = monthstr.toLowerCase; switch(monthstr) { case "jan": var month = "01"; break; case "feb": var month = "02"; break; case "mar": var month = "03"; break; case "apr": var month = "04"; break; case "may": var month = "05"; break; case "jun": var month = "06"; break; case "jul": var month = "07"; break; case "aug": var month = "08"; break; case "sep": var month = "09"; break; case "oct": var month = "10"; break; case "nov": var month = "11"; break; case "dec": var month = "12"; break; // default: var month = "00"; }		dt = date.substr(7,4)+month+date.substr(0,2); return dt; } else if (date.length == 10) { if (europeandate == false) { dt = date.substr(6,4)+date.substr(0,2)+date.substr(3,2); return dt; } else { dt = date.substr(6,4)+date.substr(3,2)+date.substr(0,2); return dt; }	} else if (date.length == 8) { yr = date.substr(6,2); if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }		if (europeandate == true) { dt = yr+date.substr(3,2)+date.substr(0,2); return dt; } else { dt = yr+date.substr(0,2)+date.substr(3,2); return dt; }	}	return dt; }

/*

unit_num & num_unit
*/

sortType_unit_num = sortType_cons('unit_num',	function (itm) {		return itm.match(/^[^\d.,¼½¾]+[\d.,]+\%?$/),	key_unit_num,	ts_sort_unit_num);

sortType_num_unit = sortType_cons('num_unit',	function (itm) {		return itm.match(/^[\d.,¼½¾]+\%?[^\d.,%]+$/),	key_num_unit,	ts_sort_unit_num);

var ts_sort_unit_num = makeSorterPairs(function (a,b) {	aa = rr_getSortKey(a)	bb = rr_getSortKey(b)	if (aa[0] == bb[0]) return (aa[1]-bb[1]);	if (aa[0] < bb[0]) return -1;	return 1; })

function key_unit_num(a) { var aa_idx = a.search(/[\d.¼½¾]/); var aa_unit = a.substr(0,aa_idx); var aa_num = convert_numeric(a.substr(aa_idx)); return [key_unit(aa_unit), key_numeric(aa_num)]; }

function key_num_unit(a) { var aa_idx = a.search(/[^\d.¼½¾]/); var aa_num = key_numeric(convert_numeric(a.substr(0,aa_idx))); var aa_unit = key_unit(a.substr(aa_idx)); //debug('Parsing "'+a+'" as "'+aa_unit+'" and "'+aa_num+'"'); return [aa_unit, aa_num]; }

function key_unit(unit){ return unit.toLowerCase; } /*

Custom Types
*/

/*

Currency
*/

sortType_currency = sortType_cons('currency',	function(itm){ return itm.match(/^[\u00a3$\u20ac\u00a5]/)}, // pound dollar euro yen	key_unit_num,	ts_sort_unit_num);