/*
 * Licensed under the Apache License, Version 2.0
 * See accompanying LICENSE file.
 */
angular.module('dashing.util.text', [])

  .factory('dashing.util.text', function() {
    'use strict';

    return {
      /**
       * Return the human readable number notation.
       */
      toHumanReadableNumber: function(value, base, digits) {
        var modifier = '';
        if (value !== 0) {
          if (base !== 1024) {
            base = 1000;
          }
          var positive = value > 0;
          var positiveValue = Math.abs(value);
          var s = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
          var e = Math.floor(Math.log(positiveValue) / Math.log(base));
          value = positiveValue / Math.pow(base, e);
          if (digits > 0 && value !== Math.floor(value)) {
            value = value.toFixed(digits);
          }
          if (!positive) {
            value *= -1;
          }
          modifier = s[e];
        }
        return {value: value, modifier: modifier};
      },
      /**
       * Return the human readable duration notation.
       */
      toHumanReadableDuration: function(millis, compact) {
        var x = parseInt(millis, 10);
        if (isNaN(x)) {
          return millis;
        }

        var units = [
          {label: ' ms', mod: 1000},
          {label: compact ? 's' : ' secs', mod: 60},
          {label: compact ? 'm' : ' mins', mod: 60},
          {label: compact ? 'h' : ' hours', mod: 24},
          {label: compact ? 'd' : ' days', mod: 7},
          {label: compact ? 'w' : ' weeks', mod: 52}
        ];
        var duration = [];

        for (var i = 0; i < units.length; i++) {
          var unit = units[i];
          var t = x % unit.mod;
          if (t !== 0) {
            duration.unshift({label: unit.label, value: t});
          }
          x = (x - t) / unit.mod;
        }

        duration = duration.slice(0, 2);
        if (duration.length > 1 && duration[1].label === ' ms') {
          duration = [duration[0]];
        }
        return duration.map(function(unit) {
          return unit.value + unit.label;
        }).join(compact ? ' ' : ' and ');
      }
    };
  })
;
@@ -1272,18 +1272,36 @@
    %7B
-label: ' m
+text: compact ? 'ms' : 'msec
 s', 
@@ -1315,37 +1315,36 @@
 00%7D,%0A          %7B
-label
+text
 : compact ? 's' 
@@ -1346,17 +1346,16 @@
  's' : '
- 
 secs', m
@@ -1366,37 +1366,36 @@
 60%7D,%0A          %7B
-label
+text
 : compact ? 'm' 
@@ -1397,17 +1397,16 @@
  'm' : '
- 
 mins', m
@@ -1417,37 +1417,36 @@
 60%7D,%0A          %7B
-label
+text
 : compact ? 'h' 
@@ -1448,17 +1448,16 @@
  'h' : '
- 
 hours', 
@@ -1469,37 +1469,36 @@
 24%7D,%0A          %7B
-label
+text
 : compact ? 'd' 
@@ -1500,17 +1500,16 @@
  'd' : '
- 
 days', m
@@ -1527,21 +1527,20 @@
        %7B
-label
+text
 : compac
@@ -1550,17 +1550,16 @@
  'w' : '
- 
 weeks', 
@@ -1777,25 +1777,23 @@
 ft(%7B
-label
+unit
 : unit.
-label
+text
 , va
@@ -1912,99 +1912,308 @@
     
-if (duration.length %3E 1 && duration%5B1%5D.label === ' ms') %7B%0A          duration = %5Bduration%5B0%5D
+var millisUnit = units%5B0%5D.text%0A        if (duration.length %3E 1 && duration%5B1%5D.unit === millisUnit) %7B%0A          // Reduce the text length by removing milliseconds part%0A          duration = %5Bduration%5B0%5D%5D;%0A        %7D else if (duration.length === 0) %7B%0A          duration = %5B%7Bvalue: '%3C1', unit: millisUnit%7D
 %5D;%0A 
@@ -2258,20 +2258,17 @@
 unction(
-unit
+p
 ) %7B%0A    
@@ -2280,20 +2280,17 @@
  return 
-unit
+p
 .value +
@@ -2294,18 +2294,37 @@
 e + 
-unit.label
+(compact ? '' : ' ') + p.unit
 ;%0A  

fix #68: when duration is less than 1ms, show text as "<1ms"