]>
Commit | Line | Data |
---|---|---|
9bf9d2a5 | 1 | import { Pipe, PipeTransform } from '@angular/core' |
b1d40cff | 2 | import { I18n } from '@ngx-translate/i18n-polyfill' |
41a94d07 | 3 | import { 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 |
7 | export class FromNowPipe implements PipeTransform { |
8 | ||
b1d40cff C |
9 | constructor (private i18n: I18n) { } |
10 | ||
1242fd55 | 11 | transform (arg: number | Date | string, short = true) { |
2fbe7f19 C |
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 | ] | |
1242fd55 RK |
51 | // compute the number of units each unit of time has, store it in "interval" |
52 | .map(i => ({ | |
53 | ...i, | |
54 | interval: Math.floor(seconds / i.unit) | |
55 | })) | |
56 | // compute the number of units each unit of time has, from the remainder of the previous bigger unit, store it in "interval" | |
57 | .map((i, index, array) => ({ | |
41a94d07 RK |
58 | ...i, |
59 | interval: index === 0 | |
60 | ? i.interval | |
61 | : Math.floor((seconds - array[index - 1].interval * array[index - 1].unit) / i.unit) | |
62 | })) | |
1242fd55 RK |
63 | // compute the final string from the "interval", cap it to the max value for the time unit |
64 | .map(i => ({ | |
41a94d07 | 65 | ...i, |
1242fd55 | 66 | value: (i.interval > 1 ? i.plural : i.singular)(Math.min(i.max, i.interval)) |
41a94d07 | 67 | })) |
9bf9d2a5 | 68 | |
41a94d07 RK |
69 | // only keep the first two intervals with enough seconds to be considered |
70 | const big_interval_index = findIndex(intervals, i => i.interval >= 1) | |
71 | intervals = intervals | |
72 | .slice(big_interval_index, big_interval_index + 2) | |
73 | .filter(i => i.interval >= 1) | |
9bf9d2a5 | 74 | |
41a94d07 RK |
75 | if (intervals.length === 0) { |
76 | return this.i18n('just now') | |
77 | } | |
9bf9d2a5 | 78 | |
1242fd55 | 79 | return intervals.length === 1 || short |
41a94d07 RK |
80 | ? this.i18n('{{interval}} ago', { interval: intervals[0].value }) |
81 | : this.i18n('{{big_interval}} {{small_interval}} ago', { | |
82 | big_interval: intervals[0].value, | |
83 | small_interval: intervals[1].value | |
84 | }) | |
9bf9d2a5 C |
85 | } |
86 | } |