-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
44 lines (38 loc) · 1.33 KB
/
utils.ts
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
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export const extractTags = (content: string) => {
const tags = content.match(/(?<!\w)#(\w+)/g);
return tags?.map((tag) => tag.slice(1));
};
export const isTag = (word: string) => {
return word.startsWith("#") && word.length > 1;
};
export const getTimeDifference = (datetime: string) => {
const currentTime = new Date();
const givenTime = new Date(datetime);
const currentYear = currentTime.getFullYear();
const givenYear = givenTime.getFullYear();
const diffInMilliseconds = currentTime.getTime() - givenTime.getTime();
const diffInMinutes = Math.floor(diffInMilliseconds / (1000 * 60));
const diffInHours = Math.floor(diffInMilliseconds / (1000 * 60 * 60));
const diffInDays = Math.floor(diffInMilliseconds / (1000 * 60 * 60 * 24));
if (diffInMinutes < 1) {
return "Now";
} else if (diffInMinutes < 60) {
return `${diffInMinutes}m`;
} else if (diffInHours < 24) {
return `${diffInHours}h`;
} else if (diffInDays < 7) {
return `${diffInDays}d`;
} else {
const isDifferentYear = currentYear - givenYear;
return `${givenTime.toLocaleDateString("en-us", {
month: "short",
day: "numeric",
year: isDifferentYear ? "numeric" : undefined,
})}`;
}
};