aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/angular/from-now.pipe.ts
blob: 61a3c498db1187c00c1f8feac2c39e0286e240a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Pipe, PipeTransform } from '@angular/core'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { findIndex } from 'lodash-es'

// Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site
@Pipe({ name: 'myFromNow' })
export class FromNowPipe implements PipeTransform {

  constructor (private i18n: I18n) { }

  transform (arg: number | Date | string, short = true) {
    const argDate = new Date(arg)
    const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000)
    let intervals = [
      {
        unit: 31536000, // 1 year
        singular: (i: number) => this.i18n('{{i}} year', { i }),
        plural: (i: number) => this.i18n('{{i}} years', { i })
      },
      {
        unit: 2592000, // 1 month
        max: 11,
        singular: (i: number) => this.i18n('{{i}} month', { i }),
        plural: (i: number) => this.i18n('{{i}} months', { i })
      },
      {
        unit: 604800, // 1 week
        max: 3,
        singular: (i: number) => this.i18n('{{i}} week', { i }),
        plural: (i: number) => this.i18n('{{i}} weeks', { i })
      },
      {
        unit: 86400, // 1 day
        max: 6,
        singular: (i: number) => this.i18n('{{i}} day', { i }),
        plural: (i: number) => this.i18n('{{i}} days', { i })
      },
      {
        unit: 3600, // 1 hour
        max: 23,
        singular: (i: number) => this.i18n('{{i}} hour', { i }),
        plural: (i: number) => this.i18n('{{i}} hours', { i })
      },
      {
        unit: 60, // 1 min
        max: 59,
        singular: (i: number) => this.i18n('{{i}} min', { i }),
        plural: (i: number) => this.i18n('{{i}} min', { i })
      }
    ]
      // compute the number of units each unit of time has, store it in "interval"
      .map(i => ({
        ...i,
        interval: Math.floor(seconds / i.unit)
      }))
      // compute the number of units each unit of time has, from the remainder of the previous bigger unit, store it in "interval"
      .map((i, index, array) => ({
        ...i,
        interval: index === 0
          ? i.interval
          : Math.floor((seconds - array[index - 1].interval * array[index - 1].unit) / i.unit)
      }))
      // compute the final string from the "interval", cap it to the max value for the time unit
      .map(i => ({
        ...i,
        value: (i.interval > 1 ? i.plural : i.singular)(Math.min(i.max, i.interval))
      }))

    // only keep the first two intervals with enough seconds to be considered
    const big_interval_index = findIndex(intervals, i => i.interval >= 1)
    intervals = intervals
      .slice(big_interval_index, big_interval_index + 2)
      .filter(i => i.interval >= 1)

    if (intervals.length === 0) {
      return this.i18n('just now')
    }

    return intervals.length === 1 || short
      ? this.i18n('{{interval}} ago', { interval: intervals[0].value })
      : this.i18n('{{big_interval}} {{small_interval}} ago', { 
          big_interval: intervals[0].value,
          small_interval: intervals[1].value
        })
  }
}