"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/ms/index.js" (8 Mar 2017, 2332 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * Helpers.
    3  */
    4 
    5 var s = 1000;
    6 var m = s * 60;
    7 var h = m * 60;
    8 var d = h * 24;
    9 var y = d * 365.25;
   10 
   11 /**
   12  * Parse or format the given `val`.
   13  *
   14  * Options:
   15  *
   16  *  - `long` verbose formatting [false]
   17  *
   18  * @param {String|Number} val
   19  * @param {Object} options
   20  * @return {String|Number}
   21  * @api public
   22  */
   23 
   24 module.exports = function(val, options){
   25   options = options || {};
   26   if ('string' == typeof val) return parse(val);
   27   return options.long
   28     ? long(val)
   29     : short(val);
   30 };
   31 
   32 /**
   33  * Parse the given `str` and return milliseconds.
   34  *
   35  * @param {String} str
   36  * @return {Number}
   37  * @api private
   38  */
   39 
   40 function parse(str) {
   41   str = '' + str;
   42   if (str.length > 10000) return;
   43   var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str);
   44   if (!match) return;
   45   var n = parseFloat(match[1]);
   46   var type = (match[2] || 'ms').toLowerCase();
   47   switch (type) {
   48     case 'years':
   49     case 'year':
   50     case 'yrs':
   51     case 'yr':
   52     case 'y':
   53       return n * y;
   54     case 'days':
   55     case 'day':
   56     case 'd':
   57       return n * d;
   58     case 'hours':
   59     case 'hour':
   60     case 'hrs':
   61     case 'hr':
   62     case 'h':
   63       return n * h;
   64     case 'minutes':
   65     case 'minute':
   66     case 'mins':
   67     case 'min':
   68     case 'm':
   69       return n * m;
   70     case 'seconds':
   71     case 'second':
   72     case 'secs':
   73     case 'sec':
   74     case 's':
   75       return n * s;
   76     case 'milliseconds':
   77     case 'millisecond':
   78     case 'msecs':
   79     case 'msec':
   80     case 'ms':
   81       return n;
   82   }
   83 }
   84 
   85 /**
   86  * Short format for `ms`.
   87  *
   88  * @param {Number} ms
   89  * @return {String}
   90  * @api private
   91  */
   92 
   93 function short(ms) {
   94   if (ms >= d) return Math.round(ms / d) + 'd';
   95   if (ms >= h) return Math.round(ms / h) + 'h';
   96   if (ms >= m) return Math.round(ms / m) + 'm';
   97   if (ms >= s) return Math.round(ms / s) + 's';
   98   return ms + 'ms';
   99 }
  100 
  101 /**
  102  * Long format for `ms`.
  103  *
  104  * @param {Number} ms
  105  * @return {String}
  106  * @api private
  107  */
  108 
  109 function long(ms) {
  110   return plural(ms, d, 'day')
  111     || plural(ms, h, 'hour')
  112     || plural(ms, m, 'minute')
  113     || plural(ms, s, 'second')
  114     || ms + ' ms';
  115 }
  116 
  117 /**
  118  * Pluralization helper.
  119  */
  120 
  121 function plural(ms, n, name) {
  122   if (ms < n) return;
  123   if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
  124   return Math.ceil(ms / n) + ' ' + name + 's';
  125 }