UNPKG

@boomerang-io/utils

Version:

A library of reusable utilities and hooks for React webapps on the Boomerang platform.

51 lines (44 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; // Note: only accurate (to human relevant precision) for up to one day b/c variable length of months and years - damn earth and gregorian calendar /** * Return humanized string for duration * @param {number} secondsParam - number of seconds to convert to human-readable string * @param {number} numOfTimeframesParam - number of timeframes to include in returned value * @example * getHumanizedDuration(3665, 3) -> 1 hour 10 minutes 5 seconds * getHumanizedDuration(3665, 2) -> 1 hour 10 minutes */ function _default() { var secondsParam = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var numOfTimeframesParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; if (isNaN(parseInt(secondsParam, 10))) { throw TypeError("'seconds' argument must be a integer parseable value. Value passed: ".concat(secondsParam)); } if (isNaN(parseInt(numOfTimeframesParam, 10))) { throw TypeError("'numOfTimeframes' argument must be a integer parseable value. Value passed: ".concat(numOfTimeframesParam)); } var seconds = parseInt(secondsParam, 10); var numOfTimeframes = parseInt(numOfTimeframesParam, 10); var EPOCHS = ["year", "month", "day", "hour", "minute", "second"]; var DURATION_IN_SECONDS = { year: 31536000, month: 2592000, day: 86400, hour: 3600, minute: 60, second: 1 }; var timeframeStrings = []; EPOCHS.forEach(function (epoch, index) { var interval = index === 0 ? Math.floor(seconds / DURATION_IN_SECONDS[epoch]) // don't use modulus for first epoch : Math.floor(seconds % DURATION_IN_SECONDS[EPOCHS[index - 1]] / DURATION_IN_SECONDS[epoch]); // use remainder from previous epoch as numerator if (interval >= 1 && timeframeStrings.length < numOfTimeframes) { timeframeStrings.push(interval === 1 ? "1 ".concat(epoch) : "".concat(interval, " ").concat(epoch, "s")); } }); return timeframeStrings.join(" "); }