$.collection.js 1.9 KB

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