composite-key.js
1.37 KB
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
// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
var Map = require('../modules/es.map');
var WeakMap = require('../modules/es.weak-map');
var create = require('../internals/object-create');
var isObject = require('../internals/is-object');
var Node = function () {
// keys
this.object = null;
this.symbol = null;
// child nodes
this.primitives = null;
this.objectsByIndex = create(null);
};
Node.prototype.get = function (key, initializer) {
return this[key] || (this[key] = initializer());
};
Node.prototype.next = function (i, it, IS_OBJECT) {
var store = IS_OBJECT
? this.objectsByIndex[i] || (this.objectsByIndex[i] = new WeakMap())
: this.primitives || (this.primitives = new Map());
var entry = store.get(it);
if (!entry) store.set(it, entry = new Node());
return entry;
};
var root = new Node();
module.exports = function () {
var active = root;
var length = arguments.length;
var i, it;
// for prevent leaking, start from objects
for (i = 0; i < length; i++) {
if (isObject(it = arguments[i])) active = active.next(i, it, true);
}
if (this === Object && active === root) throw TypeError('Composite keys must contain a non-primitive component');
for (i = 0; i < length; i++) {
if (!isObject(it = arguments[i])) active = active.next(i, it, false);
} return active;
};