Skip to content

Commit 2bb12a8

Browse files
committed
internalLinks [nfc]: Have getLinkType take URL object
1 parent cd286b6 commit 2bb12a8

File tree

2 files changed

+34
-41
lines changed

2 files changed

+34
-41
lines changed

src/utils/__tests__/internalLinks-test.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,59 +118,59 @@ describe('isNarrowLink', () => {
118118

119119
describe('getLinkType', () => {
120120
test('links to a different domain are of "non-narrow" type', () => {
121-
expect(getLinkType('https://google.com/some-path', realm)).toBe('non-narrow');
121+
expect(getLinkType(new URL('https://google.com/some-path'), realm)).toBe('non-narrow');
122122
});
123123

124124
test('only in-app link containing "stream" is a stream link', () => {
125-
expect(getLinkType('https://example.com/#narrow/pm-with/1,2-group', realm)).toBe('pm');
126-
expect(getLinkType('https://example.com/#narrow/stream/jest', realm)).toBe('stream');
127-
expect(getLinkType('https://example.com/#narrow/stream/stream/', realm)).toBe('stream');
125+
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group', realm), realm)).toBe('pm');
126+
expect(getLinkType(new URL('/#narrow/stream/jest', realm), realm)).toBe('stream');
127+
expect(getLinkType(new URL('/#narrow/stream/stream/', realm), realm)).toBe('stream');
128128
});
129129

130130
test('when a url is not a topic narrow return false', () => {
131-
expect(getLinkType('https://example.com/#narrow/pm-with/1,2-group', realm)).toBe('pm');
132-
expect(getLinkType('https://example.com/#narrow/stream/jest', realm)).toBe('stream');
133-
expect(getLinkType('https://example.com/#narrow/stream/stream/topic/topic/near/', realm)).toBe(
131+
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group', realm), realm)).toBe('pm');
132+
expect(getLinkType(new URL('/#narrow/stream/jest', realm), realm)).toBe('stream');
133+
expect(getLinkType(new URL('/#narrow/stream/stream/topic/topic/near/', realm), realm)).toBe(
134134
'home',
135135
);
136-
expect(getLinkType('https://example.com/#narrow/stream/topic/', realm)).toBe('stream');
136+
expect(getLinkType(new URL('/#narrow/stream/topic/', realm), realm)).toBe('stream');
137137
});
138138

139139
test('when a url is a topic narrow return true', () => {
140-
expect(getLinkType('https://example.com/#narrow/stream/jest/topic/test', realm)).toBe('topic');
140+
expect(getLinkType(new URL('/#narrow/stream/jest/topic/test', realm), realm)).toBe('topic');
141141
expect(
142-
getLinkType('https://example.com/#narrow/stream/mobile/subject/topic/near/378333', realm),
142+
getLinkType(new URL('/#narrow/stream/mobile/subject/topic/near/378333', realm), realm),
143143
).toBe('topic');
144-
expect(getLinkType('https://example.com/#narrow/stream/mobile/topic/topic/', realm)).toBe(
144+
expect(getLinkType(new URL('/#narrow/stream/mobile/topic/topic/', realm), realm)).toBe('topic');
145+
expect(getLinkType(new URL('/#narrow/stream/stream/topic/topic/near/1', realm), realm)).toBe(
145146
'topic',
146147
);
147-
expect(getLinkType('https://example.com/#narrow/stream/stream/topic/topic/near/1', realm)).toBe(
148+
expect(getLinkType(new URL('/#narrow/stream/stream/subject/topic/near/1', realm), realm)).toBe(
148149
'topic',
149150
);
150-
expect(
151-
getLinkType('https://example.com/#narrow/stream/stream/subject/topic/near/1', realm),
152-
).toBe('topic');
153151

154-
expect(getLinkType('/#narrow/stream/stream/subject/topic', realm)).toBe('topic');
152+
expect(getLinkType(new URL('/#narrow/stream/stream/subject/topic', realm), realm)).toBe(
153+
'topic',
154+
);
155155
});
156156

157157
test('only in-app link containing "pm-with" is a group link', () => {
158-
expect(getLinkType('https://example.com/#narrow/stream/jest/topic/test', realm)).toBe('topic');
159-
expect(getLinkType('https://example.com/#narrow/pm-with/1,2-group', realm)).toBe('pm');
160-
expect(getLinkType('https://example.com/#narrow/pm-with/1,2-group/near/1', realm)).toBe('pm');
158+
expect(getLinkType(new URL('/#narrow/stream/jest/topic/test', realm), realm)).toBe('topic');
159+
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group', realm), realm)).toBe('pm');
160+
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group/near/1', realm), realm)).toBe('pm');
161161
expect(
162-
getLinkType('https://example.com/#narrow/pm-with/a.40b.2Ecom.2Ec.2Ed.2Ecom/near/3', realm),
162+
getLinkType(new URL('/#narrow/pm-with/a.40b.2Ecom.2Ec.2Ed.2Ecom/near/3', realm), realm),
163163
).toBe('pm');
164164
});
165165

166166
test('only in-app link containing "is" is a special link', () => {
167-
expect(getLinkType('https://example.com/#narrow/stream/jest/topic/test', realm)).toBe('topic');
168-
expect(getLinkType('https://example.com/#narrow/is/private', realm)).toBe('special');
169-
expect(getLinkType('https://example.com/#narrow/is/starred', realm)).toBe('special');
170-
expect(getLinkType('https://example.com/#narrow/is/mentioned', realm)).toBe('special');
171-
expect(getLinkType('https://example.com/#narrow/is/men', realm)).toBe('home');
172-
expect(getLinkType('https://example.com/#narrow/is/men/stream', realm)).toBe('home');
173-
expect(getLinkType('https://example.com/#narrow/are/men/stream', realm)).toBe('home');
167+
expect(getLinkType(new URL('/#narrow/stream/jest/topic/test', realm), realm)).toBe('topic');
168+
expect(getLinkType(new URL('/#narrow/is/private', realm), realm)).toBe('special');
169+
expect(getLinkType(new URL('/#narrow/is/starred', realm), realm)).toBe('special');
170+
expect(getLinkType(new URL('/#narrow/is/mentioned', realm), realm)).toBe('special');
171+
expect(getLinkType(new URL('/#narrow/is/men', realm), realm)).toBe('home');
172+
expect(getLinkType(new URL('/#narrow/is/men/stream', realm), realm)).toBe('home');
173+
expect(getLinkType(new URL('/#narrow/are/men/stream', realm), realm)).toBe('home');
174174
});
175175
});
176176

src/utils/internalLinks.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,16 @@ type LinkType = 'non-narrow' | 'home' | 'pm' | 'topic' | 'stream' | 'special';
5252

5353
/**
5454
* PRIVATE -- exported only for tests.
55-
*
56-
* This performs a call to `new URL` and therefore may take a fraction of a
57-
* millisecond. Avoid using in a context where it might be called more than
58-
* 10 or 100 times per user action.
5955
*/
6056
// TODO: Work out what this does, write a jsdoc for its interface, and
6157
// reimplement using URL object (not just for the realm)
62-
export const getLinkType = (url: string, realm: URL): LinkType => {
63-
// TODO: Get this from caller
64-
const parsedUrl = new URL(url, realm);
65-
66-
if (!isNarrowLink(parsedUrl, realm)) {
58+
export const getLinkType = (url: URL, realm: URL): LinkType => {
59+
if (!isNarrowLink(url, realm)) {
6760
return 'non-narrow';
6861
}
6962

7063
// isNarrowLink(…) is true, by early return above, so this call is OK.
71-
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
64+
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
7265

7366
if (
7467
(hashSegments.length === 2 && hashSegments[0] === 'pm-with')
@@ -174,15 +167,15 @@ export const getNarrowFromLink = (
174167
streamsByName: Map<string, Stream>,
175168
ownUserId: UserId,
176169
): Narrow | null => {
177-
const type = getLinkType(url, realm);
170+
// TODO: Get this from caller
171+
const parsedUrl = new URL(url, realm);
172+
173+
const type = getLinkType(parsedUrl, realm);
178174

179175
if (type === 'non-narrow') {
180176
return null;
181177
}
182178

183-
// TODO: Get this from caller
184-
const parsedUrl = new URL(url, realm);
185-
186179
// isNarrowLink(…) is true, by early return above, so this call is OK.
187180
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
188181

0 commit comments

Comments
 (0)