-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
56 lines (49 loc) · 1.69 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
export const noop = () => {};
export const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
export function escapeTextForHTMLContent(s: string) {
return s.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function findNdx(files: any, endings: string[]) {
// calling toLowerCase a bunch is bad but there will never be more than a few files
for (const ending of endings) {
const ndx = files.findIndex((file: any) => file.name.toLowerCase().endsWith(ending.toLowerCase()));
if (ndx >= 0) {
return ndx;
}
}
return -1;
}
export function getOrFindNdx(files: any[], name: string, ...endings: string[]) {
const ndx = files.findIndex(f => f.name.toLowerCase() === name.toLowerCase);
if (ndx >= 0) {
return ndx;
}
return findNdx(files, endings);
}
export function getOrFind(files: any[], ...args: string[]) {
const ndx = getOrFindNdx(files, '', ...args);
return ndx >= 0 ? files[ndx] : '';
}
const stringify = (options: any, sep: string = ',') => Object.entries(options).map(([key, value]) => `${key}=${value}`).join(sep);
export function createPopup(url: string, config: any = {}): (Window | undefined) {
const width = config.width || 500;
const height = config.height || 500;
const options = {
width: width,
height: height,
top: window.screenY + ((window.outerHeight - height) / 2.5),
left: window.screenX + ((window.outerWidth - width) / 2)
};
return window.open(url, '_blank', stringify(options, ',')) || undefined;
}
export function once(fn: (...args: any[]) => void) {
return function (...args: any[]) {
let once = false;
if (!once) {
once = true;
fn(...args);
}
};
}