UNPKG

1.57 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 create = require('../internals/object-create');
7var isObject = require('../internals/is-object');
8
9var $Object = Object;
10var $TypeError = TypeError;
11var Map = getBuiltIn('Map');
12var WeakMap = getBuiltIn('WeakMap');
13
14var Node = function () {
15 // keys
16 this.object = null;
17 this.symbol = null;
18 // child nodes
19 this.primitives = null;
20 this.objectsByIndex = create(null);
21};
22
23Node.prototype.get = function (key, initializer) {
24 return this[key] || (this[key] = initializer());
25};
26
27Node.prototype.next = function (i, it, IS_OBJECT) {
28 var store = IS_OBJECT
29 ? this.objectsByIndex[i] || (this.objectsByIndex[i] = new WeakMap())
30 : this.primitives || (this.primitives = new Map());
31 var entry = store.get(it);
32 if (!entry) store.set(it, entry = new Node());
33 return entry;
34};
35
36var root = new Node();
37
38module.exports = function () {
39 var active = root;
40 var length = arguments.length;
41 var i, it;
42 // for prevent leaking, start from objects
43 for (i = 0; i < length; i++) {
44 if (isObject(it = arguments[i])) active = active.next(i, it, true);
45 }
46 if (this === $Object && active === root) throw new $TypeError('Composite keys must contain a non-primitive component');
47 for (i = 0; i < length; i++) {
48 if (!isObject(it = arguments[i])) active = active.next(i, it, false);
49 } return active;
50};