tablesorter/dist/js/parsers/parser-feet-inch-fraction.min.js
2015-10-31 10:20:22 -05:00

25 lines
1.3 KiB
JavaScript

/*! Parser: distance */
/*
* 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(a){"use strict";var b=a.tablesorter;b.symbolRegex=/[\u215b\u215c\u215d\u215e\u00bc\u00bd\u00be]/g,b.processFractions=function(c,d){if(c){var e,f=0;c=a.trim(c.replace(/\"/,"")),
// look for a space in the first part of the number: '10 3/4' and save the '10'
/\s/.test(c)&&(f=b.formatFloat(c.split(" ")[0],d),c=a.trim(c.substring(c.indexOf(" "),c.length))),
// look for a '/' to calculate fractions
/\//g.test(c)?(e=c.split("/"),c=f+parseInt(e[0],10)/parseInt(e[1]||1,10)):b.symbolRegex.test(c)&&(c=f+c.replace(b.symbolRegex,function(a){return{"⅛":".125",// 1/8
"⅜":".375",// 3/8
"⅝":".625",// 5/8
"⅞":".875",// 7/8
"¼":".25",// 1/4
"½":".5",// 1/2
"¾":".75"}[a]}))}return c||0},a.tablesorter.addParser({id:"distance",is:function(){
// return false so this parser is not auto detected
return!1},format:function(a,c){if(""===a)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(a)?a.split(/\'/):[0,a],e=b.processFractions(d[0],c),// feet
f=b.processFractions(d[1],c);// inches
return/[\'\"]/.test(a)?parseFloat(e)+(parseFloat(f)/12||0):parseFloat(e)+parseFloat(f)},type:"numeric"})}(jQuery);