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

22 lines
1.5 KiB
JavaScript

/*! Parser: duration & countdown - updated 2/7/2015 (v2.19.0) */
/*jshint jquery:true, unused:false */
!function(a){"use strict";
// If any number > 9999, then set table.config.durationLength = 5
// The below regex matches this duration example: 1y 23d 12h 44m 9s
a.tablesorter.addParser({id:"duration",is:function(){return!1},format:function(a,b){var c,d,e=b.config,f="",g="",h=e.durationLength||4,i=new Array(h+1).join("0"),j=(e.durationLabels||"(?:years|year|y),(?:days|day|d),(?:hours|hour|h),(?:minutes|minute|min|m),(?:seconds|second|sec|s)").split(/\s*,\s*/),k=j.length;
// build regex
if(!e.durationRegex){for(c=0;k>c;c++)f+="(?:(\\d+)\\s*"+j[c]+"\\s*)?";e.durationRegex=new RegExp(f,"i")}for(d=(e.usNumberFormat?a.replace(/,/g,""):a.replace(/(\d)(?:\.|\s*)(\d)/g,"$1$2")).match(e.durationRegex),c=1;k+1>c;c++)g+=(i+(d[c]||0)).slice(-h);return g},type:"text"}),/*! Countdown parser ( hh:mm:ss ) */
/* Added 2/7/2015 (v2.19.0) - see http://stackoverflow.com/a/27023733/145346 */
a.tablesorter.addParser({id:"countdown",is:function(){return!1},format:function(a,b){
// add values in reverse, so if there is only one block
// ( e.g. '10' ), then it would be the time in seconds
for(
// change maxDigits to 4, if values go > 999
// or to 5 for values > 9999, etc.
var c=b.config.durationLength||4,
// prefix contains leading zeros that are tacked
d=new Array(c+1).join("0"),
// split time into blocks
e=a.split(/\s*:\s*/),f=e.length,g=[];f;)g.push((d+(e[--f]||0)).slice(-c));
// reverse the results and join them
return g.length?g.reverse().join(""):a},type:"text"})}(jQuery);