Reorganize client shared modules
[oweals/peertube.git] / client / src / app / shared / shared-main / angular / number-formatter.pipe.ts
1 import { 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: 'myNumberFormatter' })
6 export 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 }