generated from nisabmohd/Aria-Docs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
80 lines (70 loc) · 2.07 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { EachRoute, ROUTES } from "./routes-config";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function helperSearch(
query: string,
node: EachRoute,
prefix: string,
currenLevel: number,
maxLevel?: number
) {
const res: EachRoute[] = [];
let parentHas = false;
const nextLink = `${prefix}${node.href}`;
if (!node.noLink && node.title.toLowerCase().includes(query.toLowerCase())) {
res.push({ ...node, items: undefined, href: nextLink });
parentHas = true;
}
const goNext = maxLevel ? currenLevel < maxLevel : true;
if (goNext)
node.items?.forEach((item) => {
const innerRes = helperSearch(
query,
item,
nextLink,
currenLevel + 1,
maxLevel
);
if (!!innerRes.length && !parentHas && !node.noLink) {
res.push({ ...node, items: undefined, href: nextLink });
parentHas = true;
}
res.push(...innerRes);
});
return res;
}
export function advanceSearch(query: string) {
return ROUTES.map((node) =>
helperSearch(query, node, "", 1, query.length == 0 ? 2 : undefined)
).flat();
}
// Thursday, May 23, 2024
export function formatDate(dateStr: string): string {
const [day, month, year] = dateStr.split("-").map(Number);
const date = new Date(year, month - 1, day);
const options: Intl.DateTimeFormatOptions = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
return date.toLocaleDateString("en-US", options);
}
// May 23, 2024
export function formatDate2(dateStr: string): string {
const [day, month, year] = dateStr.split("-").map(Number);
const date = new Date(year, month - 1, day);
const options: Intl.DateTimeFormatOptions = {
month: "short",
day: "numeric",
year: "numeric",
};
return date.toLocaleDateString("en-US", options);
}
export function stringToDate(date: string) {
const [day, month, year] = date.split("-").map(Number);
return new Date(year, month - 1, day);
}