Commit 6401db89 authored by jakeburden's avatar jakeburden

Update usage of timeago.js

parent 21cb540a
import $ from 'jquery';
import _ from 'underscore';
import timeago from 'timeago.js';
import * as timeago from 'timeago.js';
import dateFormat from 'dateformat';
import { languageCode, s__, __, n__ } from '../../locale';
......@@ -92,14 +92,10 @@ export const formatDate = (datetime, format = 'mmm d, yyyy h:MMtt Z') => {
*/
const timeagoLanguageCode = languageCode().replace(/-/g, '_');
let timeagoInstance;
/**
* Sets a timeago Instance
* Registers timeago locales
*/
export const getTimeago = () => {
if (!timeagoInstance) {
const memoizedLocaleRemaining = () => {
const memoizedLocaleRemaining = () => {
const cache = [];
const timeAgoLocaleRemaining = [
......@@ -126,9 +122,9 @@ export const getTimeago = () => {
cache[index] = timeAgoLocaleRemaining[index] && timeAgoLocaleRemaining[index]();
return cache[index];
};
};
};
const memoizedLocale = () => {
const memoizedLocale = () => {
const cache = [];
const timeAgoLocale = [
......@@ -155,27 +151,20 @@ export const getTimeago = () => {
cache[index] = timeAgoLocale[index] && timeAgoLocale[index]();
return cache[index];
};
};
timeago.register(timeagoLanguageCode, memoizedLocale());
timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining());
timeagoInstance = timeago();
}
return timeagoInstance;
};
timeago.register(timeagoLanguageCode, memoizedLocale());
timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining());
/**
* For the given elements, sets a tooltip with a formatted date.
* @param {JQuery} $timeagoEls
* @param {Boolean} setTimeago
*/
export const localTimeAgo = ($timeagoEls, setTimeago = true) => {
getTimeago();
$timeagoEls.each((i, el) => {
$(el).text(timeagoInstance.format($(el).attr('datetime'), timeagoLanguageCode));
$(el).text(timeago.format($(el).attr('datetime'), timeagoLanguageCode));
});
if (!setTimeago) {
......@@ -207,7 +196,7 @@ export const timeFor = (time, expiredLabel) => {
if (new Date(time) < new Date()) {
return expiredLabel || s__('Timeago|Past due');
}
return getTimeago()
return timeago
.format(time, `${timeagoLanguageCode}-remaining`)
.trim();
};
......@@ -417,7 +406,7 @@ export const dayInQuarter = (date, quarter) =>
window.gl = window.gl || {};
window.gl.utils = {
...(window.gl.utils || {}),
getTimeago,
timeago,
localTimeAgo,
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment