-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathutils.ts
97 lines (84 loc) · 2.52 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { StateType } from './consts';
import { Torrent } from './types';
const dlState = ['downloading', 'metaDL', 'stalledDL', 'checkingDL', 'pausedDL', 'queuedDL', 'forcedDL', 'allocating'];
const upState = ['uploading', 'stalledUP', 'checkingUP', 'queuedUP', 'forcedUP'];
const completeState = ['uploading', 'stalledUP', 'checkingUP', 'pausedUP', 'queuedUP', 'forcedUP'];
const activeState = ['metaDL', 'downloading', 'forcedDL', 'uploading', 'forcedUP', 'moving'];
const errorState = ['error', 'missingFiles'];
export function torrentIsState(type: StateType, state: string) {
let result;
switch (type) {
case StateType.Downloading: {
result = dlState.includes(state);
break;
}
case StateType.Seeding: {
result = upState.includes(state);
break;
}
case StateType.Completed: {
result = completeState.includes(state);
break;
}
case StateType.Resumed:
case StateType.Paused: {
const paused = state.startsWith('paused');
result = type === StateType.Paused ? paused : !paused;
break;
}
case StateType.Active:
case StateType.Inactive: {
const active = activeState.includes(state);
result = type === StateType.Active ? active : !active;
break;
}
case StateType.Errored: {
result = errorState.includes(state);
break;
}
default:
throw Error('Invalid type');
}
return result;
}
export function timeout(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* @deprecated renamed to `timeout`
*/
export const sleep = timeout;
export function codeToFlag(code: string) {
const magicNumber = 0x1F1A5;
// eslint-disable-next-line
code = code.toUpperCase();
const codePoints = [...code].map(c => magicNumber + c.charCodeAt(0));
const char = String.fromCodePoint(...codePoints);
const url = 'https://cdn.jsdelivr.net/npm/twemoji/2/svg/'
+ `${codePoints[0].toString(16)}-${codePoints[1].toString(16)}.svg`;
return {
char,
url,
};
}
export const isWindows = navigator.userAgent.includes('Windows');
export function findSameNamedTorrents(allTorrents: Torrent[], torrents: Torrent[]) {
const hashes = torrents.map(t => t.hash);
const result = [];
for (const t1 of torrents) {
for (const t2 of allTorrents) {
if (hashes.includes(t2.hash)) {
continue;
}
if (t1.name !== t2.name) {
continue;
}
result.push(t2);
hashes.push(t2.hash);
}
}
return result;
}
export function typed<T>(value: T): T {
return value;
}