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