aboutsummaryrefslogtreecommitdiff
path: root/apps/web/utils/url-helpers.ts
blob: 3a9ef5de0d99b9fd4f571735bc861f4871e21c54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
export const isValidUrl = (url: string): boolean => {
	try {
		new URL(url)
		return true
	} catch {
		return false
	}
}

export const normalizeUrl = (url: string): string => {
	if (!url.trim()) return ""
	if (url.startsWith("http://") || url.startsWith("https://")) {
		return url
	}
	return `https://${url}`
}

export const isTwitterUrl = (url: string): boolean => {
	const normalizedUrl = url.toLowerCase()
	return (
		normalizedUrl.includes("twitter.com") || normalizedUrl.includes("x.com")
	)
}

export const isLinkedInProfileUrl = (url: string): boolean => {
	const normalizedUrl = url.toLowerCase()
	return (
		normalizedUrl.includes("linkedin.com/in/") &&
		!normalizedUrl.includes("linkedin.com/company/")
	)
}

export const collectValidUrls = (
	linkedinProfile: string,
	otherLinks: string[],
): string[] => {
	const urls: string[] = []

	if (linkedinProfile.trim()) {
		const normalizedLinkedIn = normalizeUrl(linkedinProfile.trim())
		if (
			isValidUrl(normalizedLinkedIn) &&
			isLinkedInProfileUrl(normalizedLinkedIn)
		) {
			urls.push(normalizedLinkedIn)
		}
	}

	otherLinks
		.filter((link) => link.trim())
		.forEach((link) => {
			const normalizedLink = normalizeUrl(link.trim())
			if (isValidUrl(normalizedLink) && !isTwitterUrl(normalizedLink)) {
				urls.push(normalizedLink)
			}
		})

	return urls
}