-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathglobal.js
More file actions
36 lines (34 loc) · 892 Bytes
/
global.js
File metadata and controls
36 lines (34 loc) · 892 Bytes
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
import { window } from './1.variables.js';
import { debug } from './debug.js';
import hasOwn from './hasOwn.js';
export function global(...key) {
const {
throws = true,
} = typeof key[key.length - 1] === 'object' ? key.pop() : {};
const found = key.find((e) => hasOwn(window, e));
if (found === undefined) {
const msg = `[${key.join(',')}] does not exist`;
if (throws) throw new Error(msg);
return debug(msg);
}
return window[found];
}
export function globalSet(key, value, {
throws = true,
} = {}) {
if (!hasOwn(window, key)) {
const msg = `[${key}] does not exist`;
if (throws) throw new Error(msg);
return debug(msg);
}
const original = window[key];
if (typeof value === 'function') {
const wrapper = {
super: original,
};
window[key] = value.bind(wrapper);
} else {
window[key] = value;
}
return original;
}