File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -20,14 +20,22 @@ export function isNodeEnv(): boolean {
20
20
return Object . prototype . toString . call ( typeof process !== 'undefined' ? process : 0 ) === '[object process]' ;
21
21
}
22
22
23
+ const fallbackGlobalObject = { } ;
24
+
23
25
/**
24
26
* Safely get global scope object
25
27
*
26
28
* @returns Global scope object
27
29
*/
28
30
// tslint:disable:strict-type-predicates
29
31
export function getGlobalObject ( ) : Window | NodeJS . Global | { } {
30
- return isNodeEnv ( ) ? global : typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : { } ;
32
+ return isNodeEnv ( )
33
+ ? global
34
+ : typeof window !== 'undefined'
35
+ ? window
36
+ : typeof self !== 'undefined'
37
+ ? self
38
+ : fallbackGlobalObject ;
31
39
}
32
40
// tslint:enable:strict-type-predicates
33
41
Original file line number Diff line number Diff line change 1
- import { getEventDescription } from '../src/misc' ;
1
+ import { getEventDescription , getGlobalObject } from '../src/misc' ;
2
2
3
3
describe ( 'getEventDescription()' , ( ) => {
4
4
test ( 'message event' , ( ) => {
@@ -108,3 +108,14 @@ describe('getEventDescription()', () => {
108
108
) . toEqual ( '<unknown>' ) ;
109
109
} ) ;
110
110
} ) ;
111
+
112
+ describe ( 'getGlobalObject()' , ( ) => {
113
+ test ( 'should return the same object' , ( ) => {
114
+ const backup = global . process ;
115
+ delete global . process ;
116
+ const first = getGlobalObject ( ) ;
117
+ const second = getGlobalObject ( ) ;
118
+ expect ( first ) . toEqual ( second ) ;
119
+ global . process = backup ;
120
+ } ) ;
121
+ } ) ;
You can’t perform that action at this time.
0 commit comments