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

32 lines
2.5 KiB
JavaScript

/*! Parser: metric */
/*
* Demo: http://jsfiddle.net/Mottie/abkNM/382/
* Set the metric name in the header (defaults to 'm|meter'), e.g.
* <th data-metric-name-abbr="b|B" data-metric-name-full="byte|Byte|BYTE">HDD Size</th>
* <th data-metric-name="m|meter">Distance</th> <!-- data-metric-name is deprecated in v2.22.2 -->
*/
/*jshint jquery:true */
!function(a){"use strict";var b={
// 'prefix' : [ base 10, base 2 ]
// skipping IEEE 1541 defined prefixes: kibibyte, mebibyte, etc, for now.
"Y|Yotta|yotta":[1e24,Math.pow(1024,8)],// 1024^8
"Z|Zetta|zetta":[1e21,Math.pow(1024,7)],// 1024^7
"E|Exa|exa":[1e18,Math.pow(1024,6)],// 1024^6
"P|Peta|peta":[1e15,Math.pow(1024,5)],// 1024^5
"T|Tera|tera":[1e12,Math.pow(1024,4)],// 1024^4
"G|Giga|giga":[1e9,Math.pow(1024,3)],// 1024^3
"M|Mega|mega":[1e6,Math.pow(1024,2)],// 1024^2
"k|Kilo|kilo":[1e3,1024],// 1024
// prefixes below here are rarely, if ever, used in binary
"h|hecto":[100,100],"da|deka":[10,10],"d|deci":[.1,.1],"c|centi":[.01,.01],"m|milli":[.001,.001],"µ|micro":[1e-6,1e-6],"n|nano":[1e-9,1e-9],"p|pico":[1e-12,1e-12],"f|femto":[1e-15,1e-15],"a|atto":[1e-18,1e-18],"z|zepto":[1e-21,1e-21],"y|yocto":[1e-24,1e-24]},
// the \\d+ will not catch digits with spaces, commas or decimals; so use the value from n instead
c="(\\d+)(\\s+)?([Zz]etta|[Ee]xa|[Pp]eta|[Tt]era|[Gg]iga|[Mm]ega|kilo|hecto|deka|deci|centi|milli|micro|nano|pico|femto|atto|zepto|yocto)(",d="(\\d+)(\\s+)?(Z|E|P|T|G|M|k|h|da|d|c|m|µ|n|p|f|a|z|y)(",
// make these case-insensitive because we all forget the case for these binary values
e=/^[b|bit|byte|o|octet]/i;a.tablesorter.addParser({id:"metric",is:function(){return!1},format:function(f,g,h,i){var j,k,l,m,
// default base unit name
n="m|meter",
// process number here to get a numerical format (us or eu)
o=a.tablesorter.formatFloat(f.replace(/[^\w,. \-()]/g,""),g),p=g.config.$headerIndexed[i],q=p.data("metric");if(q||(j=(p.attr("data-metric-name")||n).split("|"),l=p.attr("data-metric-name-full")||"",m=p.attr("data-metric-name-abbr")||"",q=[l||j[1]||j[0].substring(1),m||j[0]],k=e.test(q.join("")),q[2]=new RegExp(c+((""===l?"":l+"|"+m)||(k?q[0].toLowerCase()+"|"+q[0].toUpperCase():q[0])+"|"+(k?q[1].toLowerCase()+"|"+q[1].toUpperCase():q[1]))+")"),q[3]=new RegExp(d+(m||(k?q[1].toLowerCase()+"|"+q[1].toUpperCase():q[1]))+")"),p.data("metric",q)),j=f.match(q[2])||f.match(q[3]))for(n in b)if(j[3].match(n))
// exception when using binary prefix
// change base for binary use
return k=e.test(j[4])?1:0,o*b[n][k];return o},type:"numeric"})}(jQuery);