30 lines · 991 BRaw
| 1 | export type RepoView = "tree" | "blob" | "raw" | "commits" | "last-commits"; |
| 2 | |
| 3 | export function encodePath(segments: readonly string[]): string { |
| 4 | return segments.map(encodeURIComponent).join("/"); |
| 5 | } |
| 6 | |
| 7 | export function repoLink(repo: string, view: RepoView, ref: string, path: readonly string[] = []): string { |
| 8 | return `/${repo}/${view}/${encodePath([...ref.split("/"), ...path])}`; |
| 9 | } |
| 10 | |
| 11 | /** Splits a URL splat into decoded segments, or returns null when it is not valid percent-encoding. */ |
| 12 | export function splatSegments(splat: string | undefined): string[] | null { |
| 13 | try { |
| 14 | return (splat ?? "").split("/").filter(Boolean).map(decodeURIComponent); |
| 15 | } catch { |
| 16 | return null; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | export function isCommitHash(value: string): boolean { |
| 21 | return /^[0-9a-f]{40}$/.test(value); |
| 22 | } |
| 23 | |
| 24 | export function taskLink(repo: string, number: number): string { |
| 25 | return `/${repo}/tasks/${number}`; |
| 26 | } |
| 27 | |
| 28 | export function pullLink(repo: string, number: number): string { |
| 29 | return `/${repo}/pulls/${number}`; |
| 30 | } |