forked from denoland/docland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
180 lines (164 loc) · 5.03 KB
/
util.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2021 the Deno authors. All rights reserved. MIT license.
import { removeMarkdown } from "./deps.ts";
export function assert(cond: unknown, msg = "Assertion failed"): asserts cond {
if (!cond) {
throw new Error(msg);
}
}
/**
* @param bytes Number of bytes
* @param si If `true` use metric (SI) unites (powers of 1000). If `false` use
* binary (IEC) (powers of 1024). Defaults to `true`.
* @param dp Number of decimal places to display. Defaults to `1`.
*/
export function humanSize(bytes: number, si = true, dp = 1) {
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = si
? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
: ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
let u = -1;
const r = 10 ** dp;
do {
bytes /= thresh;
++u;
} while (
Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1
);
return `${bytes.toFixed(dp)} ${units[u]}`;
}
export function getBody(
{ body, head, footer }: {
body: string;
head: string[];
footer: string[];
},
styles: string,
): string {
return `<!DOCTYPE html>
<html lang="en">
<head>
${styles}
${head.join("\n")}
</head>
<body>
${body}
${footer.join("\n")}
</body>
</html>`;
}
export function isEven(n: number) {
return !(n % 2);
}
export type Child<T> = T | [T];
/** A utility function that inspects a value, and if the value is an array,
* returns the first element of the array, otherwise returns the value. This is
* used to deal with the ambiguity around children properties with nano_jsx. */
export function take<T>(value: Child<T>, itemIsArray = false): T {
if (itemIsArray) {
return Array.isArray(value) && Array.isArray(value[0])
? value[0] // deno-lint-ignore no-explicit-any
: value as any;
} else {
return Array.isArray(value) ? value[0] : value;
}
}
/** Patterns of "registries" which will be parsed to be displayed in a more
* human readable way. */
const patterns = {
"deno.land/x": [
new URLPattern(
"https://deno.land/x/:pkg([^@/]+){@}?:ver?/:mod*",
),
],
"deno.land/std": [new URLPattern("https://deno.land/std{@}?:ver?/:mod*")],
"nest.land": [new URLPattern("https://x.nest.land/:pkg([^@/]+)@:ver/:mod*")],
"crux.land": [new URLPattern("https://crux.land/:pkg([^@/]+)@:ver")],
"github.com": [
new URLPattern(
"https://raw.githubusercontent.com/:org/:pkg/:ver/:mod*",
),
// https://github.com/denoland/deno_std/raw/main/http/mod.ts
new URLPattern(
"https://github.com/:org/:pkg/raw/:ver/:mod*",
),
],
"gist.github.com": [
new URLPattern(
"https://gist.githubusercontent.com/:org/:pkg/raw/:ver/:mod*",
),
],
"esm.sh": [
new URLPattern(
"http{s}?://esm.sh/:org(@[^/]+)?/:pkg([^@/]+){@}?:ver?/:mod?",
),
// https://cdn.esm.sh/v58/[email protected]/database/dist/database/index.d.ts
new URLPattern(
"http{s}?://cdn.esm.sh/:regver*/:org(@[^/]+)?/:pkg([^@/]+)@:ver/:mod*",
),
],
"skypack.dev": [
new URLPattern({
protocol: "https",
hostname: "cdn.skypack.dev",
pathname: "/:org(@[^/]+)?/:pkg([^@/]+){@}?:ver?/:mod?",
search: "*",
}),
// https://cdn.skypack.dev/-/@firebase/[email protected]/dist=es2019,mode=types/dist/index.d.ts
new URLPattern(
"https://cdn.skypack.dev/-/:org(@[^/]+)?/:pkg([^@/]+)@:ver([^-]+):hash/:path*",
),
],
"unpkg.com": [
new URLPattern(
"https://unpkg.com/:org(@[^/]+)?/:pkg([^@/]+){@}?:ver?/:mod?",
),
],
};
interface ParsedURL {
registry: string;
org?: string;
package?: string;
version?: string;
module?: string;
}
/** Take a string URL and attempt to pattern match it against a known registry
* and returned the parsed structure. */
export function parseURL(url: string): ParsedURL | undefined {
for (const [registry, pattern] of Object.entries(patterns)) {
for (const pat of pattern) {
const match = pat.exec(url);
if (match) {
let { pathname: { groups: { regver, org, pkg, ver, mod } } } = match;
if (registry === "gist.github.com") {
pkg = pkg.substring(0, 7);
ver = ver.substring(0, 7);
}
return {
registry: regver ? `${registry} @ ${regver}` : registry,
org: org ? org : undefined,
package: pkg ? pkg : undefined,
version: ver ? ver : undefined,
module: mod ? mod : undefined,
};
}
}
}
}
/** Convert a string into a camelCased string. */
export function camelize(str: string): string {
return str.split(/[\s_\-]+/).map((word, index) =>
index === 0
? word.toLowerCase()
: `${word.charAt(0).toUpperCase()}${word.slice(1).toLowerCase()}`
).join("");
}
/** Clean up markdown text, converting it into something that can be displayed
* just as "normal" text. */
export function cleanMarkdown(markdown: string): string {
return removeMarkdown(markdown).split("\n\n").map((l) =>
l.replaceAll("\n", " ")
).join("\n\n");
}