Skip to content

Commit 48d8703

Browse files
waabidWilcoFiers
authored andcommitted
fix: Polyfills are made non-enumerable #648. (#649)
1 parent fd94b64 commit 48d8703

File tree

1 file changed

+136
-128
lines changed

1 file changed

+136
-128
lines changed

lib/core/utils/pollyfills.js

Lines changed: 136 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ if (typeof Object.assign !== 'function') {
3030
}
3131

3232
if (!Array.prototype.find) {
33-
Array.prototype.find = function(predicate) {
34-
if (this === null) {
35-
throw new TypeError('Array.prototype.find called on null or undefined');
36-
}
37-
if (typeof predicate !== 'function') {
38-
throw new TypeError('predicate must be a function');
39-
}
40-
var list = Object(this);
41-
var length = list.length >>> 0;
42-
var thisArg = arguments[1];
43-
var value;
44-
45-
for (var i = 0; i < length; i++) {
46-
value = list[i];
47-
if (predicate.call(thisArg, value, i, list)) {
48-
return value;
33+
Object.defineProperty(Array.prototype, "find", {
34+
value: function (predicate) {
35+
if (this === null) {
36+
throw new TypeError('Array.prototype.find called on null or undefined');
37+
}
38+
if (typeof predicate !== 'function') {
39+
throw new TypeError('predicate must be a function');
4940
}
41+
var list = Object(this);
42+
var length = list.length >>> 0;
43+
var thisArg = arguments[1];
44+
var value;
45+
46+
for (var i = 0; i < length; i++) {
47+
value = list[i];
48+
if (predicate.call(thisArg, value, i, list)) {
49+
return value;
50+
}
51+
}
52+
return undefined;
5053
}
51-
return undefined;
52-
};
54+
});
5355
}
5456

5557

@@ -117,137 +119,143 @@ if (typeof window.addEventListener === 'function') {
117119

118120

119121
if (!Array.prototype.includes) {
120-
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
121-
'use strict';
122-
var O = Object(this);
123-
var len = parseInt(O.length, 10) || 0;
124-
if (len === 0) {
125-
return false;
126-
}
127-
var n = parseInt(arguments[1], 10) || 0;
128-
var k;
129-
if (n >= 0) {
130-
k = n;
131-
} else {
132-
k = len + n;
133-
if (k < 0) {k = 0;}
134-
}
135-
var currentElement;
136-
while (k < len) {
137-
currentElement = O[k];
138-
if (searchElement === currentElement ||
139-
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
140-
return true;
122+
Object.defineProperty(Array.prototype, "includes", {
123+
value: function (searchElement) {
124+
'use strict';
125+
var O = Object(this);
126+
var len = parseInt(O.length, 10) || 0;
127+
if (len === 0) {
128+
return false;
129+
}
130+
var n = parseInt(arguments[1], 10) || 0;
131+
var k;
132+
if (n >= 0) {
133+
k = n;
134+
} else {
135+
k = len + n;
136+
if (k < 0) { k = 0; }
141137
}
142-
k++;
138+
var currentElement;
139+
while (k < len) {
140+
currentElement = O[k];
141+
if (searchElement === currentElement ||
142+
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
143+
return true;
144+
}
145+
k++;
146+
}
147+
return false;
143148
}
144-
return false;
145-
};
149+
});
146150
}
147151

148152
// Production steps of ECMA-262, Edition 5, 15.4.4.17
149153
// Reference: http://es5.github.io/#x15.4.4.17
150154
if (!Array.prototype.some) {
151-
Array.prototype.some = function(fun/*, thisArg*/) {
152-
'use strict';
155+
Object.defineProperty(Array.prototype, "some", {
156+
value: function (fun) {
157+
'use strict';
153158

154-
if (this == null) {
155-
throw new TypeError('Array.prototype.some called on null or undefined');
156-
}
159+
if (this == null) {
160+
throw new TypeError('Array.prototype.some called on null or undefined');
161+
}
157162

158-
if (typeof fun !== 'function') {
159-
throw new TypeError();
160-
}
163+
if (typeof fun !== 'function') {
164+
throw new TypeError();
165+
}
161166

162-
var t = Object(this);
163-
var len = t.length >>> 0;
167+
var t = Object(this);
168+
var len = t.length >>> 0;
164169

165-
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
166-
for (var i = 0; i < len; i++) {
167-
if (i in t && fun.call(thisArg, t[i], i, t)) {
168-
return true;
170+
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
171+
for (var i = 0; i < len; i++) {
172+
if (i in t && fun.call(thisArg, t[i], i, t)) {
173+
return true;
174+
}
169175
}
170-
}
171176

172-
return false;
173-
};
177+
return false;
178+
}
179+
});
174180
}
175181

176182
if (!Array.from) {
177-
Array.from = (function () {
178-
var toStr = Object.prototype.toString;
179-
var isCallable = function (fn) {
180-
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
181-
};
182-
var toInteger = function (value) {
183-
var number = Number(value);
184-
if (isNaN(number)) { return 0; }
185-
if (number === 0 || !isFinite(number)) { return number; }
186-
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
187-
};
188-
var maxSafeInteger = Math.pow(2, 53) - 1;
189-
var toLength = function (value) {
190-
var len = toInteger(value);
191-
return Math.min(Math.max(len, 0), maxSafeInteger);
192-
};
193-
194-
// The length property of the from method is 1.
195-
return function from(arrayLike/*, mapFn, thisArg */) {
196-
// 1. Let C be the this value.
197-
var C = this;
198-
199-
// 2. Let items be ToObject(arrayLike).
200-
var items = Object(arrayLike);
201-
202-
// 3. ReturnIfAbrupt(items).
203-
if (arrayLike == null) {
204-
throw new TypeError("Array.from requires an array-like object - not null or undefined");
205-
}
206-
207-
// 4. If mapfn is undefined, then let mapping be false.
208-
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
209-
var T;
210-
if (typeof mapFn !== 'undefined') {
211-
// 5. else
212-
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
213-
if (!isCallable(mapFn)) {
214-
throw new TypeError('Array.from: when provided, the second argument must be a function');
183+
Object.defineProperty(Array, "from", {
184+
value: function () {
185+
var toStr = Object.prototype.toString;
186+
var isCallable = function (fn) {
187+
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
188+
};
189+
var toInteger = function (value) {
190+
var number = Number(value);
191+
if (isNaN(number)) { return 0; }
192+
if (number === 0 || !isFinite(number)) { return number; }
193+
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
194+
};
195+
var maxSafeInteger = Math.pow(2, 53) - 1;
196+
var toLength = function (value) {
197+
var len = toInteger(value);
198+
return Math.min(Math.max(len, 0), maxSafeInteger);
199+
};
200+
201+
// The length property of the from method is 1.
202+
return function from(arrayLike/*, mapFn, thisArg */) {
203+
// 1. Let C be the this value.
204+
var C = this;
205+
206+
// 2. Let items be ToObject(arrayLike).
207+
var items = Object(arrayLike);
208+
209+
// 3. ReturnIfAbrupt(items).
210+
if (arrayLike == null) {
211+
throw new TypeError("Array.from requires an array-like object - not null or undefined");
215212
}
216213

217-
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
218-
if (arguments.length > 2) {
219-
T = arguments[2];
220-
}
221-
}
222-
223-
// 10. Let lenValue be Get(items, "length").
224-
// 11. Let len be ToLength(lenValue).
225-
var len = toLength(items.length);
214+
// 4. If mapfn is undefined, then let mapping be false.
215+
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
216+
var T;
217+
if (typeof mapFn !== 'undefined') {
218+
// 5. else
219+
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
220+
if (!isCallable(mapFn)) {
221+
throw new TypeError('Array.from: when provided, the second argument must be a function');
222+
}
226223

227-
// 13. If IsConstructor(C) is true, then
228-
// 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
229-
// 14. a. Else, Let A be ArrayCreate(len).
230-
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
224+
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
225+
if (arguments.length > 2) {
226+
T = arguments[2];
227+
}
228+
}
231229

232-
// 16. Let k be 0.
233-
var k = 0;
234-
// 17. Repeat, while k < len… (also steps a - h)
235-
var kValue;
236-
while (k < len) {
237-
kValue = items[k];
238-
if (mapFn) {
239-
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
240-
} else {
241-
A[k] = kValue;
230+
// 10. Let lenValue be Get(items, "length").
231+
// 11. Let len be ToLength(lenValue).
232+
var len = toLength(items.length);
233+
234+
// 13. If IsConstructor(C) is true, then
235+
// 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
236+
// 14. a. Else, Let A be ArrayCreate(len).
237+
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
238+
239+
// 16. Let k be 0.
240+
var k = 0;
241+
// 17. Repeat, while k < len… (also steps a - h)
242+
var kValue;
243+
while (k < len) {
244+
kValue = items[k];
245+
if (mapFn) {
246+
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
247+
} else {
248+
A[k] = kValue;
249+
}
250+
k += 1;
242251
}
243-
k += 1;
244-
}
245-
// 18. Let putStatus be Put(A, "length", len, true).
246-
A.length = len;
247-
// 20. Return A.
248-
return A;
249-
};
250-
}());
252+
// 18. Let putStatus be Put(A, "length", len, true).
253+
A.length = len;
254+
// 20. Return A.
255+
return A;
256+
};
257+
}()
258+
});
251259
}
252260

253261
if (!String.prototype.includes) {

0 commit comments

Comments
 (0)