Skip to content

Commit e0db8a2

Browse files
chrisbobbegnprice
authored andcommitted
narrow [nfc]: Have specialNarrow take string-literals union, not string
Oddly, the regex was doing a looser check on the operand than `specialNarrow` was: it was case-insensitive, and it was only looking at the beginning of the string. That doesn't end up making a difference, because `specialNarrow` would throw an error and we'd return null, the same as if the regex hadn't matched the string.
1 parent 7b34241 commit e0db8a2

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/utils/internalLinks.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,11 @@ export const getNarrowFromNarrowLink = (
173173
if (
174174
hashSegments.length === 2
175175
&& hashSegments[0] === 'is'
176-
&& /^(private|starred|mentioned)/i.test(hashSegments[1])
176+
&& (hashSegments[1] === 'starred'
177+
|| hashSegments[1] === 'mentioned'
178+
|| hashSegments[1] === 'private')
177179
) {
178-
try {
179-
return specialNarrow(hashSegments[1]);
180-
} catch {
181-
return null;
182-
}
180+
return specialNarrow(hashSegments[1]);
183181
}
184182

185183
return null; // TODO(?) Give HOME_NARROW

src/utils/narrow.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
makePmKeyRecipients_UNSAFE,
1414
} from './recipient';
1515
import type { UserMessageFlag } from '../api/modelTypes';
16+
import { ensureUnreachable } from '../generics';
1617

1718
/* eslint-disable no-use-before-define */
1819

@@ -123,17 +124,18 @@ export const pmNarrowFromUsersUnsafe = (recipients: $ReadOnlyArray<UserOrBot>):
123124
export const pm1to1NarrowFromUser = (user: UserOrBot): Narrow =>
124125
pmNarrowInternal(pmKeyRecipientsFor1to1(user.user_id));
125126

126-
export const specialNarrow = (operand: string): Narrow => {
127-
if (operand === 'starred') {
128-
return Object.freeze({ type: 'starred' });
129-
}
130-
if (operand === 'mentioned') {
131-
return Object.freeze({ type: 'mentioned' });
132-
}
133-
if (operand === 'private') {
134-
return Object.freeze({ type: 'all-pm' });
127+
export const specialNarrow = (operand: 'starred' | 'mentioned' | 'private'): Narrow => {
128+
switch (operand) {
129+
case 'starred':
130+
return Object.freeze({ type: 'starred' });
131+
case 'mentioned':
132+
return Object.freeze({ type: 'mentioned' });
133+
case 'private':
134+
return Object.freeze({ type: 'all-pm' });
135+
default:
136+
ensureUnreachable(operand);
137+
throw new Error();
135138
}
136-
throw new Error(`specialNarrow: got unsupported operand: ${operand}`);
137139
};
138140

139141
export const STARRED_NARROW: Narrow = specialNarrow('starred');

0 commit comments

Comments
 (0)