composite-key.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
  2. var Map = require('../modules/es.map');
  3. var WeakMap = require('../modules/es.weak-map');
  4. var create = require('../internals/object-create');
  5. var isObject = require('../internals/is-object');
  6. var Node = function () {
  7. // keys
  8. this.object = null;
  9. this.symbol = null;
  10. // child nodes
  11. this.primitives = null;
  12. this.objectsByIndex = create(null);
  13. };
  14. Node.prototype.get = function (key, initializer) {
  15. return this[key] || (this[key] = initializer());
  16. };
  17. Node.prototype.next = function (i, it, IS_OBJECT) {
  18. var store = IS_OBJECT
  19. ? this.objectsByIndex[i] || (this.objectsByIndex[i] = new WeakMap())
  20. : this.primitives || (this.primitives = new Map());
  21. var entry = store.get(it);
  22. if (!entry) store.set(it, entry = new Node());
  23. return entry;
  24. };
  25. var root = new Node();
  26. module.exports = function () {
  27. var active = root;
  28. var length = arguments.length;
  29. var i, it;
  30. // for prevent leaking, start from objects
  31. for (i = 0; i < length; i++) {
  32. if (isObject(it = arguments[i])) active = active.next(i, it, true);
  33. }
  34. if (this === Object && active === root) throw TypeError('Composite keys must contain a non-primitive component');
  35. for (i = 0; i < length; i++) {
  36. if (!isObject(it = arguments[i])) active = active.next(i, it, false);
  37. } return active;
  38. };