Sha256: 4d5046c261ae7aa4671b70f248690d6624ce39b9d3198090f3373a6f3b570ea4

Contents?: true

Size: 1.86 KB

Versions: 32

Compression:

Stored size: 1.86 KB

Contents

/*! Distance parser
* This parser will parser numbers like 5'10" (5 foot 10 inches)
* and 31½ into sortable values.
* Demo: http://jsfiddle.net/Mottie/abkNM/154/
*/
/*global jQuery: false */
;(function($){
	"use strict";

	var ts = $.tablesorter;
	ts.symbolRegex = /[\u215b\u215c\u215d\u215e\u00bc\u00bd\u00be]/g;
	ts.processFractions = function(n, table) {
		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"
			if (/\s/.test(n)) {
				p = ts.formatFloat(n.split(' ')[0], table);
				// remove stuff to the left of the space
				n = $.trim(n.substring(n.indexOf(' '), n.length));
			}
			// look for a "/" to calculate fractions
			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
			} else if (ts.symbolRegex.test(n)) {
				n = p + n.replace(ts.symbolRegex, function(m){
					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],
			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);
		},
		type: 'numeric'
	});

})(jQuery);

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
jquery-tablesorter-1.16.3 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.16.2 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.16.1 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.16.0 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.15.0 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.14.1 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.14.0 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.13.4 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.13.3 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.13.2 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.13.1 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.13.0 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.8 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.7 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.6 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.5 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.4 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.3 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.2 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js
jquery-tablesorter-1.12.1 vendor/assets/javascripts/jquery-tablesorter/parsers/parser-feet-inch-fraction.js