forked from onecx/onecx-portal-ui-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move arrayHelperFunctions to lib
and improvements from review
- Loading branch information
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
31 changes: 31 additions & 0 deletions
31
libs/angular-accelerator/src/lib/utils/string-and-array-helper-functions.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters