javascript-time-ago
Version:
Localized relative date/time formatting
160 lines (130 loc) • 5.62 kB
JavaScript
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import getStepDenominator from './getStepDenominator';
import getStepMinTime from './getStepMinTime';
import { getRoundFunction } from '../round';
/**
* Finds an appropriate `step` of `steps` for the time interval (in seconds).
*
* @param {Object[]} steps - Time formatting steps.
*
* @param {number} secondsPassed - Time interval (in seconds).
* `< 0` for past dates and `> 0` for future dates.
*
* @param {number} options.now - Current timestamp.
*
* @param {boolean} [options.future] - Whether the date should be formatted as a future one
* instead of a past one.
*
* @param {string} [options.round] - (undocumented) Rounding mechanism.
*
* @param {string[]} [options.units] - A list of allowed time units.
* (Example: ['second', 'minute', 'hour', …])
*
* @param {boolean} [options.getNextStep] - Pass true to return `[step, nextStep]` instead of just `step`.
*
* @return {Object|Object[]} [step] — Either a `step` or `[prevStep, step, nextStep]`.
*/
export default function getStep(steps, secondsPassed, _ref) {
var now = _ref.now,
future = _ref.future,
round = _ref.round,
units = _ref.units,
getNextStep = _ref.getNextStep;
// Ignore steps having not-supported time units in `formatAs`.
steps = filterStepsByUnits(steps, units);
var step = _getStep(steps, secondsPassed, {
now: now,
future: future,
round: round
});
if (getNextStep) {
if (step) {
var prevStep = steps[steps.indexOf(step) - 1];
var nextStep = steps[steps.indexOf(step) + 1];
return [prevStep, step, nextStep];
}
return [undefined, undefined, steps[0]];
}
return step;
}
function _getStep(steps, secondsPassed, _ref2) {
var now = _ref2.now,
future = _ref2.future,
round = _ref2.round;
// If no steps fit the conditions then return nothing.
if (steps.length === 0) {
return;
} // Find the most appropriate step.
var i = getStepIndex(steps, secondsPassed, {
now: now,
future: future || secondsPassed < 0,
round: round
}); // If no step is applicable the return nothing.
if (i === -1) {
return;
}
var step = steps[i]; // Apply granularity to the time amount
// (and fall back to the previous step
// if the first level of granularity
// isn't met by this amount)
if (step.granularity) {
// Recalculate the amount of seconds passed based on `granularity`.
var secondsPassedGranular = getRoundFunction(round)(Math.abs(secondsPassed) / getStepDenominator(step) / step.granularity) * step.granularity; // If the granularity for this step is too high,
// then fall back to the previous step.
// (if there is any previous step)
if (secondsPassedGranular === 0 && i > 0) {
return steps[i - 1];
}
}
return step;
}
/**
* Iterates through steps until it finds the maximum one satisfying the `minTime` threshold.
* @param {Object} steps - Steps.
* @param {number} secondsPassed - How much seconds have passed since the date till `now`.
* @param {number} options.now - Current timestamp.
* @param {boolean} options.future - Whether the time interval should be formatted as a future one.
* @param {number} [i] - Gradation step currently being tested.
* @return {number} Gradation step index.
*/
function getStepIndex(steps, secondsPassed, options) {
var i = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
var minTime = getStepMinTime(steps[i], _objectSpread({
prevStep: steps[i - 1],
timestamp: options.now - secondsPassed * 1000
}, options)); // If `minTime` isn't defined or deduceable for this step, then stop.
if (minTime === undefined) {
return i - 1;
} // If the `minTime` threshold for moving from previous step
// to this step is too high then return the previous step.
if (Math.abs(secondsPassed) < minTime) {
return i - 1;
} // If it's the last step then return it.
if (i === steps.length - 1) {
return i;
} // Move to the next step.
return getStepIndex(steps, secondsPassed, options, i + 1);
}
/**
* Leaves only allowed steps.
* @param {Object[]} steps
* @param {string[]} units - Allowed time units.
* @return {Object[]}
*/
function filterStepsByUnits(steps, units) {
return steps.filter(function (_ref3) {
var unit = _ref3.unit,
formatAs = _ref3.formatAs;
// "unit" is now called "formatAs".
unit = unit || formatAs; // If this step has a `unit` defined
// then this `unit` must be in the list of allowed `units`.
if (unit) {
return units.indexOf(unit) >= 0;
} // A step is not required to specify a `unit`:
// alternatively, it could specify `format()`.
// (see "twitter" style for an example)
return true;
});
}
//# sourceMappingURL=getStep.js.map