Skip to content

Commit d5db368

Browse files
committed
[DSC-1484] feature: added internal link service
1 parent fb8ad18 commit d5db368

4 files changed

Lines changed: 129 additions & 94 deletions

File tree

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

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -251,55 +251,4 @@ 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-
305254
});

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -123,47 +123,4 @@ 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-
169126
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { TestBed, waitForAsync } from '@angular/core/testing';
2+
import { InternalLinkService } from './internal-link.service';
3+
import { NativeWindowService } from './window.service';
4+
5+
describe('InternalLinkService', () => {
6+
let service: InternalLinkService;
7+
8+
beforeEach(waitForAsync(() => {
9+
return TestBed.configureTestingModule({
10+
providers: [
11+
InternalLinkService,
12+
{ provide: NativeWindowService, useValue: { nativeWindow: { location: { origin: 'https://currentdomain' } } } },
13+
],
14+
}).compileComponents();
15+
}));
16+
17+
beforeEach(() => {
18+
service = TestBed.inject(InternalLinkService);
19+
});
20+
21+
describe('isLinkInternal', () => {
22+
it('should return true for internal link starting with "/"', () => {
23+
const result = service.isLinkInternal('/my-link');
24+
expect(result).toBe(true);
25+
});
26+
27+
it('should return true for internal link starting with currentURL', () => {
28+
const result = service.isLinkInternal('https://currentdomain/my-link');
29+
expect(result).toBe(true);
30+
});
31+
32+
it('should return true for internal link starting with "currentdomain"', () => {
33+
const result = service.isLinkInternal('currentdomain/my-link');
34+
expect(result).toBe(true);
35+
});
36+
37+
it('should return false for external link', () => {
38+
const result = service.isLinkInternal('https://externaldomain/my-link');
39+
expect(result).toBe(false);
40+
});
41+
42+
it('should return true for internal link without leading "/"', () => {
43+
const result = service.isLinkInternal('my-link');
44+
expect(result).toBe(true);
45+
});
46+
});
47+
48+
describe('transformInternalLink', () => {
49+
it('should transform internal link by removing currentURL', () => {
50+
const result = service.transformInternalLink('https://currentdomain/my-link');
51+
expect(result).toBe('/my-link');
52+
});
53+
54+
it('should transform internal link by adding leading "/" if missing', () => {
55+
const result = service.transformInternalLink('currentdomain/my-link');
56+
expect(result).toBe('/my-link');
57+
});
58+
59+
it('should return unchanged link for external link', () => {
60+
const result = service.transformInternalLink('https://externalDomain/my-link');
61+
expect(result).toBe('https://externalDomain/my-link');
62+
});
63+
64+
it('should return unchanged link for internal link with leading "/"', () => {
65+
const result = service.transformInternalLink('/my-link');
66+
expect(result).toBe('/my-link');
67+
});
68+
});
69+
70+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Inject, Injectable } from '@angular/core';
2+
import { NativeWindowRef, NativeWindowService } from './window.service';
3+
4+
/**
5+
* LinkService provides utility functions for working with links, such as checking if a link is internal
6+
* and transforming internal links based on the current URL.
7+
*/
8+
@Injectable()
9+
export class InternalLinkService {
10+
currentURL = this._window.nativeWindow.location.origin;
11+
12+
constructor(
13+
@Inject(NativeWindowService) protected _window: NativeWindowRef,
14+
) {
15+
16+
}
17+
18+
/**
19+
* Check if the provided link is internal, i.e., it starts with a '/' or matches the current URL.
20+
*
21+
* @param link The link to be checked.
22+
* @param currentURL The current URL to compare against.
23+
* @returns A boolean indicating whether the link is internal.
24+
*/
25+
public isLinkInternal(link: string): boolean {
26+
// Create a Domain object for the provided link
27+
const currentDomain = new URL(this.currentURL).hostname;
28+
29+
return link.startsWith('/')
30+
|| link.startsWith(this.currentURL)
31+
|| link.startsWith(currentDomain)
32+
|| link === currentDomain
33+
|| !link.includes('://');
34+
}
35+
36+
/**
37+
* Transform an internal link based on the current URL.
38+
*
39+
* @param link The link to be transformed.
40+
* @param currentURL The current URL used for transformation.
41+
* @returns The transformed internal link.
42+
*/
43+
public transformInternalLink(link: string): string {
44+
// Create a Domain object for the provided link
45+
const currentDomain = new URL(this.currentURL).hostname;
46+
47+
if (link.startsWith(this.currentURL)) {
48+
const currentSegments = link.substring(this.currentURL.length);
49+
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
50+
}
51+
52+
if (link.startsWith(currentDomain)) {
53+
const currentSegments = link.substring(currentDomain.length);
54+
return currentSegments.startsWith('/') ? currentSegments : `/${currentSegments}`;
55+
}
56+
57+
return link;
58+
}
59+
}

0 commit comments

Comments
 (0)