collection.js 3.0 KB

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