forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1import type { LocationQueryValue } from 'vue-router'
2import { withoutProtocol, withoutTrailingSlash } from 'ufo'
3
4/**
5 * Normalize a URL for comparison by removing protocol, www prefix,
6 * trailing slashes, hash fragments, and common git tree paths.
7 *
8 * Uses ufo utilities where possible, with additional handling for
9 * www prefix and git-specific paths that ufo's isEqual doesn't cover.
10 */
11export function normalizeUrlForComparison(url: string): string {
12 let normalized = withoutProtocol(url).toLowerCase()
13 normalized = withoutTrailingSlash(normalized)
14 normalized = normalized
15 .replace(/^www\./, '')
16 .replace(/#.*$/, '')
17 .replace(/\/tree\/(head|main|master)(\/|$)/i, '/')
18 return withoutTrailingSlash(normalized)
19}
20
21/**
22 * Check if two URLs point to the same resource.
23 * Handles differences in protocol (http/https), www prefix,
24 * trailing slashes, and common git branch paths.
25 */
26export function areUrlsEquivalent(url1: string, url2: string): boolean {
27 return normalizeUrlForComparison(url1) === normalizeUrlForComparison(url2)
28}
29
30export function normalizeSearchParam(query?: LocationQueryValue | LocationQueryValue[]): string {
31 if (!query) return ''
32
33 if (typeof query === 'string') return query
34
35 return normalizeSearchParam(query[0])
36}