-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.ts
97 lines (90 loc) · 1.98 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
/*
* validates the url
* @param string var - the url we want to scrape
*/
export function isUrlValid(url: string | any[]) {
// todo check image urls
return true;
return typeof url === 'string' && url.length > 0 && url.indexOf('http') === 0 && url.indexOf(' ') === -1;
}
/*
* forces url to start with http://
* @param string var - the url we want to scrape
*/
/*
* validate timeout - how long should we wait for a request
* @param number var - the time we want to wait
*/
/*
* findImageTypeFromUrl
* @param string url - image url
*/
export function findImageTypeFromUrl(url: string) {
let type = url.split('.').pop();
[type] = type.split('?');
return type;
}
/*
* isImageTypeValid
* @param string type - image type
*/
export function isImageTypeValid(type: string) {
const validImageTypes = ['apng', 'bmp', 'gif', 'ico', 'cur', 'jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', 'svg', 'tif', 'tiff', 'webp'];
return validImageTypes.includes(type);
}
/*
* isThisANonHTMLPage
* @param string url - url of site
*/
export function isThisANonHTMLUrl(url: any) {
const invalidImageTypes = [
'.doc',
'.docx',
'.xls',
'.xlsx',
'.ppt',
'.pptx',
'.3gp',
'.avi',
'.mov',
'.mp4',
'.m4v',
'.m4a',
'.mp3',
'.mkv',
'.ogv',
'.ogm',
'.ogg',
'.oga',
'.webm',
'.wav',
'.bmp',
'.gif',
'.jpg',
'.jpeg',
'.png',
'.webp',
'.zip',
'.rar',
'.tar',
'.tar.gz',
'.tgz',
'.tar.bz2',
'.tbz2',
'.txt',
'.pdf',
];
const extension = findImageTypeFromUrl(url);
return invalidImageTypes.some((type) => `.${extension}`.includes(type));
}
/*
* removeNestedUndefinedValues
* @param object - an object
*/
export function removeNestedUndefinedValues(object: any) {
Object.entries(object).forEach(([key, value]) => {
if (value && typeof value === 'object') removeNestedUndefinedValues(value);
else if (value === undefined) delete object[key];
});
return object;
}