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