Skip to content

Commit b4b4203

Browse files
committed
Improve hot-path performance (getRepo, getCleanPathname)
1 parent 9b197f6 commit b4b4203

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

index.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,17 @@ TEST: addTests('isEditingWikiPage', [
347347
export const hasWikiPageEditor = (url: URL | HTMLAnchorElement | Location = location): boolean => isEditingWikiPage(url) || isNewWikiPage(url);
348348
TEST: addTests('hasWikiPageEditor', combinedTestOnly);
349349

350-
export const isRepo = (url: URL | HTMLAnchorElement | Location = location): boolean => /^[^/]+\/[^/]+/.test(getCleanPathname(url))
351-
&& !reservedNames.includes(url.pathname.split('/', 2)[1]!)
352-
&& !isDashboard(url)
353-
&& !isGist(url)
354-
&& !isNewRepoTemplate(url);
350+
export const isRepo = (url: URL | HTMLAnchorElement | Location = location): boolean => {
351+
const [user, repo, extra] = getCleanPathname(url).split('/');
352+
return Boolean(
353+
user
354+
&& repo
355+
&& !reservedNames.includes(user)
356+
&& !url.hostname.startsWith('gist.')
357+
&& extra !== 'generate', // Like isNewRepoTemplate but inlined for performance
358+
);
359+
};
360+
355361
TEST: addTests('isRepo', [
356362
// Some of these are here simply as "gotchas" to other detections
357363
'https://github.com/sindresorhus/refined-github/blame/master/package.json',
@@ -751,8 +757,8 @@ TEST: addTests('isNewRepoTemplate', [
751757
/** Get the logged-in user’s username */
752758
const getLoggedInUser = (): string | undefined => $('meta[name="user-login"]')?.getAttribute('content') ?? undefined;
753759

754-
/** Drop all duplicate slashes */
755-
const getCleanPathname = (url: URL | HTMLAnchorElement | Location = location): string => url.pathname.replaceAll(/\/+/g, '/').slice(1, url.pathname.endsWith('/') ? -1 : undefined);
760+
/** Drop all redundant slashes */
761+
const getCleanPathname = (url: URL | HTMLAnchorElement | Location = location): string => url.pathname.replaceAll(/\/\/+/g, '/').replace(/\/$/, '').slice(1);
756762

757763
const getCleanGistPathname = (url: URL | HTMLAnchorElement | Location = location): string | undefined => {
758764
const pathname = getCleanPathname(url);
@@ -765,7 +771,7 @@ const getCleanGistPathname = (url: URL | HTMLAnchorElement | Location = location
765771
};
766772

767773
const getOrg = (url: URL | HTMLAnchorElement | Location = location): {name: string; path: string} | undefined => {
768-
const [, orgs, name, ...path] = url.pathname.split('/');
774+
const [orgs, name, ...path] = getCleanPathname(url).split('/');
769775
if (orgs === 'orgs' && name) {
770776
return {name, path: path.join('/')};
771777
}
@@ -792,6 +798,11 @@ export type RepositoryInfo = {
792798
path: string;
793799
};
794800

801+
/**
802+
* Parse a repository URL into its parts, or return `undefined`
803+
* @param url Can be a full URL or a relative URL
804+
* @returns
805+
*/
795806
const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryInfo | undefined => {
796807
if (!url) {
797808
// We use `canonical` here to use the correct capitalization

0 commit comments

Comments
 (0)