Added missing modules, including some as submodules.
[yaffs-website] / node_modules / pretty-hrtime / index.js
1 /*jshint node:true */\r
2 \r
3 "use strict";\r
4 \r
5 var minimalDesc = ['h', 'min', 's', 'ms', 'μs', 'ns'];\r
6 var verboseDesc = ['hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond'];\r
7 var convert = [60*60, 60, 1, 1e6, 1e3, 1];\r
8 \r
9 module.exports = function (source, opts) {\r
10         var verbose, precise, i, spot, sourceAtStep, valAtStep, decimals, strAtStep, results, totalSeconds;\r
11 \r
12         verbose = false;\r
13         precise = false;\r
14         if (opts) {\r
15                 verbose = opts.verbose || false;\r
16                 precise = opts.precise || false;\r
17         }\r
18 \r
19         if (!Array.isArray(source) || source.length !== 2) {\r
20                 return '';\r
21         }\r
22         if (typeof source[0] !== 'number' || typeof source[1] !== 'number') {\r
23                 return '';\r
24         }\r
25 \r
26         // normalize source array due to changes in node v5.4+\r
27         if (source[1] < 0) {\r
28                 totalSeconds = source[0] + source[1] / 1e9;\r
29                 source[0] = parseInt(totalSeconds);\r
30                 source[1] = parseFloat((totalSeconds % 1).toPrecision(9)) * 1e9;\r
31         }\r
32 \r
33         results = '';\r
34 \r
35         // foreach unit\r
36         for (i = 0; i < 6; i++) {\r
37                 spot = i < 3 ? 0 : 1; // grabbing first or second spot in source array\r
38                 sourceAtStep = source[spot];\r
39                 if (i !== 3 && i !== 0) {\r
40                         sourceAtStep = sourceAtStep % convert[i-1]; // trim off previous portions\r
41                 }\r
42                 if (i === 2) {\r
43                         sourceAtStep += source[1]/1e9; // get partial seconds from other portion of the array\r
44                 }\r
45                 valAtStep = sourceAtStep / convert[i]; // val at this unit\r
46                 if (valAtStep >= 1) {\r
47                         if (verbose) {\r
48                                 valAtStep = Math.floor(valAtStep); // deal in whole units, subsequent laps will get the decimal portion\r
49                         }\r
50                         if (!precise) {\r
51                                 // don't fling too many decimals\r
52                                 decimals = valAtStep >= 10 ? 0 : 2;\r
53                                 strAtStep = valAtStep.toFixed(decimals);\r
54                         } else {\r
55                                 strAtStep = valAtStep.toString();\r
56                         }\r
57                         if (strAtStep.indexOf('.') > -1 && strAtStep[strAtStep.length-1] === '0') {\r
58                                 strAtStep = strAtStep.replace(/\.?0+$/,''); // remove trailing zeros\r
59                         }\r
60                         if (results) {\r
61                                 results += ' '; // append space if we have a previous value\r
62                         }\r
63                         results += strAtStep; // append the value\r
64                         // append units\r
65                         if (verbose) {\r
66                                 results += ' '+verboseDesc[i];\r
67                                 if (strAtStep !== '1') {\r
68                                         results += 's';\r
69                                 }\r
70                         } else {\r
71                                 results += ' '+minimalDesc[i];\r
72                         }\r
73                         if (!verbose) {\r
74                                 break; // verbose gets as many groups as necessary, the rest get only one\r
75                         }\r
76                 }\r
77         }\r
78 \r
79         return results;\r
80 };\r