forked from iddan/kupot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
45 lines (40 loc) · 1.13 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
import * as xlsx from "xlsx";
export function exportToCsv<R, SR>(data: string[][], fileName: string) {
const content = data.map((cells) => cells.join(",")).join("\n");
downloadFile(
fileName,
new Blob([content], { type: "text/csv;charset=utf-8;" })
);
}
export function exportToXlsx<R, SR>(data: string[][], fileName: string) {
const wb = xlsx.utils.book_new();
const ws = xlsx.utils.aoa_to_sheet(data);
xlsx.utils.book_append_sheet(wb, ws, "Sheet 1");
xlsx.writeFile(wb, fileName);
}
export function downloadFile(fileName: string, data: Blob) {
const downloadLink = document.createElement("a");
downloadLink.download = fileName;
const url = URL.createObjectURL(data);
downloadLink.href = url;
downloadLink.click();
URL.revokeObjectURL(url);
}
export function orderBy<
SortKey extends string,
Item extends Record<SortKey, unknown>
>(array: Item[], key: SortKey, dir: "asc" | "desc"): Item[] {
return array
.concat()
.sort((a, b) =>
a[key] > b[key]
? dir === "asc"
? 1
: -1
: b[key] > a[key]
? dir === "desc"
? -1
: 1
: 0
);
}