UNPKG

1.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParseFloatPipe = void 0;
4const tslib_1 = require("tslib");
5const index_1 = require("../index");
6const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
7/**
8 * Defines the built-in ParseFloat Pipe
9 *
10 * @see [Built-in Pipes](https://docs.nestjs.com/pipes#built-in-pipes)
11 *
12 * @publicApi
13 */
14let ParseFloatPipe = class ParseFloatPipe {
15 constructor(options) {
16 options = options || {};
17 const { exceptionFactory, errorHttpStatusCode = index_1.HttpStatus.BAD_REQUEST } = options;
18 this.exceptionFactory =
19 exceptionFactory ||
20 (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
21 }
22 /**
23 * Method that accesses and performs optional transformation on argument for
24 * in-flight requests.
25 *
26 * @param value currently processed route argument
27 * @param metadata contains metadata about the currently processed route argument
28 */
29 async transform(value, metadata) {
30 if (!this.isNumeric(value)) {
31 throw this.exceptionFactory('Validation failed (numeric string is expected)');
32 }
33 return parseFloat(value);
34 }
35 /**
36 * @param value currently processed route argument
37 * @returns `true` if `value` is a valid float number
38 */
39 isNumeric(value) {
40 return (['string', 'number'].includes(typeof value) &&
41 !isNaN(parseFloat(value)) &&
42 isFinite(value));
43 }
44};
45ParseFloatPipe = tslib_1.__decorate([
46 (0, index_1.Injectable)(),
47 tslib_1.__param(0, (0, index_1.Optional)()),
48 tslib_1.__metadata("design:paramtypes", [Object])
49], ParseFloatPipe);
50exports.ParseFloatPipe = ParseFloatPipe;