-
Notifications
You must be signed in to change notification settings - Fork 784
/
globals.js
126 lines (120 loc) 路 3.67 KB
/
globals.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// We don't use global-this-polyfill [1], because it modifies
// the globals scope by default. QUnit must not affect the host context
// as developers may test their project may be such a polyfill, and/or
// they may intentionally test their project with and without certain
// polyfills and we must not affect that. It also uses an obscure
// mechanism that seems to sometimes causes a runtime error in older
// browsers (specifically Safari and IE versions that support
// Object.defineProperty but then report _T_ as undefined).
// [1] https://github.com/ungap/global-this/blob/v0.4.4/esm/index.js
//
// Another way is `Function('return this')()`, but doing so relies
// on eval which will cause a CSP error on some servers.
//
// Instead, simply check the four options that already exist
// in all supported environments.
function getGlobalThis () {
if (typeof globalThis !== 'undefined') {
// For SpiderMonkey, modern browsers, and recent Node.js
// eslint-disable-next-line no-undef
return globalThis;
}
if (typeof self !== 'undefined') {
// For web workers
// eslint-disable-next-line no-undef
return self;
}
if (typeof window !== 'undefined') {
// For document context in browsers
return window;
}
if (typeof global !== 'undefined') {
// For Node.js
// eslint-disable-next-line no-undef
return global;
}
throw new Error('Unable to locate global object');
}
// This avoids a simple `export const` assignment as that would lead Rollup
// to change getGlobalThis and use the same (generated) variable name there.
const g = getGlobalThis();
export { g as globalThis };
export const window = g.window;
export const console = g.console;
export const setTimeout = g.setTimeout;
export const clearTimeout = g.clearTimeout;
export const document = window && window.document;
export const navigator = window && window.navigator;
export const localSessionStorage = (function () {
const x = 'qunit-test-string';
try {
g.sessionStorage.setItem(x, x);
g.sessionStorage.removeItem(x);
return g.sessionStorage;
} catch (e) {
return undefined;
}
}());
// Basic fallback for ES6 Map
// Support: IE 9-10, Safari 7, PhantomJS; Map is undefined
// Support: iOS 8; `new Map(iterable)` is not supported
//
// Fallback for ES7 Map#keys
// Support: IE 11; Map#keys is undefined
export const StringMap = typeof g.Map === 'function' &&
typeof g.Map.prototype.keys === 'function' &&
typeof g.Symbol === 'function' &&
typeof g.Symbol.iterator === 'symbol'
? g.Map
: function StringMap (input) {
let store = Object.create(null);
let hasOwn = Object.prototype.hasOwnProperty;
this.has = function (strKey) {
return hasOwn.call(store, strKey);
};
this.get = function (strKey) {
return store[strKey];
};
this.set = function (strKey, val) {
if (!hasOwn.call(store, strKey)) {
this.size++;
}
store[strKey] = val;
return this;
};
this.delete = function (strKey) {
if (hasOwn.call(store, strKey)) {
delete store[strKey];
this.size--;
}
};
this.forEach = function (callback) {
for (let strKey in store) {
callback(store[strKey], strKey);
}
};
this.keys = function () {
return Object.keys(store);
};
this.clear = function () {
store = Object.create(null);
this.size = 0;
};
this.size = 0;
if (input) {
input.forEach((val, strKey) => {
this.set(strKey, val);
});
}
};
export const StringSet = window.Set || function () {
const set = Object.create(null);
return {
add (value) {
set[value] = true;
},
has (value) {
return value in set;
}
};
};