Skip to content

Commit 094464b

Browse files
vNovskiFrancescoMolinaro
authored andcommitted
[DSC-1484] feature: added utility functions
1 parent 4d840b5 commit 094464b

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

src/app/core/cache/builders/link.service.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,55 @@ describe('LinkService', () => {
251251
});
252252
});
253253

254+
describe('User Functions', () => {
255+
fdescribe('isLinkInternal', () => {
256+
it('should return true for internal link starting with "/"', () => {
257+
result = service.isLinkInternal('/my-link', 'https://currentdomain');
258+
expect(result).toBe(true);
259+
});
260+
261+
it('should return true for internal link starting with currentURL', () => {
262+
result = service.isLinkInternal('https://currentdomain/my-link', 'https://currentdomain');
263+
expect(result).toBe(true);
264+
});
265+
266+
it('should return true for internal link starting with "currentdomain"', () => {
267+
result = service.isLinkInternal('currentdomain/my-link', 'https://currentdomain');
268+
expect(result).toBe(true);
269+
});
270+
271+
it('should return false for external link', () => {
272+
result = service.isLinkInternal('https://externaldomain/my-link', 'https://currentdomain');
273+
expect(result).toBe(false);
274+
});
275+
276+
it('should return true for internal link link without leading "/"', () => {
277+
result = service.isLinkInternal('my-link', 'https://currentdomain');
278+
expect(result).toBe(true);
279+
});
280+
});
281+
282+
fdescribe('transformInternalLink', () => {
283+
it('should transform internal link by removing currentURL', () => {
284+
result = service.transformInternalLink('https://currentdomain/my-link', 'https://currentdomain');
285+
expect(result).toBe('/my-link');
286+
});
287+
288+
it('should transform internal link by adding leading "/" if missing', () => {
289+
result = service.transformInternalLink('currentdomain/my-link', 'https://currentdomain');
290+
expect(result).toBe('/my-link');
291+
});
292+
293+
it('should return unchanged link for external link', () => {
294+
result = service.transformInternalLink('https://externalDomain/my-link', 'https://currentdomain');
295+
expect(result).toBe('https://externalDomain/my-link');
296+
});
297+
298+
it('should return unchanged link for internal link with leading "/"', () => {
299+
result = service.transformInternalLink('/my-link', 'https://currentdomain');
300+
expect(result).toBe('/my-link');
301+
});
302+
});
303+
});
304+
254305
});

src/app/core/cache/builders/link.service.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,47 @@ export class LinkService {
123123
return result;
124124
}
125125

126+
/**
127+
* Check if the provided link is internal, i.e., it starts with a '/' or matches the current URL.
128+
*
129+
* @param link The link to be checked.
130+
* @param currentURL The current URL to compare against.
131+
* @returns A boolean indicating whether the link is internal.
132+
*/
133+
public isLinkInternal(link: string, currentURL: string): boolean {
134+
// Create a Domain object for the provided link
135+
const currentDomain = new URL(currentURL).hostname;
136+
137+
return link.startsWith('/')
138+
|| link.startsWith(currentURL)
139+
|| link.startsWith(currentDomain)
140+
|| link === currentDomain
141+
|| !link.includes('://');
142+
}
143+
144+
/**
145+
* Transform an internal link based on the current URL.
146+
*
147+
* @param link The link to be transformed.
148+
* @param currentURL The current URL used for transformation.
149+
* @returns The transformed internal link.
150+
*/
151+
public transformInternalLink(link: string, currentURL: string): string {
152+
// Create a Domain object for the provided link
153+
const currentDomain = new URL(currentURL).hostname;
154+
155+
if (link.startsWith(currentURL)) {
156+
const currentSegments = link.substring(currentURL.length);
157+
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
158+
}
159+
160+
if (link.startsWith(currentDomain)) {
161+
const currentSegments = link.substring(currentDomain.length);
162+
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
163+
}
164+
165+
return link;
166+
167+
}
168+
126169
}

0 commit comments

Comments
 (0)