UNPKG

javascript-time-ago

Version:

Localized relative date/time formatting

173 lines (167 loc) 6.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.INFINITY = void 0; exports["default"] = getTimeToNextUpdate; exports.getStepChangesAt = getStepChangesAt; exports.getTimeToStepChange = getTimeToStepChange; var _getTimeToNextUpdateForUnit2 = _interopRequireDefault(require("./getTimeToNextUpdateForUnit.js")); var _getStepMinTime = _interopRequireDefault(require("./getStepMinTime.js")); var _round = require("../round.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } // A thousand years is practically a metaphor for "infinity". var YEAR = 365 * 24 * 60 * 60 * 1000; // It can't really return `Infinity` in order to not screw any potential calculations performed with it. // Hence, it emulates `Infinity` with a really large number that is "practically infinite". var INFINITY = exports.INFINITY = 1000 * YEAR; /** * Returns the time (in milliseconds) after which the formatted date label should be refreshed. * @param {number} date — The date passed to `.format()`, converted to a timestamp. * @param {object} step * @param {object} [options.previousStep] * @param {object} [options.nextStep] * @param {number} options.now * @param {boolean} options.future * @param {string} [options.round] - (undocumented) Rounding mechanism. * @return {number} [timeToNextUpdate] Returns the time to next update. Returns `undefined` when it can't determine time to next update. That could only happen when the "style" doesn't meet the minimum requirements for `getTimeToNextUpdate: true` feature (see README). */ function getTimeToNextUpdate(date, step, _ref) { var prevStep = _ref.prevStep, nextStep = _ref.nextStep, now = _ref.now, future = _ref.future, round = _ref.round; var timestamp = date.getTime ? date.getTime() : date; var getTimeToNextUpdateForUnit = function getTimeToNextUpdateForUnit(unit) { return (0, _getTimeToNextUpdateForUnit2["default"])(unit, timestamp, { now: now, round: round }); }; // For future dates, steps move from the last one to the first one, // while for past dates, steps move from the first one to the last one, // due to the fact that time flows in one direction, // and future dates' interval naturally becomes smaller // while past dates' interval naturally grows larger. // // For future dates, it's the transition // from the current step to the previous step, // therefore check the `minTime` of the current step. // // For past dates, it's the transition // from the current step to the next step, // therefore check the `minTime` of the next step. // var timeToStepChange = getTimeToStepChange(future ? step : nextStep, timestamp, { future: future, now: now, round: round, prevStep: future ? prevStep : step // isFirstStep: future && isFirstStep }); // If the "style" doesn't meet the minimum requirements for `getTimeToNextUpdate: true` feature (see README) // then `timeToStepChange` will be `undefined`. In that case, just return `undefined`. if (timeToStepChange === undefined) { // Can't reliably determine "time to next update" // if not all of the steps provide `minTime`. return; } var timeToNextUpdate; if (step) { if (step.getTimeToNextUpdate) { timeToNextUpdate = step.getTimeToNextUpdate(timestamp, { getTimeToNextUpdateForUnit: getTimeToNextUpdateForUnit, getRoundFunction: _round.getRoundFunction, now: now, future: future, round: round }); } if (timeToNextUpdate === undefined) { // "unit" is now called "formatAs". var unit = step.unit || step.formatAs; if (unit) { // For some units, like "now", there's no defined amount of seconds in them. // In such cases, `getTimeToNextUpdateForUnit()` returns `undefined`, // and the next step's `minTime` could be used to calculate the update interval: // it will just assume that the label never changes for this step. timeToNextUpdate = getTimeToNextUpdateForUnit(unit); } } } if (timeToNextUpdate === undefined) { return timeToStepChange; } return Math.min(timeToNextUpdate, timeToStepChange); } // Returns the timestamp at which it'll move to the next step. // Returns `undefined` when it can't determine when it'll move to the next stemp. That could only happen when the "style" doesn't meet the minimum requirements for `getTimeToNextUpdate: true` feature (see README). function getStepChangesAt(currentOrNextStep, timestamp, _ref2) { var now = _ref2.now, future = _ref2.future, round = _ref2.round, prevStep = _ref2.prevStep; // The first step's `minTime` is `0` by default. // It doesn't "change" steps at zero point // but it does change the wording when switching // from "future" to "past": "in ..." -> "... ago". // Therefore, the label should be updated at zero-point too. var minTime = (0, _getStepMinTime["default"])(currentOrNextStep, { timestamp: timestamp, now: now, future: future, round: round, prevStep: prevStep }); if (minTime === undefined) { return; } if (future) { // The step changes to the previous step // as soon as `timestamp - now` becomes // less than the `minTime` of the current step: // `timestamp - now === minTime - 1` // => `now === timestamp - minTime + 1`. return timestamp - minTime * 1000 + 1; } else { // The step changes to the next step // as soon as `now - timestamp` becomes // equal to `minTime` of the next step: // `now - timestamp === minTime` // => `now === timestamp + minTime`. // This is a special case when double-update could be skipped. if (minTime === 0 && timestamp === now) { return INFINITY; } return timestamp + minTime * 1000; } } function getTimeToStepChange(step, timestamp, _ref3) { var now = _ref3.now, future = _ref3.future, round = _ref3.round, prevStep = _ref3.prevStep; if (step) { var stepChangesAt = getStepChangesAt(step, timestamp, { now: now, future: future, round: round, prevStep: prevStep }); if (stepChangesAt === undefined) { return; } return stepChangesAt - now; } else { if (future) { // No step. // Update right after zero point, when it changes from "future" to "past". return timestamp - now + 1; } else { // The last step doesn't ever change when `date` is in the past. return INFINITY; } } } //# sourceMappingURL=getTimeToNextUpdate.js.map