From 6a7774f3b62ce63c69f860d9702dc88eeab01fed Mon Sep 17 00:00:00 2001 From: Liubov Zatsepina Date: Mon, 2 Dec 2024 10:30:02 +0200 Subject: [PATCH] Liuboff/add one more constant and remove redundant property --- .../filters-list/searchbar/searchbar.component.ts | 8 ++++---- src/app/shared/constants/regex-constants.ts | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/app/shared/components/filters-list/searchbar/searchbar.component.ts b/src/app/shared/components/filters-list/searchbar/searchbar.component.ts index 0d5042423..0973cce52 100644 --- a/src/app/shared/components/filters-list/searchbar/searchbar.component.ts +++ b/src/app/shared/components/filters-list/searchbar/searchbar.component.ts @@ -10,7 +10,8 @@ import { Navigation } from 'shared/models/navigation.model'; import { SetSearchQueryValue } from 'shared/store/filter.actions'; import { FilterState } from 'shared/store/filter.state'; import { NavigationState } from 'shared/store/navigation.state'; -import { SEARCHBAR_REGEX } from 'shared/constants/regex-constants'; +import { SEARCHBAR_REGEX_VALID } from 'shared/constants/regex-constants'; +import { SEARCHBAR_REGEX_REPLACE } from 'shared/constants/regex-constants'; @Component({ selector: 'app-searchbar', @@ -25,10 +26,9 @@ export class SearchbarComponent implements OnInit, OnDestroy { private navigationPaths$: Observable; @Select(FilterState.searchQuery) private searchQuery$: Observable; - public searchRegex: RegExp = SEARCHBAR_REGEX; public filteredResults: string[]; - public searchValueFormControl = new FormControl('', [Validators.maxLength(256), Validators.pattern(this.searchRegex)]); + public searchValueFormControl = new FormControl('', [Validators.maxLength(256), Validators.pattern(SEARCHBAR_REGEX_VALID)]); private previousResults: string[] = this.getPreviousResults(); private isResultPage = false; @@ -87,7 +87,7 @@ export class SearchbarComponent implements OnInit, OnDestroy { } public handleInvalidCharacter(value: string): void { - const validValue = value?.replace(/[^A-Za-zА-Яа-яІіЇїЄєҐґ0-9`.,№"'\s]/g, ''); + const validValue = value?.replace(SEARCHBAR_REGEX_REPLACE, ''); if (validValue !== value) { this.searchValueFormControl.setValue(validValue); this.invalidCharacterDetected.emit(); diff --git a/src/app/shared/constants/regex-constants.ts b/src/app/shared/constants/regex-constants.ts index cc18d0446..37f7bc60c 100644 --- a/src/app/shared/constants/regex-constants.ts +++ b/src/app/shared/constants/regex-constants.ts @@ -37,5 +37,8 @@ export const SECTION_NAME_REGEX: RegExp = /^(?!`)(?!\^)(?!_)(?!\[)(?!])(?!\\)[А // Regex for checking if string has a letter export const MUST_CONTAIN_LETTERS: RegExp = /[a-zA-ZА-ЯЄІЇҐа-яґєії]/; -// Regex for searchbar -export const SEARCHBAR_REGEX: RegExp = /^[A-Za-zА-Яа-яІіЇїЄєҐґ0-9`.,№"'\\s]*$/; +// Regex for searchbar validation +export const SEARCHBAR_REGEX_VALID: RegExp = /^[A-Za-zА-Яа-яІіЇїЄєҐґ0-9`.,№"'\\s]*$/; + +// Regex for searchbar replace invalid characters +export const SEARCHBAR_REGEX_REPLACE: RegExp = /[^A-Za-zА-Яа-яІіЇїЄєҐґ0-9`.,№"'\s]/g;