-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
40 lines (35 loc) · 998 Bytes
/
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
export function trimEndSlash(str: string) {
if (str.endsWith("/") || str.endsWith("\\")) return str.slice(0, -1);
return str;
}
export function trimStartDot(str: string) {
if (str.startsWith("./")) return str.slice(2);
return str;
}
export function dirname(str: string) {
const parts = str.split(/[\/\\]/g);
return parts[parts.length - 2];
}
export function dirpathparts(str: string) {
const parts = str.split(/[\/\\]/g);
return parts.slice(0, parts.length - 1);
}
export function join(...parts: string[]) {
return parts.map(trimEndSlash).map(trimStartDot).join("/");
}
export function groupBy<TElement, TKey>(
elements: TElement[],
keyGetter: (element: TElement) => TKey,
): Map<TKey, TElement[]> {
const map = new Map<TKey, TElement[]>();
for (const element of elements) {
const key = keyGetter(element);
if (map.has(key)) {
const group = map.get(key);
group?.push(element);
} else {
map.set(key, [element]);
}
}
return map;
}