1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParseFloatPipe = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const core_1 = require("../decorators/core");
|
6 | const index_1 = require("../index");
|
7 | const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
|
8 | const shared_utils_1 = require("../utils/shared.utils");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | let ParseFloatPipe = class ParseFloatPipe {
|
17 | constructor(options) {
|
18 | this.options = options;
|
19 | options = options || {};
|
20 | const { exceptionFactory, errorHttpStatusCode = index_1.HttpStatus.BAD_REQUEST } = options;
|
21 | this.exceptionFactory =
|
22 | exceptionFactory ||
|
23 | (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | async transform(value, metadata) {
|
33 | if ((0, shared_utils_1.isNil)(value) && this.options?.optional) {
|
34 | return value;
|
35 | }
|
36 | if (!this.isNumeric(value)) {
|
37 | throw this.exceptionFactory('Validation failed (numeric string is expected)');
|
38 | }
|
39 | return parseFloat(value);
|
40 | }
|
41 | |
42 |
|
43 |
|
44 |
|
45 | isNumeric(value) {
|
46 | return (['string', 'number'].includes(typeof value) &&
|
47 | !isNaN(parseFloat(value)) &&
|
48 | isFinite(value));
|
49 | }
|
50 | };
|
51 | exports.ParseFloatPipe = ParseFloatPipe;
|
52 | exports.ParseFloatPipe = ParseFloatPipe = tslib_1.__decorate([
|
53 | (0, core_1.Injectable)(),
|
54 | tslib_1.__param(0, (0, core_1.Optional)()),
|
55 | tslib_1.__metadata("design:paramtypes", [Object])
|
56 | ], ParseFloatPipe);
|