collection.js 4.4 KB

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