diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 10:00:54 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 10:00:54 +0000 |
commit | 3cccd102ba543e02725d247893729e5c73b38295 (patch) | |
tree | f36a04ec38517f5deaaacb5acc7d949688d1e187 /app/assets/javascripts/lib/utils/datetime/timeago_utility.js | |
parent | 205943281328046ef7b4528031b90fbda70c75ac (diff) | |
download | gitlab-ce-3cccd102ba543e02725d247893729e5c73b38295.tar.gz |
Add latest changes from gitlab-org/gitlab@14-10-stable-eev14.10.0-rc42
Diffstat (limited to 'app/assets/javascripts/lib/utils/datetime/timeago_utility.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/datetime/timeago_utility.js | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/app/assets/javascripts/lib/utils/datetime/timeago_utility.js b/app/assets/javascripts/lib/utils/datetime/timeago_utility.js index d68682ebed1..095a29a2eff 100644 --- a/app/assets/javascripts/lib/utils/datetime/timeago_utility.js +++ b/app/assets/javascripts/lib/utils/datetime/timeago_utility.js @@ -1,5 +1,5 @@ import * as timeago from 'timeago.js'; -import { languageCode, s__, createDateTimeFormat } from '../../../locale'; +import { languageCode, s__, createDateTimeFormat } from '~/locale'; import { formatDate } from './date_format_utility'; /** @@ -70,8 +70,41 @@ const memoizedLocale = () => { }; }; +/** + * Registers timeago time duration + */ +const memoizedLocaleDuration = () => { + const cache = []; + + const durations = [ + () => [s__('Duration|%s seconds')], + () => [s__('Duration|%s seconds')], + () => [s__('Duration|1 minute')], + () => [s__('Duration|%s minutes')], + () => [s__('Duration|1 hour')], + () => [s__('Duration|%s hours')], + () => [s__('Duration|1 day')], + () => [s__('Duration|%s days')], + () => [s__('Duration|1 week')], + () => [s__('Duration|%s weeks')], + () => [s__('Duration|1 month')], + () => [s__('Duration|%s months')], + () => [s__('Duration|1 year')], + () => [s__('Duration|%s years')], + ]; + + return (_, index) => { + if (cache[index]) { + return cache[index]; + } + cache[index] = durations[index] && durations[index](); + return cache[index]; + }; +}; + timeago.register(timeagoLanguageCode, memoizedLocale()); timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining()); +timeago.register(`${timeagoLanguageCode}-duration`, memoizedLocaleDuration()); let memoizedFormatter = null; @@ -133,3 +166,16 @@ export const timeFor = (time, expiredLabel) => { } return timeago.format(time, `${timeagoLanguageCode}-remaining`).trim(); }; + +/** + * Returns a duration of time given an amount. + * + * @param {number} milliseconds - Duration in milliseconds. + * @returns {string} A formatted duration, e.g. "10 minutes". + */ +export const duration = (milliseconds) => { + const now = new Date(); + return timeago + .format(now.getTime() - Math.abs(milliseconds), `${timeagoLanguageCode}-duration`) + .trim(); +}; |