All files / src/app/pipes number-suffix.pipe.ts

63.63% Statements 14/22
58.33% Branches 14/24
100% Functions 3/3
65% Lines 13/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46            145x       430x 430x 335x     335x       95x 95x 95x   95x                       95x 95x 95x           111x    
import { Pipe, PipeTransform } from '@angular/core';
 
@Pipe({
  name: 'numberSuffix',
  standalone: true
})
export class NumberSuffixPipe implements PipeTransform {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  transform(input: any, args?: number): string {
    let exp;
    const suffixes = ['K', 'M', 'G', 'T', 'P', 'E'];
    if (Number.isNaN(input) || (input < 1000 && input >= 0) || !this.isNumeric(input) || (input < 0 && input > -1000)) {
      Iif (!!args && this.isNumeric(input) && !(input < 0) && input != 0) {
        return input.toFixed(args);
      } else {
        return input.toString();
      }
    }
 
    const isNagtiveValues = input < 0;
    Eif (!isNagtiveValues) {
      exp = Math.floor(Math.log(input) / Math.log(1000));
 
      return (input / Math.pow(1000, exp)).toFixed(args) + suffixes[exp - 1];
    } else {
      input = input * -1;
 
      exp = Math.floor(Math.log(input) / Math.log(1000));
 
      return (input * -1 / Math.pow(1000, exp)).toFixed(args) + suffixes[exp - 1];
    }
 
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  isNumeric(value:any): boolean {
    Iif (value < 0) value = value * -1;
    Eif (/^-{0,1}\d+$/.test(value)) {
      return true;
    } else if (/^\d+\.\d+$/.test(value)) {
      return true;
    } else {
      return false;
    }
  }
}