aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/shared/misc/number-formatter.pipe.ts
blob: 8a0756a36adfe342e4691400aec705681205c240 (plain) (tree)
1
2
3
4
5



                                                                                    
                                    













                                                                                                          
import { Pipe, PipeTransform } from '@angular/core'

// Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts

@Pipe({ name: 'myNumberFormatter' })
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}`
  }
}