@@ -17,21 +17,12 @@ import { isUrlOnRealm } from './url';
17
17
*
18
18
* If `url` ends with a slash, the returned array won't have '' as its last
19
19
* element; that element is removed.
20
- *
21
- * This performs a call to `new URL` and therefore may take a fraction of a
22
- * millisecond. Avoid using in a context where it might be called more than
23
- * 10 or 100 times per user action.
24
20
*/
25
- // TODO: Take a URL object for `url` instead of a string, and remove
26
- // performance warning in the jsdoc.
27
21
// TODO: Parse into an array of objects with { negated, operator, operand },
28
22
// like the web app's parse_narrow in static/js/hash_util.js.
29
23
// TODO(#3757): Use @zulip/shared for that parsing.
30
- const getHashSegmentsFromNarrowLink = ( url : string , realm : URL ) => {
31
- // TODO: Get this from caller
32
- const parsedUrl = new URL ( url , realm ) ;
33
-
34
- const result = parsedUrl . hash
24
+ const getHashSegmentsFromNarrowLink = ( url : URL , realm : URL ) => {
25
+ const result = url . hash
35
26
. split ( '/' )
36
27
// Remove the first item, "#narrow".
37
28
. slice ( 1 ) ;
@@ -77,7 +68,7 @@ export const getLinkType = (url: string, realm: URL): LinkType => {
77
68
}
78
69
79
70
// isNarrowLink(…) is true, by early return above, so this call is OK.
80
- const hashSegments = getHashSegmentsFromNarrowLink ( url , realm ) ;
71
+ const hashSegments = getHashSegmentsFromNarrowLink ( parsedUrl , realm ) ;
81
72
82
73
if (
83
74
( hashSegments . length === 2 && hashSegments [ 0 ] === 'pm-with' )
@@ -189,8 +180,11 @@ export const getNarrowFromLink = (
189
180
return null ;
190
181
}
191
182
183
+ // TODO: Get this from caller
184
+ const parsedUrl = new URL ( url , realm ) ;
185
+
192
186
// isNarrowLink(…) is true, by early return above, so this call is OK.
193
- const hashSegments = getHashSegmentsFromNarrowLink ( url , realm ) ;
187
+ const hashSegments = getHashSegmentsFromNarrowLink ( parsedUrl , realm ) ;
194
188
195
189
switch ( type ) {
196
190
case 'pm' : {
@@ -233,8 +227,11 @@ export const getNarrowFromLink = (
233
227
* 10 or 100 times per user action.
234
228
*/
235
229
export const getNearOperandFromLink = ( url : string , realm : URL ) : number | null = > {
230
+ // TODO: Get this from caller
231
+ const parsedUrl = new URL ( url , realm ) ;
232
+
236
233
// isNarrowLink(…) is true, by jsdoc, so this call is OK.
237
- const hashSegments = getHashSegmentsFromNarrowLink ( url , realm ) ;
234
+ const hashSegments = getHashSegmentsFromNarrowLink ( parsedUrl , realm ) ;
238
235
239
236
// This and nearOperandIndex can simplify when we rename/repurpose
240
237
// getHashSegmentsFromNarrowLink so it gives an array of
0 commit comments