-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (55 loc) · 1.41 KB
/
index.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
(() => {
const keyToObjectMap = new Map();
const createKey = () => {
let key = Symbol(`metaproperties.key`);
keyToObjectMap.set(key, new WeakMap());
return key;
};
const defaultMap = new WeakMap();
function createVars (map, object, value) {
try {
map.set(object, value);
} catch (error) {
throw new Error(`invalid target`);
}
}
let lastObject,
lastKey,
lastVars;
const meta = (object, key) => {
if (object === lastObject && key === lastKey) {
return lastVars;
}
const map = (key !== undefined) ? (keyToObjectMap.get(key)) : (defaultMap);
if (!map) {
console.log(map);
throw new Error(`unknown \`key\``);
}
let vars = map.get(object);
if (vars === undefined) {
createVars(map, object, {});
}
lastObject = object;
lastKey = key;
lastVars = map.get(object);
return lastVars;
};
meta.createKey = createKey;
if (module && module.exports) {
module.exports = meta;
return;
}
let _global =
(typeof self === 'object' && self.self === self && self) ||
(typeof global === 'object' && global.global === global && global) ||
this;
let existingDefinition;
if (existingDefinition = (_global ? _global.meta : meta)) {
Object.defineProperty(meta, 'existingDefinition', {
get () {
return existingDefinition;
}
});
}
_global.meta = meta;
})();