From 1f138dae8bb7d08848440020921b84c6fb39dfae Mon Sep 17 00:00:00 2001 From: Annika Nowak Date: Tue, 15 Oct 2024 08:26:20 +0200 Subject: [PATCH] feat: move arrayHelperFunctions to lib and improvements from review --- libs/angular-accelerator/src/index.ts | 1 + ...g-and-array-helper-functions.utils.spec.ts | 40 +++++++++++++++++++ ...string-and-array-helper-functions.utils.ts | 31 ++++++++++++++ ...navigated-event-store-connector-service.ts | 4 +- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.spec.ts create mode 100644 libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.ts diff --git a/libs/angular-accelerator/src/index.ts b/libs/angular-accelerator/src/index.ts index 87ae2c0a..bfbbfbb0 100644 --- a/libs/angular-accelerator/src/index.ts +++ b/libs/angular-accelerator/src/index.ts @@ -60,3 +60,4 @@ export * from './lib/utils/create-remote-component-and-mfe-translate-loader.util export * from './lib/utils/create-remote-component-translate-loader.utils' export * from './lib/utils/enum-to-dropdown-options.utils' export * from './lib/utils/criteria.utils' +export * from './lib/utils/string-and-array-helper-functions.utils' diff --git a/libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.spec.ts b/libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.spec.ts new file mode 100644 index 00000000..6691f443 --- /dev/null +++ b/libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.spec.ts @@ -0,0 +1,40 @@ +import { + findEntryWithKeyword, + removeKeyword, + searchPrefixWithSpecialChars, +} from './string-and-array-helper-functions.utils' + +describe('findEntryWithKeyword', () => { + it('should find the entry containing the keyword', () => { + expect(findEntryWithKeyword(['entry1', 'entry2', 'keywordEntry'], 'keyword')).toBe('keywordEntry') + expect(findEntryWithKeyword(['entry1', 'entry2', 'entry3'], 'keyword')).toBe(null) + expect(findEntryWithKeyword(undefined, 'keyword')).toBe(null) + }) +}) + +describe('removeKeyword', () => { + it('should remove the keyword and trailing details from the input string', () => { + expect(removeKeyword('/ibt-tsg-mgmt-page/keyword/123', 'keyword')).toBe('/ibt-tsg-mgmt-page') + expect(removeKeyword('/ibt-tsg-mgmt-page/keyword/123/', 'keyword')).toBe('/ibt-tsg-mgmt-page') + expect(removeKeyword('/ibt-tsg-mgmt-page/search/123', 'keyword')).toBe('/ibt-tsg-mgmt-page/search/123') + }) + + describe('searchPrefixWithSpecialChars', () => { + it('should return the latest string starting with the prefix followed by ? or #', () => { + const exampleUrls1 = ['ibt-tsg-mgmt-page?id', 'ibt-tsg-mgmt-page#id', 'ibt-order-mgmt-page#id'] + const exampleUrls2 = ['ibt-tsg-mgmt-page#id', 'ibt-tsg-mgmt-page#id', 'ibt-tsg-mgmt-page?id'] + const prefix = 'ibt-tsg-mgmt-page' + const expected1 = 'ibt-tsg-mgmt-page#id' + const expected2 = 'ibt-tsg-mgmt-page?id' + expect(searchPrefixWithSpecialChars(exampleUrls1, prefix)).toEqual(expected1) + expect(searchPrefixWithSpecialChars(exampleUrls2, prefix)).toEqual(expected2) + }) + + it('should return the latest string of an array starting with the prefix followed by ? or #', () => { + const strings = ['test?case', 'test#case', 'test case', 'example?test', 'example#test'] + const prefix = 'test' + const expected = 'test#case' + expect(searchPrefixWithSpecialChars(strings, prefix)).toEqual(expected) + }) + }) +}) diff --git a/libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.ts b/libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.ts new file mode 100644 index 00000000..8da69ad1 --- /dev/null +++ b/libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.ts @@ -0,0 +1,31 @@ +export function findEntryWithKeyword( + array: string[] | undefined, + keyword: string, +): string | null { + const entry = array?.find((entry) => entry.includes(keyword)); + return entry || null; +} + +export function removeKeyword(input: string, keyword: string): string { + let result = input.replace(new RegExp(`/${keyword}.*`), ''); + if (result.endsWith('/')) { + result = result.slice(0, -1); + } + return result; +} + +export function searchPrefixWithSpecialChars( + strings: string[], + prefix: string, +): string | null { + for (let i = strings.length - 1; i >= 0; i--) { + const str = strings[i]; + if ( + str.startsWith(prefix) && + (str[prefix.length] === '?' || str[prefix.length] === '#') + ) { + return str; + } + } + return null; +} diff --git a/libs/ngrx-accelerator/src/lib/store-connector/navigated-event-store-connector-service.ts b/libs/ngrx-accelerator/src/lib/store-connector/navigated-event-store-connector-service.ts index 9bf2812a..e6a68eb9 100644 --- a/libs/ngrx-accelerator/src/lib/store-connector/navigated-event-store-connector-service.ts +++ b/libs/ngrx-accelerator/src/lib/store-connector/navigated-event-store-connector-service.ts @@ -21,8 +21,8 @@ export function provideNavigatedEventStoreConnector() { export class NavigatedEventStoreConnectorService implements OnDestroy { eventsTopic$ = new EventsTopic() constructor(store: Store) { - this.eventsTopic$.pipe(filter((e) => e.type === 'navigated')).subscribe((eventType) => { - store.dispatch(OneCxActions.navigated({ event: eventType.payload })) + this.eventsTopic$.pipe(filter((e) => e.type === 'navigated')).subscribe((navigatedEvent) => { + store.dispatch(OneCxActions.navigated({ event: navigatedEvent.payload })) }) } ngOnDestroy(): void {