diff --git a/.gitignore b/.gitignore index 46cb338..a338f91 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ distribution dist .cache .parcel-cache +.vscode/ \ No newline at end of file diff --git a/index.ts b/index.ts index 738b517..350ce10 100644 --- a/index.ts +++ b/index.ts @@ -812,6 +812,38 @@ TEST: addTests('isRepositoryActions', [ 'https://github.com/refined-github/github-url-detection/actions/workflows/esm-lint.yml', ]); +const isStars = (url: URL | HTMLAnchorElement | Location = location): boolean => { + const patharr = getCleanPathname(url).split('/'); + const [stars, , subpath] = patharr; // Only get the elements we're only to check from destructuring to avoid compile error + return Boolean(stars === 'stars' + && ([1, 2, 4].includes(patharr.length) + || (subpath === 'lists' && patharr.length !== 3) + || (subpath && ['repositories', 'topics'].includes(subpath) && patharr.length === 3)), + ); +}; + +TEST: addTests('isStars', [ + 'https://github.com/stars', + 'https://github.com/stars/lstn', + 'https://github.com/stars/lstn/repositories', + 'https://github.com/stars/lstn/topics', + 'https://github.com/stars/lstn/lists/test', +]); + +export const isOwnStars = (url: URL | HTMLAnchorElement | Location = location): boolean => + isStars(url) + && [['stars'], ['stars', getLoggedInUser()]].some( + (needle => pathArray => pathArray.length === needle.length + && pathArray.every((v, i) => v === needle[i]))(getCleanPathname(url).split('/').slice(0, 2)), + ); + +export const isStarsList = (url: URL | HTMLAnchorElement | Location = location): boolean => isStars(url) && getCleanPathname(url).split('/')[2] === 'lists'; +TEST: addTests('isStarsList', [ + 'https://github.com/stars/lstn/lists/test', +]); + +export const isOwnStarsList = (url: URL | HTMLAnchorElement | Location = location): boolean => isOwnStars(url) && getCleanPathname(url).split('/')[2] === 'lists'; + export const isUserTheOrganizationOwner = (): boolean => isOrganizationProfile() && exists('[aria-label="Organization"] [data-tab-item="org-header-settings-tab"]'); export const canUserAdminRepo = (): boolean => isRepo() && exists('.reponav-item[href$="/settings"], [data-tab-item$="settings-tab"]');