Skip to content

Commit da63e22

Browse files
committed
internalLinks [nfc]: Have getNearOperandFromLink take URL object
1 parent 4925441 commit da63e22

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

src/message/messagesActions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export const messageLinkPress =
240240
// which should be futile.
241241
if (narrow) {
242242
// This call is OK: `narrow` is truthy, so isNarrowLink(…) was true.
243-
const nearOperand = getNearOperandFromLink(href, auth.realm);
243+
const nearOperand = getNearOperandFromLink(parsedUrl, auth.realm);
244244
if (nearOperand === null) {
245245
dispatch(doNarrow(narrow));
246246
return;

src/utils/__tests__/internalLinks-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,23 +371,23 @@ describe('getNarrowFromLink', () => {
371371

372372
describe('getNearOperandFromLink', () => {
373373
test('not message link', () => {
374-
expect(getNearOperandFromLink('https://example.com/#narrow/is/private', realm)).toBe(null);
375-
expect(getNearOperandFromLink('https://example.com/#narrow/stream/jest', realm)).toBe(null);
374+
expect(getNearOperandFromLink(new URL('/#narrow/is/private', realm), realm)).toBe(null);
375+
expect(getNearOperandFromLink(new URL('/#narrow/stream/jest', realm), realm)).toBe(null);
376376
});
377377

378378
test('`near` is the only operator', () => {
379-
expect(getNearOperandFromLink('https://example.com/#narrow/near/1', realm)).toBe(1);
379+
expect(getNearOperandFromLink(new URL('/#narrow/near/1', realm), realm)).toBe(1);
380380
});
381381

382382
test('when link is a group link, return anchor message id', () => {
383383
expect(
384-
getNearOperandFromLink('https://example.com/#narrow/pm-with/1,3-group/near/1/', realm),
384+
getNearOperandFromLink(new URL('/#narrow/pm-with/1,3-group/near/1/', realm), realm),
385385
).toBe(1);
386386
});
387387

388388
test('when link is a topic link, return anchor message id', () => {
389389
expect(
390-
getNearOperandFromLink('https://example.com/#narrow/stream/jest/topic/test/near/1', realm),
390+
getNearOperandFromLink(new URL('/#narrow/stream/jest/topic/test/near/1', realm), realm),
391391
).toBe(1);
392392
});
393393
});

src/utils/internalLinks.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,10 @@ export const getNarrowFromLink = (
207207
/**
208208
* From a URL and realm with `isNarrowLink(url, realm) === true`, give
209209
* message_id if the URL has /near/{message_id}, otherwise give null.
210-
*
211-
* This performs a call to `new URL` and therefore may take a fraction of a
212-
* millisecond. Avoid using in a context where it might be called more than
213-
* 10 or 100 times per user action.
214210
*/
215-
export const getNearOperandFromLink = (url: string, realm: URL): number | null => {
216-
// TODO: Get this from caller
217-
const parsedUrl = new URL(url, realm);
218-
211+
export const getNearOperandFromLink = (url: URL, realm: URL): number | null => {
219212
// isNarrowLink(…) is true, by jsdoc, so this call is OK.
220-
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
213+
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
221214

222215
// This and nearOperandIndex can simplify when we rename/repurpose
223216
// getHashSegmentsFromNarrowLink so it gives an array of

0 commit comments

Comments
 (0)