Debian Codegraph @

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

raw symbols SCIP
1 /*! Parser: distance *//*
2 * This parser will parser numbers like 5'10" (5 foot 10 inches)
3 * and 31½ into sortable values.
4 * Demo: http://jsfiddle.net/Mottie/abkNM/154/
5 */
6 /*global jQuery: false */
7 ;(function($) {
8 'use strict';
9
10 var ts = $.tablesorter;
11 ts.symbolRegex = /[\u215b\u215c\u215d\u215e\u00bc\u00bd\u00be]/g;
12 ts.processFractions = function(n, table) {
13 if (n) {
14 var t, p = 0;
15 n = $.trim(n.replace(/\"/, ''));
16 // look for a space in the first part of the number: '10 3/4' and save the '10'
17 if (/\s/.test(n)) {
18 p = ts.formatFloat(n.split(' ')[0], table);
19 // remove stuff to the left of the space
20 n = $.trim(n.substring(n.indexOf(' '), n.length));
21 }
22 // look for a '/' to calculate fractions
23 if (/\//g.test(n)) {
24 t = n.split('/');
25 // turn 3/4 into .75; make sure we don't divide by zero
26 n = p + parseInt(t[0], 10) / parseInt(t[1] || 1, 10);
27 // look for fraction symbols
28 } else if (ts.symbolRegex.test(n)) {
29 n = p + n.replace(ts.symbolRegex, function(m) {
30 return {
31 '\u215b' : '.125', // 1/8
32 '\u215c' : '.375', // 3/8
33 '\u215d' : '.625', // 5/8
34 '\u215e' : '.875', // 7/8
35 '\u00bc' : '.25', // 1/4
36 '\u00bd' : '.5', // 1/2
37 '\u00be' : '.75' // 3/4
38 }[m];
39 });
40 }
41 }
42 return n || 0;
43 };
44
45 $.tablesorter.addParser({
46 id: 'distance',
47 is: function() {
48 // return false so this parser is not auto detected
49 return false;
50 },
51 format: function(s, table) {
52 if (s === '') { return ''; }
53 // look for feet symbol = '
54 // very generic test to catch 1.1', 1 1/2' and 1½'
55 var d = (/^\s*\S*(\s+\S+)?\s*\'/.test(s)) ? s.split(/\'/) : [ 0, s ],
56 f = ts.processFractions(d[0], table), // feet
57 i = ts.processFractions(d[1], table); // inches
58 return (/[\'\"]/).test(s) ? parseFloat(f) + (parseFloat(i) / 12 || 0) : parseFloat(f) + parseFloat(i);
59 },
60 type: 'numeric'
61 });
62
63 })(jQuery);