-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfiletype.test.ts
64 lines (54 loc) · 2.29 KB
/
filetype.test.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
import { deepStrictEqual, ok, strictEqual } from "node:assert";
import { UTIMap } from "./filetype/constants.ts";
import { getExtensions, getMIME, getUTI } from "./filetype.ts";
describe("filetype", () => {
it("getUTI", () => {
strictEqual(getUTI(".png"), "public.png");
strictEqual(getUTI("*.png"), "public.png");
strictEqual(getUTI("public.png"), "public.png");
strictEqual(getUTI("image/png"), "public.png");
for (const [uti, values] of Object.entries(UTIMap)) {
values.forEach(value => {
strictEqual(getUTI(value), uti);
});
}
});
it("getMIME", () => {
strictEqual(getMIME(".png"), "image/png");
strictEqual(getMIME("*.png"), "image/png");
strictEqual(getMIME("public.png"), "image/png");
strictEqual(getMIME("image/png"), "image/png");
for (const [uti, values] of Object.entries(UTIMap)) {
const mime = getMIME(uti);
if (values.some(value => value.includes("/"))) {
ok(values.some(value => value === mime));
}
}
});
it("getExtensions", () => {
strictEqual(getExtensions(".png").join(","), ".png");
strictEqual(getExtensions("*.png").join(","), ".png");
strictEqual(getExtensions("image/png").join(","), ".png");
strictEqual(getExtensions("public.png").join(","), ".png");
for (const [uti, values] of Object.entries(UTIMap)) {
const extensions = getExtensions(uti).join(",");
strictEqual(extensions, values.filter(v => v[0] === ".").join(","));
}
for (const values of Object.values(UTIMap)) {
const mime = values.find(v => v.includes("/") && !v.endsWith("/*"));
if (mime) {
const extensions = getExtensions(mime).join(",");
strictEqual(extensions, values.filter(v => v[0] === ".").join(","));
}
}
deepStrictEqual(
getExtensions("image/*").sort().join(","),
Object.values(UTIMap)
.filter(values => values.some(v => v !== "image/*" && v.startsWith("image/")))
.map(values => values.filter(v => v[0] === "."))
.flat()
.sort()
.join(","),
);
});
});