UNPKG

632 BPlain TextView Raw
1export interface SluggablePackage {
2 homepage?: string
3 repository?: string | { url?: string }
4}
5
6export function getGitHubRepoSlug(pkg: SluggablePackage) {
7 let match = null
8 if (typeof pkg.repository === 'string') {
9 match = pkg.repository.match(/^(?:github:)?([^/:]+\/[^/:]+)$/)
10 } else {
11 let url = null
12 if (pkg.repository && typeof pkg.repository.url === 'string') {
13 url = pkg.repository && pkg.repository.url
14 } else if (typeof pkg.homepage === 'string') {
15 url = pkg.homepage
16 } else {
17 return null
18 }
19 match = url.match(/github\.com\/([^/:]+\/[^/:]+?)(?:\.git|\/)?$/)
20 }
21 return (match && match[1]) || null
22}