Skip to content

Commit

Permalink
Avoid polluting Array.prototype with new enumerable properties
Browse files Browse the repository at this point in the history
This way, when enumerating with for(var i in array) the new "fill" and
"indexOf" will not show up as array elements.
  • Loading branch information
piglione73 committed Mar 15, 2018
1 parent ad123be commit 42032e7
Showing 1 changed file with 56 additions and 51 deletions.
107 changes: 56 additions & 51 deletions lib/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,74 +48,79 @@ if (!Object.assign) {
*/
/* istanbul ignore next */
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
Object.defineProperty(Array.prototype, "indexOf", {
value: function(searchElement, fromIndex) {
var k;

if (this === null) {
throw new TypeError('"this" is null or not defined');
}
if (this === null) {
throw new TypeError('"this" is null or not defined');
}

var O = Object(this);
var len = O.length >>> 0;
var O = Object(this);
var len = O.length >>> 0;

if (len === 0) {
return -1;
}
if (len === 0) {
return -1;
}

var n = +fromIndex || 0;
var n = +fromIndex || 0;

if (Math.abs(n) === Infinity) {
n = 0;
}
if (Math.abs(n) === Infinity) {
n = 0;
}

if (n >= len) {
return -1;
}
if (n >= len) {
return -1;
}

k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

while (k < len) {
if (k in O && O[k] === searchElement) {
return k;
}
while (k < len) {
if (k in O && O[k] === searchElement) {
return k;
}

k++;
}
k++;
}

return -1;
};
return -1;
}
});
}

/**
* @external {Array.fill} https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
*/
/* istanbul ignore next */
if (!Array.prototype.fill) {
Array.prototype.fill = function(value) {
if (this === null) {
throw new TypeError('this is null or not defined');
}

var O = Object(this);
var len = O.length >>> 0;
var start = arguments[1];
var relativeStart = start >> 0;
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
while (k < final) {
O[k] = value;
k++;
}

return O;
};
Object.defineProperty(Array.prototype, "fill", {
value: function(value) {
if (this === null) {
throw new TypeError('this is null or not defined');
}

var O = Object(this);
var len = O.length >>> 0;
var start = arguments[1];
var relativeStart = start >> 0;
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
while (k < final) {
O[k] = value;
k++;
}

return O;
}
});

}

/**
Expand Down

0 comments on commit 42032e7

Please sign in to comment.