UNPKG

@webilix/nestjs-helper

Version:

Helper library for NestJS

63 lines 3.63 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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NormalizerService = void 0; const common_1 = require("@nestjs/common"); const normalizer_map_1 = require("./normalizer.map"); let NormalizerService = class NormalizerService { constructor(config) { this.config = config; } update(content, map) { Object.keys(map).forEach((key) => { const search = map[key].split(' ').filter((s) => s !== ''); const regex = new RegExp(`(${search.join('|')})`, 'gi'); content = content.replace(regex, key); }); return content; } check(config1, config2) { if (config1 === false || config2 === false) return false; return (config1 === undefined && config2 === undefined) || config1 === true || config2 === true; } normalize(text, config) { var _a, _b, _c, _d, _e, _f; // NORMALIZE if (this.check((_a = this.config) === null || _a === void 0 ? void 0 : _a.persian, config === null || config === void 0 ? void 0 : config.persian)) text = this.update(text, normalizer_map_1.NormalizerMap.persian); if (this.check((_b = this.config) === null || _b === void 0 ? void 0 : _b.english, config === null || config === void 0 ? void 0 : config.english)) text = this.update(text, normalizer_map_1.NormalizerMap.english); if (this.check((_c = this.config) === null || _c === void 0 ? void 0 : _c.arabic, config === null || config === void 0 ? void 0 : config.arabic)) text = this.update(text, normalizer_map_1.NormalizerMap.arabic); if (this.check((_d = this.config) === null || _d === void 0 ? void 0 : _d.number, config === null || config === void 0 ? void 0 : config.number)) text = this.update(text, normalizer_map_1.NormalizerMap.number); // WHITE SPACE if (this.check((_e = this.config) === null || _e === void 0 ? void 0 : _e.whitespace, config === null || config === void 0 ? void 0 : config.whitespace)) while (text.indexOf(' ') !== -1) text = text.replace(/ /g, ' '); // TRIM if (this.check((_f = this.config) === null || _f === void 0 ? void 0 : _f.trim, config === null || config === void 0 ? void 0 : config.trim)) text = text.trim(); return text; } }; exports.NormalizerService = NormalizerService; exports.NormalizerService = NormalizerService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Optional)()), __param(0, (0, common_1.Inject)('NORMALIZER_CONFIG')), __metadata("design:paramtypes", [Object]) ], NormalizerService); //# sourceMappingURL=normalizer.service.js.map