Skip to content

Commit

Permalink
feat: move arrayHelperFunctions to lib
Browse files Browse the repository at this point in the history
and improvements from review
  • Loading branch information
anninowak committed Oct 15, 2024
1 parent 449f7df commit 1f138da
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions libs/angular-accelerator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1f138da

Please sign in to comment.