tablesorter/js/parsers/parser-duration.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

/*! Duration parser
*/
/*jshint jquery:true, unused:false */
;(function($){
"use strict";
// If any number > 9999, then set table.config.durationLength = 5
// The below regex matches this duration example: 1y 23d 12h 44m 9s
$.tablesorter.addParser({
id: "duration",
is: function() {
return false;
},
format: function(s, table) {
var i, time,
c = table.config,
t = '',
duration = '',
len = c.durationLength || 4,
str = new Array(len + 1).join('0'),
2014-09-16 00:14:50 +00:00
labels = (c.durationLabels || '(?:years|year|y),(?:days|day|d),(?:hours|hour|h),(?:minutes|minute|min|m),(?:seconds|second|sec|s)').split(/\s*,\s*/),
llen = labels.length;
2014-09-16 00:14:50 +00:00
// build regex
if (!c.durationRegex) {
for (i = 0; i < llen; i++) {
2014-09-16 00:14:50 +00:00
t += '(?:(\\d+)\\s*' + labels[i] + '\\s*)?';
}
c.durationRegex = new RegExp(t, 'i');
}
2014-09-16 00:14:50 +00:00
// remove commas from value
time = ( c.usNumberFormat ? s.replace(/,/g, '') : s.replace( /(\d)(?:\.|\s*)(\d)/g, '$1$2') ).match(c.durationRegex);
for (i = 1; i < llen + 1; i++) {
duration += ( str + ( time[i] || 0 ) ).slice(-len);
}
return duration;
},
type: "text"
});
})(jQuery);