# All files / common/pipes parse-int.pipe.ts

100% Statements 9/9
100% Branches 5/5
100% Functions 1/1
100% Lines 7/7

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    201x

    1x

    1x

    2x

    2x
    1x

    1x

     import { BadRequestException } from '../exceptions/bad-request.exception';
    import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
    import { ArgumentMetadata, Injectable } from '../index';

    @Injectable()
    export class ParseIntPipe implements PipeTransform<string> {
      async transform(value: string, metadata: ArgumentMetadata): Promise<number> {
        const isNumeric =
          'string' === typeof value &&
          !isNaN(parseFloat(value)) &&
          isFinite(value as any);
        if (!isNumeric) {
          throw new BadRequestException(
            'Validation failed (numeric string is expected)',
          );
        }
        return parseInt(value, 10);
      }
    }