-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutils.js
More file actions
100 lines (76 loc) · 2.05 KB
/
utils.js
File metadata and controls
100 lines (76 loc) · 2.05 KB
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
98
99
100
const Utils = {
isBrowser: isBrowser(),
isLocalStorageSupported: isLocalStorageSupported(),
globalScope: (
(typeof self === 'object' && self.self === self && self) ||
(typeof global === 'object' && global.global === global && global) ||
(typeof globalThis === 'object' && globalThis)
),
castArray(value) {
if (Array.isArray(value)) {
return value
}
if (typeof value === 'undefined') {
return []
}
return [value]
},
isCustomClassInstance(item) {
if (!item || typeof item !== 'object' || Array.isArray(item)) {
return false
}
return item.constructor !== Object
},
getClassName(obj) {
if (obj && obj.className) {
return obj.className
}
if (typeof obj === 'function') {
if (obj.name) {
return obj.name
}
}
if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
if (obj.___class) {
return obj.___class
}
if (obj.constructor !== Object) {
return Utils.getClassName(obj.constructor)
}
}
return null
},
uuid() {
const chr4 = () => Math.random().toString(16).slice(-4).toUpperCase()
const chr8 = () => `${chr4()}${chr4()}`
const chr12 = () => `${chr4()}${chr4()}${chr4()}`
return `${chr8()}-${chr4()}-${chr4()}-${chr4()}-${chr12()}`
},
isObject(obj) {
return obj != null && obj.constructor.name === 'Object'
},
getWindowNavigator() {
if (typeof __test_navigator !== 'undefined') {
return __test_navigator
}
if (typeof navigator !== 'undefined') {
return navigator
}
return Utils.globalScope && Utils.globalScope.navigator
}
}
function isBrowser() {
return (typeof self === 'object' && self.self === self) && (typeof window === 'object' && window === self)
}
function isLocalStorageSupported() {
try {
if (isBrowser() && window.localStorage) {
localStorage.setItem('localStorageTest', true)
localStorage.removeItem('localStorageTest')
return true
}
} catch (e) {
}
return false
}
export default Utils