UNPKG

@webilix/nestjs-helper

Version:

Helper library for NestJS

62 lines 3.17 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NumberFilterPipe = void 0; const common_1 = require("@nestjs/common"); const helper_library_1 = require("@webilix/helper-library"); const errors_1 = require("../../errors"); const typeList = ['EQUAL', 'GREATER', 'LESS', 'BETWEEN']; let NumberFilterPipe = class NumberFilterPipe { constructor(title, options) { this.title = title; this.options = options; this.isNumeric = (value) => { if (value.substring(0, 1) === '-') value = value.substring(1); return helper_library_1.Helper.IS.STRING.numeric(value); }; } transform(value) { var _a; if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.optional) && helper_library_1.Helper.IS.empty(value)) return null; if (helper_library_1.Helper.IS.empty(value)) errors_1.Errors.throw(errors_1.Errors.undefined(this.title)); if (!helper_library_1.Helper.IS.string(value)) errors_1.Errors.throw(errors_1.Errors.invalid(this.title)); const index = value.lastIndexOf(':'); const query = value.substring(0, index).trim(); const type = value.substring(index + 1); if (query === '' || !typeList.includes(type)) errors_1.Errors.throw(errors_1.Errors.invalid(this.title)); switch (type) { case 'EQUAL': case 'LESS': case 'GREATER': if (!this.isNumeric(query) || isNaN(+query)) errors_1.Errors.throw(errors_1.Errors.invalid(this.title)); return { type, value: +query }; case 'BETWEEN': const [from, to] = query.split(':', 2).map((q) => q.trim()); if (!from || !this.isNumeric(from) || isNaN(+from) || !to || !this.isNumeric(to) || isNaN(+to)) errors_1.Errors.throw(errors_1.Errors.invalid(this.title)); if (+from >= +to) errors_1.Errors.throw(errors_1.Errors.invalid(this.title)); return { type, from: +from, to: +to }; } } }; exports.NumberFilterPipe = NumberFilterPipe; exports.NumberFilterPipe = NumberFilterPipe = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [String, Object]) ], NumberFilterPipe); //# sourceMappingURL=number.pipe.js.map