tablesorter/js/parsers/parser-feet-inch-fraction.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

/*! Parser: distance *//*
2013-09-29 19:37:03 +00:00
* This parser will parser numbers like 5'10" (5 foot 10 inches)
* and 31½ into sortable values.
* Demo: http://jsfiddle.net/Mottie/abkNM/154/
*/
2013-03-27 23:13:44 +00:00
/*global jQuery: false */
2018-03-17 20:30:25 +00:00
;(function($) {
'use strict';
2013-03-27 23:13:44 +00:00
2013-09-29 19:37:03 +00:00
var ts = $.tablesorter;
ts.symbolRegex = /[\u215b\u215c\u215d\u215e\u00bc\u00bd\u00be]/g;
ts.processFractions = function(n, table) {
2013-03-27 23:13:44 +00:00
if (n) {
var t, p = 0;
n = $.trim(n.replace(/\"/, ''));
// look for a space in the first part of the number: '10 3/4' and save the '10'
2013-03-27 23:13:44 +00:00
if (/\s/.test(n)) {
2013-09-29 19:37:03 +00:00
p = ts.formatFloat(n.split(' ')[0], table);
2013-03-27 23:13:44 +00:00
// remove stuff to the left of the space
n = $.trim(n.substring(n.indexOf(' '), n.length));
}
// look for a '/' to calculate fractions
2013-03-27 23:13:44 +00:00
if (/\//g.test(n)) {
t = n.split('/');
// turn 3/4 into .75; make sure we don't divide by zero
n = p + parseInt(t[0], 10) / parseInt(t[1] || 1, 10);
// look for fraction symbols
2013-09-29 19:37:03 +00:00
} else if (ts.symbolRegex.test(n)) {
2018-03-17 20:30:25 +00:00
n = p + n.replace(ts.symbolRegex, function(m) {
2013-03-27 23:13:44 +00:00
return {
'\u215b' : '.125', // 1/8
'\u215c' : '.375', // 3/8
'\u215d' : '.625', // 5/8
'\u215e' : '.875', // 7/8
'\u00bc' : '.25', // 1/4
'\u00bd' : '.5', // 1/2
'\u00be' : '.75' // 3/4
}[m];
});
}
}
return n || 0;
};
$.tablesorter.addParser({
id: 'distance',
is: function() {
// return false so this parser is not auto detected
return false;
},
format: function(s, table) {
if (s === '') { return ''; }
// look for feet symbol = '
// 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 ],
2013-09-29 19:37:03 +00:00
f = ts.processFractions(d[0], table), // feet
i = ts.processFractions(d[1], table); // inches
return (/[\'\"]/).test(s) ? parseFloat(f) + (parseFloat(i) / 12 || 0) : parseFloat(f) + parseFloat(i);
2013-03-27 23:13:44 +00:00
},
type: 'numeric'
});
})(jQuery);