-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathcheck-types.js
57 lines (39 loc) · 1.28 KB
/
check-types.js
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
// Universal Imports Only
export function isString( maybeString ) {
return (typeof maybeString === 'string' || maybeString instanceof String)
}
export function isNonEmptyString ( maybeString ) {
if ( !isString( maybeString ) ) return false
return maybeString.length > 0
}
export function isNonEmptyArray ( maybeArray ) {
if ( !Array.isArray( maybeArray ) ) return false
return maybeArray.length > 0
}
export function isPositiveNumberString ( maybeNumber ) {
if ( !isString( maybeNumber ) ) return false
return /\d+$/.test( maybeNumber )
}
export function isValidHttpUrl( maybeUrl, allowUnsecure = false ) {
if ( !isString( maybeUrl ) ) return false
let url
try {
url = new URL(maybeUrl)
} catch (_) {
return false
}
if ( allowUnsecure ) {
return url.protocol === "http:" || url.protocol === "https:"
}
return url.protocol === "https:"
}
export function isValidImageUrl ( maybeUrl ) {
if ( !isValidHttpUrl( maybeUrl ) ) return false
// Check if url has a file extension
const url = new URL(maybeUrl)
const fileExtension = url.pathname.split('.').pop()
return isNonEmptyString( fileExtension )
}
export function isObject( maybeObject ) {
return maybeObject === Object( maybeObject )
}