1 /*jshint node:true */
\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
9 module.exports = function (source, opts) {
\r
10 var verbose, precise, i, spot, sourceAtStep, valAtStep, decimals, strAtStep, results, totalSeconds;
\r
15 verbose = opts.verbose || false;
\r
16 precise = opts.precise || false;
\r
19 if (!Array.isArray(source) || source.length !== 2) {
\r
22 if (typeof source[0] !== 'number' || typeof source[1] !== 'number') {
\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
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
43 sourceAtStep += source[1]/1e9; // get partial seconds from other portion of the array
\r
45 valAtStep = sourceAtStep / convert[i]; // val at this unit
\r
46 if (valAtStep >= 1) {
\r
48 valAtStep = Math.floor(valAtStep); // deal in whole units, subsequent laps will get the decimal portion
\r
51 // don't fling too many decimals
\r
52 decimals = valAtStep >= 10 ? 0 : 2;
\r
53 strAtStep = valAtStep.toFixed(decimals);
\r
55 strAtStep = valAtStep.toString();
\r
57 if (strAtStep.indexOf('.') > -1 && strAtStep[strAtStep.length-1] === '0') {
\r
58 strAtStep = strAtStep.replace(/\.?0+$/,''); // remove trailing zeros
\r
61 results += ' '; // append space if we have a previous value
\r
63 results += strAtStep; // append the value
\r
66 results += ' '+verboseDesc[i];
\r
67 if (strAtStep !== '1') {
\r
71 results += ' '+minimalDesc[i];
\r
74 break; // verbose gets as many groups as necessary, the rest get only one
\r