PeerTube/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts

20 lines
644 B
TypeScript
Raw Normal View History

2017-12-01 14:46:22 +01:00
import { Pipe, PipeTransform } from '@angular/core'
2020-08-11 16:50:00 +02:00
// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts
2017-12-01 14:46:22 +01:00
2017-12-04 11:17:08 +01:00
@Pipe({ name: 'myNumberFormatter' })
2017-12-01 14:46:22 +01:00
export class NumberFormatterPipe implements PipeTransform {
private dictionary: Array<{max: number, type: string}> = [
{ max: 1000, type: '' },
{ max: 1000000, type: 'K' },
{ max: 1000000000, type: 'M' }
]
transform (value: number) {
const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]
const calc = Math.floor(value / (format.max / 1000))
return `${calc}${format.type}`
}
}