-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
28 lines (24 loc) · 810 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
import fileTypeFromBuffer from "magic-bytes.js";
export function isSSR() {
return typeof window === "undefined";
}
export const makePairs = <T>(array: T[]) => {
const direct = array.flatMap((v, i) => array.slice(i + 1).map((w) => [v, w]));
const reversed = array.flatMap((v, i) =>
array.slice(i + 1).map((w) => [w, v])
);
return [...direct, ...reversed];
};
function getExtFromName(name: string) {
const result = /(?:\.([^.]+))?$/.exec(name);
if (!result) return "";
return result[1];
}
export async function getFileExtenstion(file: File) {
let type: string;
const magic = await file.slice(0, 16).arrayBuffer();
const [result] = fileTypeFromBuffer(new Uint8Array(magic));
if (result?.extension) type = result.extension;
else type = getExtFromName(file.name);
return type;
}