-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
utils.js
118 lines (102 loc) · 2.4 KB
/
utils.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { bold, red } from "./deps/colors.js";
import { SEP } from "./deps/path.js";
export async function concurrent(iterable, iteratorFn, limit = 200) {
const executing = [];
for await (const item of iterable) {
const p = iteratorFn(item).then(() =>
executing.splice(executing.indexOf(p), 1)
);
executing.push(p);
if (executing.length >= limit) {
await Promise.race(executing);
}
}
await Promise.all(executing);
}
export const mimes = new Map([
[".html", "text/html; charset=utf-8"],
[".txt", "text/plain; charset=utf-8"],
[".js", "text/javascript; charset=utf-8"],
[".css", "text/css; charset=utf-8"],
[".json", "application/json; charset=utf-8"],
[".ico", "image/x-icon"],
[".png", "image/png"],
[".jpg", "image/jpg"],
[".jpeg", "image/jpg"],
[".gif", "image/gif"],
[".svg", "image/svg+xml"],
[".mp3", "audio/mpeg"],
[".mp4", "video/mpeg"],
[".xml", "text/xml"],
[".woff", "font/woff"],
[".woff2", "font/woff2"],
[".wasm", "application/wasm"],
[".webp", "image/webp"],
[".webm", "video/webm"],
[".zip", "application/zip"],
]);
export function error(context, message, exception) {
console.error(bold(red(`${context}:`)), message);
if (exception) {
console.error(exception);
}
console.error("");
}
export function merge(defaults, user) {
const merged = { ...defaults };
if (!user) {
return merged;
}
for (const [key, value] of Object.entries(user)) {
if (
typeof merged[key] === "object" && typeof value === "object" &&
!Array.isArray(merged[key])
) {
merged[key] = merge(merged[key], value);
continue;
}
merged[key] = value;
}
return merged;
}
export function normalizePath(path) {
if (SEP !== "/") {
return path.replaceAll(SEP, "/");
}
return path;
}
export const slugChars = new Map([
["á", "a"],
["à", "a"],
["â", "a"],
["ã", "a"],
["ä", "a"],
["å", "a"],
["é", "e"],
["è", "e"],
["ê", "e"],
["ë", "e"],
["ì", "i"],
["í", "i"],
["î", "i"],
["ï", "i"],
["ò", "o"],
["ó", "o"],
["ô", "o"],
["õ", "o"],
["ö", "o"],
["ð", "d"],
["ù", "u"],
["ú", "u"],
["ü", "u"],
["û", "u"],
["ñ", "n"],
["ª", "a"],
["º", "o"],
["ç", "c"],
]);
export function slugify(string) {
return string.toLowerCase()
.replaceAll(/[\s-_]+/g, () => "-")
.replaceAll(/[^\w-\/]/g, (char) => slugChars.get(char) || "");
}