collection.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var globalThis = require('../internals/global-this');
  4. var InternalMetadataModule = require('../internals/internal-metadata');
  5. var fails = require('../internals/fails');
  6. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  7. var iterate = require('../internals/iterate');
  8. var anInstance = require('../internals/an-instance');
  9. var isCallable = require('../internals/is-callable');
  10. var isObject = require('../internals/is-object');
  11. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  12. var setToStringTag = require('../internals/set-to-string-tag');
  13. var defineProperty = require('../internals/object-define-property').f;
  14. var forEach = require('../internals/array-iteration').forEach;
  15. var DESCRIPTORS = require('../internals/descriptors');
  16. var InternalStateModule = require('../internals/internal-state');
  17. var setInternalState = InternalStateModule.set;
  18. var internalStateGetterFor = InternalStateModule.getterFor;
  19. module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
  20. var IS_MAP = CONSTRUCTOR_NAME.indexOf('Map') !== -1;
  21. var IS_WEAK = CONSTRUCTOR_NAME.indexOf('Weak') !== -1;
  22. var ADDER = IS_MAP ? 'set' : 'add';
  23. var NativeConstructor = globalThis[CONSTRUCTOR_NAME];
  24. var NativePrototype = NativeConstructor && NativeConstructor.prototype;
  25. var exported = {};
  26. var Constructor;
  27. if (!DESCRIPTORS || !isCallable(NativeConstructor)
  28. || !(IS_WEAK || NativePrototype.forEach && !fails(function () { new NativeConstructor().entries().next(); }))
  29. ) {
  30. // create collection constructor
  31. Constructor = common.getConstructor(wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER);
  32. InternalMetadataModule.enable();
  33. } else {
  34. Constructor = wrapper(function (target, iterable) {
  35. setInternalState(anInstance(target, Prototype), {
  36. type: CONSTRUCTOR_NAME,
  37. collection: new NativeConstructor()
  38. });
  39. if (!isNullOrUndefined(iterable)) iterate(iterable, target[ADDER], { that: target, AS_ENTRIES: IS_MAP });
  40. });
  41. var Prototype = Constructor.prototype;
  42. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  43. forEach(['add', 'clear', 'delete', 'forEach', 'get', 'has', 'set', 'keys', 'values', 'entries'], function (KEY) {
  44. var IS_ADDER = KEY === 'add' || KEY === 'set';
  45. if (KEY in NativePrototype && !(IS_WEAK && KEY === 'clear')) {
  46. createNonEnumerableProperty(Prototype, KEY, function (a, b) {
  47. var collection = getInternalState(this).collection;
  48. if (!IS_ADDER && IS_WEAK && !isObject(a)) return KEY === 'get' ? undefined : false;
  49. var result = collection[KEY](a === 0 ? 0 : a, b);
  50. return IS_ADDER ? this : result;
  51. });
  52. }
  53. });
  54. IS_WEAK || defineProperty(Prototype, 'size', {
  55. configurable: true,
  56. get: function () {
  57. return getInternalState(this).collection.size;
  58. }
  59. });
  60. }
  61. setToStringTag(Constructor, CONSTRUCTOR_NAME, false, true);
  62. exported[CONSTRUCTOR_NAME] = Constructor;
  63. $({ global: true, forced: true }, exported);
  64. if (!IS_WEAK) common.setStrong(Constructor, CONSTRUCTOR_NAME, IS_MAP);
  65. return Constructor;
  66. };