UNPKG

tint-js

Version:
70 lines (55 loc) 1.07 kB
/* jshint node: true */ "use strict"; var Duration = require("dur-js"); /* * expose */ exports.till = till; /* * till returns a string based *till* a time * * @param {Number} dur (millisecond time duration) * @param {Object} opts * @return {String} (eg. 3 days, 4 hours, 52 minutes, 16 seconds) * @api public */ function till(dur, opts) { if ("number" !== typeof dur) { return NaN; } opts = opts || {}; var d = new Duration(dur); var t = []; push(t, d, "days"); push(t, d, "hours"); push(t, d, "minutes"); push(t, d, "seconds"); if (!!opts.ms) { push(t, d, "milliseconds"); } if (!!opts.hideZeros) { var i = 0; var len = t.length; for(; i < len; i++) { var a = t[i]; if (a[0] !== "0") { break; } else { t.shift(); i--; } } } return t.join(", "); } /* * push pushes a duration label to arr * * @param {Array} arr * @param {Duration} v * @param {String} str * @api private */ function push(arr, d, str) { arr.push(d[str]() + " " + str); }