balena-sdk
Version:
The Balena JavaScript SDK
22 lines (21 loc) • 918 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeSince = void 0;
const tslib_1 = require("tslib");
const formatDistance_1 = require("date-fns/formatDistance");
const memoizee_1 = tslib_1.__importDefault(require("memoizee"));
const now = (0, memoizee_1.default)(() => new Date(), { maxAge: 1000 });
const dateToMoment = (0, memoizee_1.default)((date) => new Date(date), {
max: 1000,
primitive: true,
});
const timeSince = (input, suffix = true) => {
const date = dateToMoment(input);
// We do this to avoid out-of-sync times causing this to return
// e.g. 'in a few seconds'.
// if the date is in the future, make it at maximum the time since now
// which results in 'a few seconds ago'.
const $now = now();
return (0, formatDistance_1.formatDistance)($now < date ? $now : date, $now, { addSuffix: suffix });
};
exports.timeSince = timeSince;