collection.js
2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
var $ = require('./export');
var global = require('../internals/global');
var InternalMetadataModule = require('../internals/internal-metadata');
var fails = require('../internals/fails');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
var iterate = require('../internals/iterate');
var anInstance = require('../internals/an-instance');
var isObject = require('../internals/is-object');
var setToStringTag = require('../internals/set-to-string-tag');
var defineProperty = require('../internals/object-define-property').f;
var forEach = require('../internals/array-iteration').forEach;
var DESCRIPTORS = require('../internals/descriptors');
var InternalStateModule = require('../internals/internal-state');
var setInternalState = InternalStateModule.set;
var internalStateGetterFor = InternalStateModule.getterFor;
module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
var IS_MAP = CONSTRUCTOR_NAME.indexOf('Map') !== -1;
var IS_WEAK = CONSTRUCTOR_NAME.indexOf('Weak') !== -1;
var ADDER = IS_MAP ? 'set' : 'add';
var NativeConstructor = global[CONSTRUCTOR_NAME];
var NativePrototype = NativeConstructor && NativeConstructor.prototype;
var exported = {};
var Constructor;
if (!DESCRIPTORS || typeof NativeConstructor != 'function'
|| !(IS_WEAK || NativePrototype.forEach && !fails(function () { new NativeConstructor().entries().next(); }))
) {
// create collection constructor
Constructor = common.getConstructor(wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER);
InternalMetadataModule.REQUIRED = true;
} else {
Constructor = wrapper(function (target, iterable) {
setInternalState(anInstance(target, Constructor, CONSTRUCTOR_NAME), {
type: CONSTRUCTOR_NAME,
collection: new NativeConstructor()
});
if (iterable != undefined) iterate(iterable, target[ADDER], { that: target, AS_ENTRIES: IS_MAP });
});
var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
forEach(['add', 'clear', 'delete', 'forEach', 'get', 'has', 'set', 'keys', 'values', 'entries'], function (KEY) {
var IS_ADDER = KEY == 'add' || KEY == 'set';
if (KEY in NativePrototype && !(IS_WEAK && KEY == 'clear')) {
createNonEnumerableProperty(Constructor.prototype, KEY, function (a, b) {
var collection = getInternalState(this).collection;
if (!IS_ADDER && IS_WEAK && !isObject(a)) return KEY == 'get' ? undefined : false;
var result = collection[KEY](a === 0 ? 0 : a, b);
return IS_ADDER ? this : result;
});
}
});
IS_WEAK || defineProperty(Constructor.prototype, 'size', {
configurable: true,
get: function () {
return getInternalState(this).collection.size;
}
});
}
setToStringTag(Constructor, CONSTRUCTOR_NAME, false, true);
exported[CONSTRUCTOR_NAME] = Constructor;
$({ global: true, forced: true }, exported);
if (!IS_WEAK) common.setStrong(Constructor, CONSTRUCTOR_NAME, IS_MAP);
return Constructor;
};