_collection.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var global = require('./_global');
  3. var $export = require('./_export');
  4. var meta = require('./_meta');
  5. var fails = require('./_fails');
  6. var hide = require('./_hide');
  7. var redefineAll = require('./_redefine-all');
  8. var forOf = require('./_for-of');
  9. var anInstance = require('./_an-instance');
  10. var isObject = require('./_is-object');
  11. var setToStringTag = require('./_set-to-string-tag');
  12. var dP = require('./_object-dp').f;
  13. var each = require('./_array-methods')(0);
  14. var DESCRIPTORS = require('./_descriptors');
  15. module.exports = function (NAME, wrapper, methods, common, IS_MAP, IS_WEAK) {
  16. var Base = global[NAME];
  17. var C = Base;
  18. var ADDER = IS_MAP ? 'set' : 'add';
  19. var proto = C && C.prototype;
  20. var O = {};
  21. if (!DESCRIPTORS || typeof C != 'function' || !(IS_WEAK || proto.forEach && !fails(function () {
  22. new C().entries().next();
  23. }))) {
  24. // create collection constructor
  25. C = common.getConstructor(wrapper, NAME, IS_MAP, ADDER);
  26. redefineAll(C.prototype, methods);
  27. meta.NEED = true;
  28. } else {
  29. C = wrapper(function (target, iterable) {
  30. anInstance(target, C, NAME, '_c');
  31. target._c = new Base();
  32. if (iterable != undefined) forOf(iterable, IS_MAP, target[ADDER], target);
  33. });
  34. each('add,clear,delete,forEach,get,has,set,keys,values,entries,toJSON'.split(','), function (KEY) {
  35. var IS_ADDER = KEY == 'add' || KEY == 'set';
  36. if (KEY in proto && !(IS_WEAK && KEY == 'clear')) hide(C.prototype, KEY, function (a, b) {
  37. anInstance(this, C, KEY);
  38. if (!IS_ADDER && IS_WEAK && !isObject(a)) return KEY == 'get' ? undefined : false;
  39. var result = this._c[KEY](a === 0 ? 0 : a, b);
  40. return IS_ADDER ? this : result;
  41. });
  42. });
  43. IS_WEAK || dP(C.prototype, 'size', {
  44. get: function () {
  45. return this._c.size;
  46. }
  47. });
  48. }
  49. setToStringTag(C, NAME);
  50. O[NAME] = C;
  51. $export($export.G + $export.W + $export.F, O);
  52. if (!IS_WEAK) common.setStrong(C, NAME, IS_MAP);
  53. return C;
  54. };