debian-mirror-gitlab/core-js/internals/composite-key.js
2019-09-25 13:30:05 +05:30

43 lines
1.4 KiB
JavaScript

// 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;
};