]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/angular/from-now.pipe.ts
Refactor from-now pipe to display the two most significant intervals
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / angular / from-now.pipe.ts
CommitLineData
9bf9d2a5 1import { Pipe, PipeTransform } from '@angular/core'
b1d40cff 2import { I18n } from '@ngx-translate/i18n-polyfill'
41a94d07 3import { findIndex } from 'lodash-es'
9bf9d2a5 4
2bbb3412 5// Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site
be447678 6@Pipe({ name: 'myFromNow' })
9bf9d2a5
C
7export class FromNowPipe implements PipeTransform {
8
b1d40cff
C
9 constructor (private i18n: I18n) { }
10
2fbe7f19
C
11 transform (arg: number | Date | string) {
12 const argDate = new Date(arg)
13 const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000)
41a94d07
RK
14 let intervals = [
15 {
16 unit: 31536000, // 1 year
17 singular: (i: number) => this.i18n('{{i}} year', { i }),
18 plural: (i: number) => this.i18n('{{i}} years', { i })
19 },
20 {
21 unit: 2592000, // 1 month
22 max: 11,
23 singular: (i: number) => this.i18n('{{i}} month', { i }),
24 plural: (i: number) => this.i18n('{{i}} months', { i })
25 },
26 {
27 unit: 604800, // 1 week
28 max: 3,
29 singular: (i: number) => this.i18n('{{i}} week', { i }),
30 plural: (i: number) => this.i18n('{{i}} weeks', { i })
31 },
32 {
33 unit: 86400, // 1 day
34 max: 6,
35 singular: (i: number) => this.i18n('{{i}} day', { i }),
36 plural: (i: number) => this.i18n('{{i}} days', { i })
37 },
38 {
39 unit: 3600, // 1 hour
40 max: 23,
41 singular: (i: number) => this.i18n('{{i}} hour', { i }),
42 plural: (i: number) => this.i18n('{{i}} hours', { i })
43 },
44 {
45 unit: 60, // 1 min
46 max: 59,
47 singular: (i: number) => this.i18n('{{i}} min', { i }),
48 plural: (i: number) => this.i18n('{{i}} min', { i })
49 }
50 ]
51 .map(i => ({ ...i, interval: Math.floor(seconds / i.unit) })) // absolute interval
52 .map((i, index, array) => ({ // interval relative to remainder
53 ...i,
54 interval: index === 0
55 ? i.interval
56 : Math.floor((seconds - array[index - 1].interval * array[index - 1].unit) / i.unit)
57 }))
58 .map(i => ({ // value, interval put in its translated text wrt max value
59 ...i,
60 value: (i.interval > 1
61 ? i.plural
62 : i.singular
63 )(Math.min(i.max, i.interval)) // respect the max value
64 }))
9bf9d2a5 65
41a94d07
RK
66 // only keep the first two intervals with enough seconds to be considered
67 const big_interval_index = findIndex(intervals, i => i.interval >= 1)
68 intervals = intervals
69 .slice(big_interval_index, big_interval_index + 2)
70 .filter(i => i.interval >= 1)
9bf9d2a5 71
41a94d07
RK
72 if (intervals.length === 0) {
73 return this.i18n('just now')
74 }
9bf9d2a5 75
41a94d07
RK
76 return intervals.length == 1
77 ? this.i18n('{{interval}} ago', { interval: intervals[0].value })
78 : this.i18n('{{big_interval}} {{small_interval}} ago', {
79 big_interval: intervals[0].value,
80 small_interval: intervals[1].value
81 })
9bf9d2a5
C
82 }
83}