UNPKG

2.1 kBJavaScriptView Raw
1'use strict';
2// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
3require('../modules/es.map');
4require('../modules/es.weak-map');
5var getBuiltIn = require('../internals/get-built-in');
6var uncurryThis = require('../internals/function-uncurry-this');
7var shared = require('../internals/shared');
8
9var Map = getBuiltIn('Map');
10var WeakMap = getBuiltIn('WeakMap');
11var push = uncurryThis([].push);
12
13var metadata = shared('metadata');
14var store = metadata.store || (metadata.store = new WeakMap());
15
16var getOrCreateMetadataMap = function (target, targetKey, create) {
17 var targetMetadata = store.get(target);
18 if (!targetMetadata) {
19 if (!create) return;
20 store.set(target, targetMetadata = new Map());
21 }
22 var keyMetadata = targetMetadata.get(targetKey);
23 if (!keyMetadata) {
24 if (!create) return;
25 targetMetadata.set(targetKey, keyMetadata = new Map());
26 } return keyMetadata;
27};
28
29var ordinaryHasOwnMetadata = function (MetadataKey, O, P) {
30 var metadataMap = getOrCreateMetadataMap(O, P, false);
31 return metadataMap === undefined ? false : metadataMap.has(MetadataKey);
32};
33
34var ordinaryGetOwnMetadata = function (MetadataKey, O, P) {
35 var metadataMap = getOrCreateMetadataMap(O, P, false);
36 return metadataMap === undefined ? undefined : metadataMap.get(MetadataKey);
37};
38
39var ordinaryDefineOwnMetadata = function (MetadataKey, MetadataValue, O, P) {
40 getOrCreateMetadataMap(O, P, true).set(MetadataKey, MetadataValue);
41};
42
43var ordinaryOwnMetadataKeys = function (target, targetKey) {
44 var metadataMap = getOrCreateMetadataMap(target, targetKey, false);
45 var keys = [];
46 if (metadataMap) metadataMap.forEach(function (_, key) { push(keys, key); });
47 return keys;
48};
49
50var toMetadataKey = function (it) {
51 return it === undefined || typeof it == 'symbol' ? it : String(it);
52};
53
54module.exports = {
55 store: store,
56 getMap: getOrCreateMetadataMap,
57 has: ordinaryHasOwnMetadata,
58 get: ordinaryGetOwnMetadata,
59 set: ordinaryDefineOwnMetadata,
60 keys: ordinaryOwnMetadataKeys,
61 toKey: toMetadataKey
62};