diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-04-12 11:10:44 +0200 |
---|---|---|
committer | Rigel Kent <sendmemail@rigelk.eu> | 2020-04-12 11:10:44 +0200 |
commit | 66be5213333702a1182e6fef73aa15cb45eb04ee (patch) | |
tree | 65b2794859eb2962e5d6b7f43b808c4bffa67aa8 /client/src/app/shared | |
parent | 1242fd55f9617cf22fa7568e2b0c9b2050045629 (diff) | |
download | PeerTube-66be5213333702a1182e6fef73aa15cb45eb04ee.tar.gz PeerTube-66be5213333702a1182e6fef73aa15cb45eb04ee.tar.zst PeerTube-66be5213333702a1182e6fef73aa15cb45eb04ee.zip |
Revert 41a94d0, keep the fix for years
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/angular/from-now.pipe.ts | 93 |
1 files changed, 23 insertions, 70 deletions
diff --git a/client/src/app/shared/angular/from-now.pipe.ts b/client/src/app/shared/angular/from-now.pipe.ts index 61a3c498d..07f114ab6 100644 --- a/client/src/app/shared/angular/from-now.pipe.ts +++ b/client/src/app/shared/angular/from-now.pipe.ts | |||
@@ -1,6 +1,5 @@ | |||
1 | import { Pipe, PipeTransform } from '@angular/core' | 1 | import { Pipe, PipeTransform } from '@angular/core' |
2 | import { I18n } from '@ngx-translate/i18n-polyfill' | 2 | import { I18n } from '@ngx-translate/i18n-polyfill' |
3 | import { findIndex } from 'lodash-es' | ||
4 | 3 | ||
5 | // Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site | 4 | // Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site |
6 | @Pipe({ name: 'myFromNow' }) | 5 | @Pipe({ name: 'myFromNow' }) |
@@ -8,79 +7,33 @@ export class FromNowPipe implements PipeTransform { | |||
8 | 7 | ||
9 | constructor (private i18n: I18n) { } | 8 | constructor (private i18n: I18n) { } |
10 | 9 | ||
11 | transform (arg: number | Date | string, short = true) { | 10 | transform (arg: number | Date | string) { |
12 | const argDate = new Date(arg) | 11 | const argDate = new Date(arg) |
13 | const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000) | 12 | const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000) |
14 | let intervals = [ | 13 | |
15 | { | 14 | let interval = Math.floor(seconds / 31536000) |
16 | unit: 31536000, // 1 year | 15 | if (interval > 1) return this.i18n('{{interval}} years ago', { interval }) |
17 | singular: (i: number) => this.i18n('{{i}} year', { i }), | 16 | if (interval === 1) return this.i18n('{{interval}} year ago', { interval }) |
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 | // 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) => ({ | ||
58 | ...i, | ||
59 | interval: index === 0 | ||
60 | ? i.interval | ||
61 | : Math.floor((seconds - array[index - 1].interval * array[index - 1].unit) / i.unit) | ||
62 | })) | ||
63 | // compute the final string from the "interval", cap it to the max value for the time unit | ||
64 | .map(i => ({ | ||
65 | ...i, | ||
66 | value: (i.interval > 1 ? i.plural : i.singular)(Math.min(i.max, i.interval)) | ||
67 | })) | ||
68 | 17 | ||
69 | // only keep the first two intervals with enough seconds to be considered | 18 | interval = Math.floor(seconds / 2592000) |
70 | const big_interval_index = findIndex(intervals, i => i.interval >= 1) | 19 | if (interval > 1) return this.i18n('{{interval}} months ago', { interval }) |
71 | intervals = intervals | 20 | if (interval === 1) return this.i18n('{{interval}} month ago', { interval }) |
72 | .slice(big_interval_index, big_interval_index + 2) | ||
73 | .filter(i => i.interval >= 1) | ||
74 | 21 | ||
75 | if (intervals.length === 0) { | 22 | interval = Math.floor(seconds / 604800) |
76 | return this.i18n('just now') | 23 | if (interval > 1) return this.i18n('{{interval}} weeks ago', { interval }) |
77 | } | 24 | if (interval === 1) return this.i18n('{{interval}} week ago', { interval }) |
78 | 25 | ||
79 | return intervals.length === 1 || short | 26 | interval = Math.floor(seconds / 86400) |
80 | ? this.i18n('{{interval}} ago', { interval: intervals[0].value }) | 27 | if (interval > 1) return this.i18n('{{interval}} days ago', { interval }) |
81 | : this.i18n('{{big_interval}} {{small_interval}} ago', { | 28 | if (interval === 1) return this.i18n('{{interval}} day ago', { interval }) |
82 | big_interval: intervals[0].value, | 29 | |
83 | small_interval: intervals[1].value | 30 | interval = Math.floor(seconds / 3600) |
84 | }) | 31 | if (interval > 1) return this.i18n('{{interval}} hours ago', { interval }) |
32 | if (interval === 1) return this.i18n('{{interval}} hour ago', { interval }) | ||
33 | |||
34 | interval = Math.floor(seconds / 60) | ||
35 | if (interval >= 1) return this.i18n('{{interval}} min ago', { interval }) | ||
36 | |||
37 | return this.i18n('just now') | ||
85 | } | 38 | } |
86 | } | 39 | } |