Debian Codegraph @

js/parsers/parser-duration.js

raw symbols SCIP
1 /*! Parser: duration & countdown - updated 2/7/2015 (v2.19.0) */
2 /*jshint jquery:true, unused:false */
3 ;(function($) {
4 'use strict';
5
6 // If any number > 9999, then set table.config.durationLength = 5
7 // The below regex matches this duration example: 1y 23d 12h 44m 9s
8 $.tablesorter.addParser({
9 id: 'duration',
10 is: function() {
11 return false;
12 },
13 format: function(s, table) {
14 var i, time,
15 c = table.config,
16 t = '',
17 duration = '',
18 len = c.durationLength || 4,
19 str = new Array(len + 1).join('0'),
20 labels = (c.durationLabels || '(?:years|year|y),(?:days|day|d),(?:hours|hour|h),(?:minutes|minute|min|m),(?:seconds|second|sec|s)').split(/\s*,\s*/),
21 llen = labels.length;
22 // build regex
23 if (!c.durationRegex) {
24 for (i = 0; i < llen; i++) {
25 t += '(?:(\\d+)\\s*' + labels[i] + '\\s*)?';
26 }
27 c.durationRegex = new RegExp(t, 'i');
28 }
29 // remove commas from value
30 time = ( c.usNumberFormat ? s.replace(/,/g, '') : s.replace( /(\d)(?:\.|\s*)(\d)/g, '$1$2') ).match(c.durationRegex);
31 for (i = 1; i < llen + 1; i++) {
32 duration += ( str + ( time[i] || 0 ) ).slice(-len);
33 }
34 return duration;
35 },
36 type: 'text'
37 });
38
39 /*! Countdown parser ( hh:mm:ss ) */
40 /* Added 2/7/2015 (v2.19.0) - see http://stackoverflow.com/a/27023733/145346 */
41 $.tablesorter.addParser({
42 id: 'countdown',
43 is: function () {
44 return false;
45 },
46 format: function ( text, table ) {
47 // change maxDigits to 4, if values go > 999
48 // or to 5 for values > 9999, etc.
49 var maxDigits = table.config.durationLength || 4,
50 // prefix contains leading zeros that are tacked
51 prefix = new Array( maxDigits + 1 ).join( '0' ),
52 // split time into blocks
53 blocks = text.split( /\s*:\s*/ ),
54 len = blocks.length,
55 result = [];
56 // add values in reverse, so if there is only one block
57 // ( e.g. '10' ), then it would be the time in seconds
58 while ( len ) {
59 result.push( ( prefix + ( blocks[ --len ] || 0 ) ).slice( -maxDigits ) );
60 }
61 // reverse the results and join them
62 return result.length ? result.reverse().join( '' ) : text;
63 },
64 type: 'text'
65 });
66
67 })(jQuery);