forked from rocicorp/zero-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
88 lines (77 loc) · 2.3 KB
/
utils.ts
File metadata and controls
88 lines (77 loc) · 2.3 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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));
}
// Utility function to create slugs from text
export function sluggify(text: string) {
return text
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with dashes
.replace(/[^a-z0-9-]/g, ''); // Remove non-alphanumeric characters
}
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);
}