-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
52 lines (40 loc) · 1.33 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { FastIndexedDbFsController } from '~fs'
export function dec2hex(dec) {
return dec.toString(16).padStart(2, "0")
}
export function generateId(len) {
var arr = new Uint8Array((len || 40) / 2)
self.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
export async function getFs() {
const fs = new FastIndexedDbFsController("rime-files");
await fs.open();
return fs;
}
export function formatBytes(bytes, decimals = 1) {
if (!+bytes) return '0 Bytes'
const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}
export function getParentPath(path) {
const regex = /^(.*)\/[^/]*$/;
const match = regex.exec(path);
return match ? match[1] : "/";
}
export function getFileName(path) {
const regex = /\/([^/]+)$/;
const match = regex.exec(path);
return match ? match[1] : null;
}
export interface ImeSettings {
schema: string;
pageSize: number;
algebraList: string[];
horizontal?: boolean;
}
export const kDefaultSettings: ImeSettings = { schema: "aurora_pinyin", pageSize: 5, algebraList: [], horizontal: false };
export const $$ = chrome.i18n.getMessage;