utils.ts
⎇
Raw
1export function sleep(ms: number) {
2 return new Promise((resolve) => setTimeout(resolve, ms));
3}
4
5//join string and dedup slashes and make sure it doesn't start with a slash
6export function joinPath(...paths: string[]): string {
7 return paths.join("/").replace(/\/+/g, "/").replace(/^\//, "");
8}
9
10export function formatTime(seconds: number): string {
11 const mins = Math.floor(seconds / 60);
12 const secs = Math.floor(seconds % 60);
13 return `${mins}:${secs < 10 ? "0" : ""}${secs}`;
14}
15
16export const LocalStorageValues = {
17 options: "Options",
18 volume: "Volume",
19 playlist: "Playlist",
20 shuffle: "Shuffle",
21 repeat: "Repeat",
22 offline: "Offline",
23 path: "Path"
24};
25
26export function generateUUID() {
27 return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
28 const r = (Math.random() * 16) | 0;
29 const v = c === "x" ? r : (r & 0x3) | 0x8;
30 return v.toString(16);
31 });
32}
33
34
35export function formatBytes(bytes: number): string {
36 if (bytes === 0) return '0 Bytes';
37
38 const k = 1024;
39 const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
40 const i = Math.floor(Math.log(bytes) / Math.log(k));
41 const value = bytes / k ** i;
42
43 return `${Number.parseFloat(value.toFixed(2))} ${sizes[i]}`;
44 }