aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/misc
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/misc')
-rw-r--r--client/src/app/shared/misc/from-now.pipe.ts37
-rw-r--r--client/src/app/shared/misc/number-formatter.pipe.ts19
2 files changed, 56 insertions, 0 deletions
diff --git a/client/src/app/shared/misc/from-now.pipe.ts b/client/src/app/shared/misc/from-now.pipe.ts
new file mode 100644
index 000000000..25e5d6a85
--- /dev/null
+++ b/client/src/app/shared/misc/from-now.pipe.ts
@@ -0,0 +1,37 @@
1import { Pipe, PipeTransform } from '@angular/core'
2
3// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
4
5@Pipe({name: 'fromNow'})
6export class FromNowPipe implements PipeTransform {
7
8 transform (value: number) {
9 const seconds = Math.floor((Date.now() - value) / 1000)
10
11 let interval = Math.floor(seconds / 31536000)
12 if (interval > 1) {
13 return interval + ' years ago'
14 }
15
16 interval = Math.floor(seconds / 2592000)
17 if (interval > 1) return interval + ' months ago'
18 if (interval === 1) return interval + ' month ago'
19
20 interval = Math.floor(seconds / 604800)
21 if (interval > 1) return interval + ' weeks ago'
22 if (interval === 1) return interval + ' week ago'
23
24 interval = Math.floor(seconds / 86400)
25 if (interval > 1) return interval + ' days ago'
26 if (interval === 1) return interval + ' day ago'
27
28 interval = Math.floor(seconds / 3600)
29 if (interval > 1) return interval + ' hours ago'
30 if (interval === 1) return interval + ' hour ago'
31
32 interval = Math.floor(seconds / 60)
33 if (interval >= 1) return interval + ' min ago'
34
35 return Math.floor(seconds) + ' sec ago'
36 }
37}
diff --git a/client/src/app/shared/misc/number-formatter.pipe.ts b/client/src/app/shared/misc/number-formatter.pipe.ts
new file mode 100644
index 000000000..2491fb1d6
--- /dev/null
+++ b/client/src/app/shared/misc/number-formatter.pipe.ts
@@ -0,0 +1,19 @@
1import { Pipe, PipeTransform } from '@angular/core'
2
3// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
4
5@Pipe({name: 'numberFormatter'})
6export class NumberFormatterPipe implements PipeTransform {
7 private dictionary: Array<{max: number, type: string}> = [
8 { max: 1000, type: '' },
9 { max: 1000000, type: 'K' },
10 { max: 1000000000, type: 'M' }
11 ]
12
13 transform (value: number) {
14 const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]
15 const calc = Math.floor(value / (format.max / 1000))
16
17 return `${calc}${format.type}`
18 }
19}