Skip to content

Commit 4b63be2

Browse files
chrisbobbegnprice
authored andcommitted
links [nfc]: Move getLinkType's isNarrowLink out to caller
1 parent 083f6bf commit 4b63be2

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/utils/__tests__/internalLinks-test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ describe('isNarrowLink', () => {
117117
});
118118

119119
describe('getLinkType', () => {
120-
test('links to a different domain are of "non-narrow" type', () => {
121-
expect(getLinkType(new URL('https://google.com/some-path'), realm)).toBe('non-narrow');
122-
});
123-
124-
test('only in-app link containing "stream" is a stream link', () => {
120+
test('only link containing "stream" is a stream link', () => {
125121
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group', realm), realm)).toBe('pm');
126122
expect(getLinkType(new URL('/#narrow/stream/jest', realm), realm)).toBe('stream');
127123
expect(getLinkType(new URL('/#narrow/stream/stream/', realm), realm)).toBe('stream');
@@ -154,7 +150,7 @@ describe('getLinkType', () => {
154150
);
155151
});
156152

157-
test('only in-app link containing "pm-with" is a group link', () => {
153+
test('only link containing "pm-with" is a group link', () => {
158154
expect(getLinkType(new URL('/#narrow/stream/jest/topic/test', realm), realm)).toBe('topic');
159155
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group', realm), realm)).toBe('pm');
160156
expect(getLinkType(new URL('/#narrow/pm-with/1,2-group/near/1', realm), realm)).toBe('pm');
@@ -163,7 +159,7 @@ describe('getLinkType', () => {
163159
).toBe('pm');
164160
});
165161

166-
test('only in-app link containing "is" is a special link', () => {
162+
test('only link containing "is" is a special link', () => {
167163
expect(getLinkType(new URL('/#narrow/stream/jest/topic/test', realm), realm)).toBe('topic');
168164
expect(getLinkType(new URL('/#narrow/is/private', realm), realm)).toBe('special');
169165
expect(getLinkType(new URL('/#narrow/is/starred', realm), realm)).toBe('special');

src/utils/internalLinks.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ export const isNarrowLink = (url: URL, realm: URL): boolean =>
4848
&& url.search === ''
4949
&& /^#narrow\//i.test(url.hash);
5050

51-
type LinkType = 'non-narrow' | 'home' | 'pm' | 'topic' | 'stream' | 'special';
51+
type LinkType = 'home' | 'pm' | 'topic' | 'stream' | 'special';
5252

5353
/**
5454
* PRIVATE -- exported only for tests.
55+
*
56+
* The passed `url` must appear to be a link to a Zulip narrow on the given
57+
* `realm`. In particular, `isNarrowLink(url, realm)` must be true.
5558
*/
5659
// TODO: Work out what this does, write a jsdoc for its interface, and
5760
// reimplement using URL object (not just for the realm)
5861
export const getLinkType = (url: URL, realm: URL): LinkType => {
59-
if (!isNarrowLink(url, realm)) {
60-
return 'non-narrow';
61-
}
62-
63-
// isNarrowLink(…) is true, by early return above, so this call is OK.
62+
// isNarrowLink(…) is true, by jsdoc, so this call is OK.
6463
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
6564

6665
if (
@@ -163,12 +162,12 @@ export const getNarrowFromLink = (
163162
streamsByName: Map<string, Stream>,
164163
ownUserId: UserId,
165164
): Narrow | null => {
166-
const type = getLinkType(url, realm);
167-
168-
if (type === 'non-narrow') {
165+
if (!isNarrowLink(url, realm)) {
169166
return null;
170167
}
171168

169+
const type = getLinkType(url, realm);
170+
172171
// isNarrowLink(…) is true, by early return above, so this call is OK.
173172
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
174173

0 commit comments

Comments
 (0)