[PATCH v2] web: Made elapsed time calculation dynamic (javascript).

Bill Barry after.fallout at gmail.com
Thu Apr 28 09:11:35 CDT 2011


some minor suggestions about the js code here:
1. surround block with self executing anonymous function so as not to 
pollute global scope:
     (function(){
         ...code...
     })();
2. do not blindly overwrite window.onload, store and use previous value:

     var prev_onload = window.onload;
     window.onload = function () {
         if(prev_onload) prev_onload();
         process_dates();
     }

3. In case of future usage of a js minimizer send window (and other 
globals in use) into anonymous function:
     (function(window, document, isNaN, Math, RegExp, Date, _false, _true) {
         ...code...
     })(window, document, isNaN, Math, RegExp, Date, false, true);
(and use _false and _true in place of false and true in the functions)

4. also for minimizer usage, quote the property names for the scales 
object because they are used as part of a string being returned:
     var scales = {
         'year':  365 * 24 * 60 * 60,
         'month':  30 * 24 * 60 * 60,
         'week':    7 * 24 * 60 * 60,
         'day':    24 * 60 * 60,
         'hour':   60 * 60,
         'minute': 60,
         'second': 1
     };

On 4/27/2011 3:21 AM, Benoît Allard wrote:
>
> +var scales = {
> +    year:  365 * 24 * 60 * 60,
> +    month:  30 * 24 * 60 * 60,
> +    week:    7 * 24 * 60 * 60,
> +    day:    24 * 60 * 60,
> +    hour:   60 * 60,
> +    minute: 60,
> +    second: 1
> +};
> +
> +function format(count, string){
> +    var ret = count + ' ' + string;
> +    if (count > 1){
> +        ret = ret + 's';
> +    }
> +    return ret;
> +}
> +
> +function age(datestr){
> +    var now = new Date();
> +    var once = new Date(datestr);
> +
> +    if (isNaN(once.getTime())){
> +        // parsing error
> +        return datestr;
> +    }
> +
> +    var delta = Math.floor((now.getTime() - once.getTime()) / 1000);
> +
> +    var future = false;
> +    if (delta < 0){
> +        future = true;
> +        delta = -delta;
> +        if (delta > (30 * scales.year)){
> +            return "in the distant future";
> +        }
> +    }
> +
> +    if (delta > (2 * scales.year)){
> +        return once.getFullYear() + '-' + once.getMonth() + '-' + 
> once.getDate();
> +    }
> +
> +    for (unit in scales){
> +        var s = scales[unit];
> +        var n = Math.floor(delta / s);
> +        if ((n >= 2) || (s == 1)){
> +            if (future){
> +                return format(n, unit) + ' from now';
> +            } else {
> +                return format(n, unit) + ' ago';
> +            }
> +        }
> +    }
> +}
> +
> +function process_dates(){
> +    var nodes = document.getElementsByTagName('*');
> +    var ageclass = new RegExp('\\bage\\b');
> +    var dateclass = new RegExp('\\bdate\\b');
> +    for (var i=0; i<nodes.length; ++i){
> +        var node = nodes[i];
> +        var classes = node.className;
> +        if (ageclass.test(classes)){
> +            var agevalue = age(node.textContent);
> +            if (dateclass.test(classes)){
> +                // We want both: date + (age)
> +                node.textContent += ' ('+agevalue+')';
> +            } else {
> +                node.textContent = agevalue;
> +            }
> +        }
> +    }
> +}
> +
> +window.onload = function () {
> +    process_dates();
> +} 



More information about the Mercurial-devel mailing list