-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmap-polyfill.js
144 lines (126 loc) · 3.87 KB
/
map-polyfill.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
function assert(val, msg) {
if (!val) {
throw new TypeError(msg);
}
}
function isObject(x) {
return (typeof x === 'function' || typeof x === 'object');
}
function SpeciesConstructor(O, defaultConstructor) {
assert(isObject(O), 'O is not Object');
const C = O.constructor;
if (typeof C === 'undefined') {
return defaultConstructor;
}
if (!isObject(C)) {
throw new TypeError('O.constructor is not an Object');
}
const S = C[Symbol.species];
if (S == null) {
return defaultConstructor;
}
if (typeof S === 'function' && !!S.prototype) {
return S;
}
throw new TypeError('no constructor found');
}
function isCallable(x) {
try {
Function.prototype.toString.call(x);
return true;
} catch(e) {
return false;
}
}
function filter(callbackFn, thisArg) {
assert(isObject(this), 'this is not an object');
assert(isCallable(callbackFn), 'callback is not a function');
const Ctor = SpeciesConstructor(this, Map);
const retObj = new Ctor();
const _set = retObj.set;
for(const [key, value] of this) {
if (Reflect.apply(callbackFn, thisArg, [value, key, this])) {
Reflect.apply(_set, retObj, [key, value]);
}
}
return retObj;
}
function mapValues(callbackFn, thisArg) {
assert(isObject(this), 'this is not an object');
assert(isCallable(callbackFn), 'callback is not a function');
const Ctor = SpeciesConstructor(this, Map);
const retObj = new Ctor();
const _set = retObj.set;
for(const [key, value] of this) {
const newValue = Reflect.apply(callbackFn, thisArg, [value, key, this])
Reflect.apply(_set, retObj, [key, newValue]);
}
return retObj;
}
function mapKeys(callbackFn, thisArg) {
assert(isObject(this), 'this is not an object');
assert(isCallable(callbackFn), 'callback is not a function');
const Ctor = SpeciesConstructor(this, Map);
const retObj = new Ctor();
const _set = retObj.set;
for(const [key, value] of this) {
const newKey = Reflect.apply(callbackFn, thisArg, [value, key, this])
Reflect.apply(_set, retObj, [newKey, value]);
}
return retObj;
}
function merge(iterable) {
assert(isObject(this), 'this is not an object');
const Ctor = SpeciesConstructor(this, Map);
return new Ctor([...this, ...iterable]);
}
function groupBy(iterable, keyDerivative) {
assert(isObject(this), 'this is not an object');
assert(isCallable(this), 'this is not a function');
assert(Map === this || Map.isPrototypeOf(this), 'this is not a Map constructor');
const retObj = new this();
for(const element of iterable) {
const derivedKey = Reflect.apply(keyDerivative, null, [element]);
if (!retObj.has(derivedKey)) {
retObj.set(derivedKey, []);
}
const arr = retObj.get(derivedKey);
arr.push(element);
}
return retObj;
}
function keyBy(iterable, keyDerivative) {
assert(isObject(this), 'this is not an object');
assert(isCallable(this), 'this is not a function');
assert(Map === this || Map.isPrototypeOf(this), 'this is not a Map constructor');
const retObj = new this();
for(const element of iterable) {
const derivedKey = Reflect.apply(keyDerivative, null, [element]);
retObj.set(derivedKey, value);
}
return retObj;
}
assert(typeof Map === 'function', 'Map does not exist');
const prototypeMethods = {
mapValues,
mapKeys,
filter,
merge,
};
const staticMethods = {
groupBy,
keyBy
};
for (const [key, value] of Object.entries(prototypeMethods)) {
Object.defineProperty(Map.prototype, key, {
configurable: true,
value
});
}
for (const [key, value] of Object.entries(staticMethods)) {
Object.defineProperty(Map, key, {
configurable: true,
value
});
}