collection.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var global = require('../internals/global');
  4. var isForced = require('../internals/is-forced');
  5. var redefine = require('../internals/redefine');
  6. var InternalMetadataModule = require('../internals/internal-metadata');
  7. var iterate = require('../internals/iterate');
  8. var anInstance = require('../internals/an-instance');
  9. var isObject = require('../internals/is-object');
  10. var fails = require('../internals/fails');
  11. var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');
  12. var setToStringTag = require('../internals/set-to-string-tag');
  13. var inheritIfRequired = require('../internals/inherit-if-required');
  14. module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
  15. var IS_MAP = CONSTRUCTOR_NAME.indexOf('Map') !== -1;
  16. var IS_WEAK = CONSTRUCTOR_NAME.indexOf('Weak') !== -1;
  17. var ADDER = IS_MAP ? 'set' : 'add';
  18. var NativeConstructor = global[CONSTRUCTOR_NAME];
  19. var NativePrototype = NativeConstructor && NativeConstructor.prototype;
  20. var Constructor = NativeConstructor;
  21. var exported = {};
  22. var fixMethod = function (KEY) {
  23. var nativeMethod = NativePrototype[KEY];
  24. redefine(NativePrototype, KEY,
  25. KEY == 'add' ? function add(value) {
  26. nativeMethod.call(this, value === 0 ? 0 : value);
  27. return this;
  28. } : KEY == 'delete' ? function (key) {
  29. return IS_WEAK && !isObject(key) ? false : nativeMethod.call(this, key === 0 ? 0 : key);
  30. } : KEY == 'get' ? function get(key) {
  31. return IS_WEAK && !isObject(key) ? undefined : nativeMethod.call(this, key === 0 ? 0 : key);
  32. } : KEY == 'has' ? function has(key) {
  33. return IS_WEAK && !isObject(key) ? false : nativeMethod.call(this, key === 0 ? 0 : key);
  34. } : function set(key, value) {
  35. nativeMethod.call(this, key === 0 ? 0 : key, value);
  36. return this;
  37. }
  38. );
  39. };
  40. // eslint-disable-next-line max-len
  41. if (isForced(CONSTRUCTOR_NAME, typeof NativeConstructor != 'function' || !(IS_WEAK || NativePrototype.forEach && !fails(function () {
  42. new NativeConstructor().entries().next();
  43. })))) {
  44. // create collection constructor
  45. Constructor = common.getConstructor(wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER);
  46. InternalMetadataModule.REQUIRED = true;
  47. } else if (isForced(CONSTRUCTOR_NAME, true)) {
  48. var instance = new Constructor();
  49. // early implementations not supports chaining
  50. var HASNT_CHAINING = instance[ADDER](IS_WEAK ? {} : -0, 1) != instance;
  51. // V8 ~ Chromium 40- weak-collections throws on primitives, but should return false
  52. var THROWS_ON_PRIMITIVES = fails(function () { instance.has(1); });
  53. // most early implementations doesn't supports iterables, most modern - not close it correctly
  54. // eslint-disable-next-line no-new
  55. var ACCEPT_ITERABLES = checkCorrectnessOfIteration(function (iterable) { new NativeConstructor(iterable); });
  56. // for early implementations -0 and +0 not the same
  57. var BUGGY_ZERO = !IS_WEAK && fails(function () {
  58. // V8 ~ Chromium 42- fails only with 5+ elements
  59. var $instance = new NativeConstructor();
  60. var index = 5;
  61. while (index--) $instance[ADDER](index, index);
  62. return !$instance.has(-0);
  63. });
  64. if (!ACCEPT_ITERABLES) {
  65. Constructor = wrapper(function (dummy, iterable) {
  66. anInstance(dummy, Constructor, CONSTRUCTOR_NAME);
  67. var that = inheritIfRequired(new NativeConstructor(), dummy, Constructor);
  68. if (iterable != undefined) iterate(iterable, that[ADDER], that, IS_MAP);
  69. return that;
  70. });
  71. Constructor.prototype = NativePrototype;
  72. NativePrototype.constructor = Constructor;
  73. }
  74. if (THROWS_ON_PRIMITIVES || BUGGY_ZERO) {
  75. fixMethod('delete');
  76. fixMethod('has');
  77. IS_MAP && fixMethod('get');
  78. }
  79. if (BUGGY_ZERO || HASNT_CHAINING) fixMethod(ADDER);
  80. // weak collections should not contains .clear method
  81. if (IS_WEAK && NativePrototype.clear) delete NativePrototype.clear;
  82. }
  83. exported[CONSTRUCTOR_NAME] = Constructor;
  84. $({ global: true, forced: Constructor != NativeConstructor }, exported);
  85. setToStringTag(Constructor, CONSTRUCTOR_NAME);
  86. if (!IS_WEAK) common.setStrong(Constructor, CONSTRUCTOR_NAME, IS_MAP);
  87. return Constructor;
  88. };