Debian Codegraph @

js/parsers/parser-metric.js

raw symbols SCIP
1 /*! Parser: metric *//*
2 * Demo: http://jsfiddle.net/Mottie/abkNM/382/
3 * Set the metric name in the header (defaults to 'm|meter'), e.g.
4 * <th data-metric-name-abbr="b|B" data-metric-name-full="byte|Byte|BYTE">HDD Size</th>
5 * <th data-metric-name="m|meter">Distance</th> <!-- data-metric-name is deprecated in v2.22.2 -->
6 */
7 /*jshint jquery:true */
8 ;( function( $ ) {
9 'use strict';
10
11 var prefixes = {
12 // 'prefix' : [ base 10, base 2 ]
13 // skipping IEEE 1541 defined prefixes: kibibyte, mebibyte, etc, for now.
14 'Y|Yotta|yotta' : [ 1e24, Math.pow(1024, 8) ], // 1024^8
15 'Z|Zetta|zetta' : [ 1e21, Math.pow(1024, 7) ], // 1024^7
16 'E|Exa|exa' : [ 1e18, Math.pow(1024, 6) ], // 1024^6
17 'P|Peta|peta' : [ 1e15, Math.pow(1024, 5) ], // 1024^5
18 'T|Tera|tera' : [ 1e12, Math.pow(1024, 4) ], // 1024^4
19 'G|Giga|giga' : [ 1e9, Math.pow(1024, 3) ], // 1024^3
20 'M|Mega|mega' : [ 1e6, Math.pow(1024, 2) ], // 1024^2
21 'k|Kilo|kilo' : [ 1e3, 1024 ], // 1024
22 // prefixes below here are rarely, if ever, used in binary
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 // the \\d+ will not catch digits with spaces, commas or decimals; so use the value from n instead
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 // make these case-insensitive because we all forget the case for these binary values
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 // default base unit name
50 base = 'm|meter',
51 // process number here to get a numerical format (us or eu)
52 num = $.tablesorter.formatFloat( txt.replace(/[^\w,. \-()]/g, ''), table ),
53 $t = table.config.$headerIndexed[ cellIndex ],
54 regex = $t.data( 'metric' );
55 if ( !regex ) {
56 // stored values
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 // adding 'data-metric-name-full' which would contain 'byte|BYTE|Byte' etc
63 regex[2] = new RegExp( RegLong + (
64 ( nameLong === '' ? '' : nameLong + '|' + nameAbbr ) ||
65 // with data-metric-name='b|byte', we end up with 'b|B|byte|BYTE' - maybe not the best solution for case-insensitivity
66 ( ( isBinary ? regex[0].toLowerCase() + '|' + regex[0].toUpperCase() : regex[0] ) + '|' +
67 ( isBinary ? regex[1].toLowerCase() + '|' + regex[1].toUpperCase() : regex[1] ) ) ) +
68 ')' );
69 // adding 'data-metric-name-abbr' which would contain 'b|B' etc
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 // find match to full name or abbreviation
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 // exception when using binary prefix
81 // change base for binary use
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);