composite-key.js 1.5 KB

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