-
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathutils.ts
More file actions
57 lines (51 loc) · 1.47 KB
/
Copy pathutils.ts
File metadata and controls
57 lines (51 loc) · 1.47 KB
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
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function isValidUrl(url: string) {
try {
new URL(url);
return true;
} catch {
return false;
}
}
export function getUrlFromString(str: string) {
if (isValidUrl(str)) return str;
try {
if (str.includes(".") && !str.includes(" ")) {
return new URL(`https://${str}`).toString();
}
} catch {
return null;
}
}
// @TODO move this somewhere nicer
const commonCamelCaseCSWords = new Map([
["javascript", "JavaScript"],
["css", "CSS"],
["js", "JS"],
["typescript", "TypeScript"],
]);
// @TODO make a list of words like "JavaScript" that we can map the words to if they exist
/**
* URL-friendly tag slug from a title. Must stay in sync with the slug written on
* tag creation (server/api/router/content.ts + tag.ts). Client-safe fallback for
* tag links when a row's stored slug isn't loaded.
*/
export const slugifyTag = (title: string): string =>
title
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "") || title.toLowerCase();
export const getCamelCaseFromLower = (str: string) => {
let formatedString = commonCamelCaseCSWords.get(str.toLowerCase());
if (!formatedString) {
formatedString = str
.toLowerCase()
.replace(/(?:^|\s|["'([{])+\S/g, (match) => match.toUpperCase());
}
return formatedString;
};