| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
;( function( $ ) { |
| 9 |
'use strict'; |
| 10 |
|
| 11 |
var prefixes = { |
| 12 |
|
| 13 |
|
| 14 |
'Y|Yotta|yotta' : [ 1e24, Math.pow(1024, 8) ], |
| 15 |
'Z|Zetta|zetta' : [ 1e21, Math.pow(1024, 7) ], |
| 16 |
'E|Exa|exa' : [ 1e18, Math.pow(1024, 6) ], |
| 17 |
'P|Peta|peta' : [ 1e15, Math.pow(1024, 5) ], |
| 18 |
'T|Tera|tera' : [ 1e12, Math.pow(1024, 4) ], |
| 19 |
'G|Giga|giga' : [ 1e9, Math.pow(1024, 3) ], |
| 20 |
'M|Mega|mega' : [ 1e6, Math.pow(1024, 2) ], |
| 21 |
'k|Kilo|kilo' : [ 1e3, 1024 ], |
| 22 |
|
| 23 |
'h|hecto' : [ 1e2, 1e2 ], |
| 24 |
'da|deka' : [ 1e1, 1e1 ], |
| 25 |
'd|deci' : [ 1e-1, 1e-1 ], |
| 26 |
'c|centi' : [ 1e-2, 1e-2 ], |
| 27 |
'm|milli' : [ 1e-3, 1e-3 ], |
| 28 |
'µ|micro' : [ 1e-6, 1e-6 ], |
| 29 |
'n|nano' : [ 1e-9, 1e-9 ], |
| 30 |
'p|pico' : [ 1e-12, 1e-12 ], |
| 31 |
'f|femto' : [ 1e-15, 1e-15 ], |
| 32 |
'a|atto' : [ 1e-18, 1e-18 ], |
| 33 |
'z|zepto' : [ 1e-21, 1e-21 ], |
| 34 |
'y|yocto' : [ 1e-24, 1e-24 ] |
| 35 |
}, |
| 36 |
|
| 37 |
RegLong = '(\\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)(', |
| 38 |
RegAbbr = '(\\d+)(\\s+)?(Z|E|P|T|G|M|k|h|da|d|c|m|µ|n|p|f|a|z|y)(', |
| 39 |
|
| 40 |
byteTest = /^[b|bit|byte|o|octet]/i; |
| 41 |
|
| 42 |
$.tablesorter.addParser({ |
| 43 |
id: 'metric', |
| 44 |
is: function() { |
| 45 |
return false; |
| 46 |
}, |
| 47 |
format: function(txt, table, cell, cellIndex) { |
| 48 |
var unit, isBinary, nameLong, nameAbbr, |
| 49 |
|
| 50 |
base = 'm|meter', |
| 51 |
|
| 52 |
num = $.tablesorter.formatFloat( txt.replace(/[^\w,. \-()]/g, ''), table ), |
| 53 |
$t = table.config.$headerIndexed[ cellIndex ], |
| 54 |
regex = $t.data( 'metric' ); |
| 55 |
if ( !regex ) { |
| 56 |
|
| 57 |
unit = ( $t.attr('data-metric-name') || base ).split( '|' ); |
| 58 |
nameLong = $t.attr( 'data-metric-name-full' ) || ''; |
| 59 |
nameAbbr = $t.attr( 'data-metric-name-abbr' ) || ''; |
| 60 |
regex = [ nameLong || unit[1] || unit[0].substring(1), nameAbbr || unit[0] ]; |
| 61 |
isBinary = byteTest.test( regex.join( '' ) ); |
| 62 |
|
| 63 |
regex[2] = new RegExp( RegLong + ( |
| 64 |
( nameLong === '' ? '' : nameLong + '|' + nameAbbr ) || |
| 65 |
|
| 66 |
( ( isBinary ? regex[0].toLowerCase() + '|' + regex[0].toUpperCase() : regex[0] ) + '|' + |
| 67 |
( isBinary ? regex[1].toLowerCase() + '|' + regex[1].toUpperCase() : regex[1] ) ) ) + |
| 68 |
')' ); |
| 69 |
|
| 70 |
regex[3] = new RegExp( RegAbbr + ( nameAbbr || |
| 71 |
( ( isBinary ? regex[1].toLowerCase() + '|' + regex[1].toUpperCase() : regex[1] ) ) ) + |
| 72 |
')' ); |
| 73 |
$t.data( 'metric', regex ); |
| 74 |
} |
| 75 |
|
| 76 |
unit = txt.match( regex[2] ) || txt.match( regex[3] ); |
| 77 |
if ( unit ) { |
| 78 |
for ( base in prefixes ) { |
| 79 |
if ( unit[3].match( base ) ) { |
| 80 |
|
| 81 |
|
| 82 |
isBinary = byteTest.test( unit[4] ) ? 1 : 0; |
| 83 |
return num * prefixes[ base ][ isBinary ]; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
return num; |
| 88 |
}, |
| 89 |
type: 'numeric' |
| 90 |
}); |
| 91 |
|
| 92 |
})(jQuery); |