polyfill-service
Version:
A polyfill combinator
145 lines (126 loc) • 4 kB
JavaScript
(function () {
function hasProperty(o, p) {
while (o) {
if (Object.prototype.hasOwnProperty.call(o, p)) {
return true;
}
if (typeof o !== 'object') {
return false;
}
o = Object.getPrototypeOf(o);
}
return false;
}
function toInteger(n) {
n = Number(n);
if (isNaN(n)) {
return 0;
}
if (n === 0 || n === Infinity || n === -Infinity) {
return n;
} else {
return ((n < 0) ? -1 : 1) * Math.floor(Math.abs(n));
}
}
Object.defineProperty(Array.prototype, 'copyWithin', {
configurable: true,
enumerable: false,
writable: true,
// 22.1.3.3 Array.prototype.copyWithin ( target, start [ , end ] )
value: function (target, start/*, end*/) {
var end = arguments[2];
// 22.1.3.3.1 Let O be ? ToObject(this value).
if (this === null || this === undefined) {
throw new TypeError('Cannot call method on ' + this);
}
var o = Object(this);
// 22.1.3.3.2 Let len be ? ToLength(? Get(O, "length")).
var len = toInteger(o.length);
if (len <= 0) {
len = 0;
}
if (len === Infinity) {
len = Math.pow(2, 53) - 1;
} else {
len = Math.min(len, Math.pow(2, 53) - 1);
}
len = Math.max(len, 0);
// 22.1.3.3.3 Let relativeTarget be ? ToInteger(target).
var relativeTarget = toInteger(target);
// 22.1.3.3.4 If relativeTarget < 0, let to be max((len + relativeTarget), 0); else let to be min(relativeTarget, len).
var to;
if (relativeTarget < 0) {
to = Math.max(len + relativeTarget, 0);
} else {
to = Math.min(relativeTarget, len);
}
// 22.1.3.3.5 Let relativeStart be ? ToInteger(start).
var relativeStart = toInteger(start);
// 22.1.3.3.6 If relativeStart < 0, let from be max((len + relativeStart), 0); else let from be min(relativeStart, len).
var from;
if (relativeStart < 0) {
from = Math.max(len + relativeStart, 0);
} else {
from = Math.min(relativeStart, len);
}
// 22.1.3.3.7 If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToInteger(end).
var relativeEnd;
if (end === undefined) {
relativeEnd = len;
} else {
relativeEnd = toInteger(end);
}
// 22.1.3.3.8 If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let final be min(relativeEnd, len).
var final;
if (relativeEnd < 0) {
final = Math.max(len + relativeEnd, 0);
} else {
final = Math.min(relativeEnd, len);
}
// 22.1.3.3.9 Let count be min(final-from, len-to).
var count = Math.min(final - from, len - to);
// 22.1.3.3.10 If from<to and to<from+count, then
var direction;
if (from < to && to < from + count) {
// 22.1.3.3.10.a Let direction be -1.
direction = -1;
// 22.1.3.3.10.b Let from be from + count - 1.
from = from + count - 1;
// 22.1.3.3.10.c Let to be to + count - 1.
to = to + count - 1;
} else {
// 22.1.3.3.11 Else,
// 22.1.3.3.11.a Let direction be 1.
direction = 1;
}
// 22.1.3.3.12 Repeat, while count > 0
while (count > 0) {
// 22.1.3.3.12.a Let fromKey be ! ToString(from).
var fromKey = String(from);
// 22.1.3.3.12.b Let toKey be ! ToString(to).
var toKey = String(to);
// 22.1.3.3.12.c Let fromPresent be ? HasProperty(O, fromKey).
var fromPresent = hasProperty(o, fromKey);
// 22.1.3.3.12.d If fromPresent is true, then
if (fromPresent) {
// 22.1.3.3.12.d.i Let fromVal be ? Get(O, fromKey).
var fromVal = o[fromKey];
// 22.1.3.3.12.d.ii Perform ? Set(O, toKey, fromVal, true).
o[toKey] = fromVal;
} else {
// 22.1.3.3.12.e Else fromPresent is false,
// 22.1.3.3.12.e.i Perform ? DeletePropertyOrThrow(O, toKey).
delete o[toKey];
}
// 22.1.3.3.12.f Let from be from + direction.
from = from + direction;
// 22.1.3.3.12.g Let to be to + direction.
to = to + direction;
// 22.1.3.3.12.h Let count be count - 1.
count = count - 1;
}
// 22.1.3.3.13 Return O.
return o;
}
});
}());