Skip to content

Commit d9fd3d9

Browse files
vNovskiFrancescoMolinaro
authored andcommitted
[CST-13263] feature: added in links queryParams and add new function for internal-link
1 parent 9d41408 commit d9fd3d9

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

src/app/core/services/internal-link.service.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,40 @@ export class InternalLinkService {
3939
* @returns The relative path for the given internal link.
4040
*/
4141
public getRelativePath(link: string): string {
42-
// Create a Domain object for the provided link
42+
const baseUrl = link.split('?')[0];
4343
const currentDomain = new URL(this.currentURL).hostname;
4444

45-
if (link.startsWith(this.currentURL)) {
46-
const currentSegments = link.substring(this.currentURL.length);
45+
if (baseUrl.startsWith(this.currentURL) || baseUrl.startsWith(currentDomain)) {
46+
const base = baseUrl.startsWith(this.currentURL) ? this.currentURL : currentDomain;
47+
const currentSegments = baseUrl.substring(base.length);
4748
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
4849
}
4950

50-
if (link.startsWith(currentDomain)) {
51-
const currentSegments = link.substring(currentDomain.length);
52-
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
51+
return baseUrl.startsWith('/') ? baseUrl : `/${baseUrl}`;
52+
}
53+
54+
/**
55+
* Parse the query parameters from a given URL link.
56+
*
57+
* @param link The URL link containing query parameters.
58+
* @returns An object containing the parsed query parameters.
59+
*/
60+
public getQueryParams(link: string): Record<string, string> {
61+
const queryParams: Record<string, string> = {};
62+
63+
const queryStringStartIndex = link.indexOf('?');
64+
if (queryStringStartIndex !== -1) {
65+
const paramsString = link.substring(queryStringStartIndex + 1);
66+
const paramsArray = paramsString.split('&');
67+
68+
paramsArray.forEach(param => {
69+
const [key, value] = param.split('=');
70+
if (key && value) {
71+
queryParams[key] = decodeURIComponent(value.replace(/\+/g, ' '));
72+
}
73+
});
5374
}
5475

55-
return link;
76+
return queryParams;
5677
}
5778
}

0 commit comments

Comments
 (0)