1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ParseFloatPipe = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const index_1 = require("../index");
|
6 | const http_error_by_code_util_1 = require("../utils/http-error-by-code.util");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | let 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 |
|
24 |
|
25 |
|
26 |
|
27 |
|
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 |
|
37 |
|
38 |
|
39 | isNumeric(value) {
|
40 | return (['string', 'number'].includes(typeof value) &&
|
41 | !isNaN(parseFloat(value)) &&
|
42 | isFinite(value));
|
43 | }
|
44 | };
|
45 | ParseFloatPipe = 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);
|
50 | exports.ParseFloatPipe = ParseFloatPipe;
|