Skip to content
Snippets Groups Projects
Commit 97c2ab4d authored by echicken's avatar echicken :chicken:
Browse files

Merge branch 'format-array-polyfills' into 'master'

Update array.js formatting

See merge request !120
parents 30ebfa0a a7ceebeb
No related branches found
No related tags found
2 merge requests!463MRC mods by Codefenix (2024-10-20),!120Update array.js formatting
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
* load('array.js'); * load('array.js');
*/ */
if (!Array.from) { if (!Array.from) {
Array.from = (function () { Array.from = (function () {
var toStr = Object.prototype.toString; var toStr = Object.prototype.toString;
var isCallable = function (fn) { var isCallable = function (fn) {
return typeof fn === 'function' || toStr.call(fn) === '[object Function]'; return (
typeof fn === 'function' ||
toStr.call(fn) === '[object Function]'
);
}; };
var toInteger = function (value) { var toInteger = function (value) {
var number = Number(value); var number = Number(value);
...@@ -36,7 +38,9 @@ if (!Array.from) { ...@@ -36,7 +38,9 @@ if (!Array.from) {
// 3. ReturnIfAbrupt(items). // 3. ReturnIfAbrupt(items).
if (arrayLike == null) { if (arrayLike == null) {
throw new TypeError('Array.from requires an array-like object - not null or undefined'); throw new TypeError(
'Array.from requires an array-like object - not null or undefined'
);
} }
// 4. If mapfn is undefined, then let mapping be false. // 4. If mapfn is undefined, then let mapping be false.
...@@ -46,7 +50,9 @@ if (!Array.from) { ...@@ -46,7 +50,9 @@ if (!Array.from) {
// 5. else // 5. else
// 5. a If IsCallable(mapfn) is false, throw a TypeError exception. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
if (!isCallable(mapFn)) { if (!isCallable(mapFn)) {
throw new TypeError('Array.from: when provided, the second argument must be a function'); throw new TypeError(
'Array.from: when provided, the second argument must be a function'
);
} }
// 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined. // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
...@@ -70,7 +76,10 @@ if (!Array.from) { ...@@ -70,7 +76,10 @@ if (!Array.from) {
while (k < len) { while (k < len) {
kValue = items[k]; kValue = items[k];
if (mapFn) { if (mapFn) {
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k); A[k] =
typeof T === 'undefined'
? mapFn(kValue, k)
: mapFn.call(T, kValue, k);
} else { } else {
A[k] = kValue; A[k] = kValue;
} }
...@@ -81,7 +90,7 @@ if (!Array.from) { ...@@ -81,7 +90,7 @@ if (!Array.from) {
// 20. Return A. // 20. Return A.
return A; return A;
}; };
}()); })();
} }
if (!Array.of) { if (!Array.of) {
...@@ -106,24 +115,27 @@ if (!Array.prototype.copyWithin) { ...@@ -106,24 +115,27 @@ if (!Array.prototype.copyWithin) {
// Steps 6-8. // Steps 6-8.
var relativeTarget = target >> 0; var relativeTarget = target >> 0;
var to = relativeTarget < 0 ? var to =
Math.max(len + relativeTarget, 0) : relativeTarget < 0
Math.min(relativeTarget, len); ? Math.max(len + relativeTarget, 0)
: Math.min(relativeTarget, len);
// Steps 9-11. // Steps 9-11.
var relativeStart = start >> 0; var relativeStart = start >> 0;
var from = relativeStart < 0 ? var from =
Math.max(len + relativeStart, 0) : relativeStart < 0
Math.min(relativeStart, len); ? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Steps 12-14. // Steps 12-14.
var end = arguments[2]; var end = arguments[2];
var relativeEnd = end === undefined ? len : end >> 0; var relativeEnd = end === undefined ? len : end >> 0;
var final = relativeEnd < 0 ? var final =
Math.max(len + relativeEnd, 0) : relativeEnd < 0
Math.min(relativeEnd, len); ? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Step 15. // Step 15.
var count = Math.min(final - from, len - to); var count = Math.min(final - from, len - to);
...@@ -131,7 +143,7 @@ if (!Array.prototype.copyWithin) { ...@@ -131,7 +143,7 @@ if (!Array.prototype.copyWithin) {
// Steps 16-17. // Steps 16-17.
var direction = 1; var direction = 1;
if (from < to && to < (from + count)) { if (from < to && to < from + count) {
direction = -1; direction = -1;
from += count - 1; from += count - 1;
to += count - 1; to += count - 1;
...@@ -153,7 +165,7 @@ if (!Array.prototype.copyWithin) { ...@@ -153,7 +165,7 @@ if (!Array.prototype.copyWithin) {
return O; return O;
}, },
configurable: true, configurable: true,
writable: true writable: true,
}); });
} }
...@@ -161,7 +173,8 @@ if (!Array.prototype.fill) { ...@@ -161,7 +173,8 @@ if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', { Object.defineProperty(Array.prototype, 'fill', {
value: function (value) { value: function (value) {
// Steps 1-2. // Steps 1-2.
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 O = Object(this);
...@@ -173,19 +186,20 @@ if (!Array.prototype.fill) { ...@@ -173,19 +186,20 @@ if (!Array.prototype.fill) {
var relativeStart = start >> 0; var relativeStart = start >> 0;
// Step 8. // Step 8.
var k = relativeStart < 0 ? var k =
Math.max(len + relativeStart, 0) : relativeStart < 0
Math.min(relativeStart, len); ? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Steps 9-10. // Steps 9-10.
var end = arguments[2]; var end = arguments[2];
var relativeEnd = end === undefined ? var relativeEnd = end === undefined ? len : end >> 0;
len : end >> 0;
// Step 11. // Step 11.
var final = relativeEnd < 0 ? var final =
Math.max(len + relativeEnd, 0) : relativeEnd < 0
Math.min(relativeEnd, len); ? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Step 12. // Step 12.
while (k < final) { while (k < final) {
...@@ -195,7 +209,7 @@ if (!Array.prototype.fill) { ...@@ -195,7 +209,7 @@ if (!Array.prototype.fill) {
// Step 13. // Step 13.
return O; return O;
} },
}); });
} }
...@@ -240,7 +254,7 @@ if (!Array.prototype.find) { ...@@ -240,7 +254,7 @@ if (!Array.prototype.find) {
return undefined; return undefined;
}, },
configurable: true, configurable: true,
writable: true writable: true,
}); });
} }
...@@ -285,7 +299,7 @@ if (!Array.prototype.findIndex) { ...@@ -285,7 +299,7 @@ if (!Array.prototype.findIndex) {
return -1; return -1;
}, },
configurable: true, configurable: true,
writable: true writable: true,
}); });
} }
...@@ -294,7 +308,8 @@ if (!Array.prototype.includes) { ...@@ -294,7 +308,8 @@ if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', { Object.defineProperty(Array.prototype, 'includes', {
value: function (searchElement, fromIndex) { value: function (searchElement, fromIndex) {
// 1. Let O be ? ToObject(this value). // 1. Let O be ? ToObject(this value).
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 o = Object(this);
...@@ -316,7 +331,13 @@ if (!Array.prototype.includes) { ...@@ -316,7 +331,13 @@ if (!Array.prototype.includes) {
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y) { function sameValueZero(x, y) {
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); return (
x === y ||
(typeof x === 'number' &&
typeof y === 'number' &&
isNaN(x) &&
isNaN(y))
);
} }
// 7. Repeat, while k < len // 7. Repeat, while k < len
...@@ -330,6 +351,6 @@ if (!Array.prototype.includes) { ...@@ -330,6 +351,6 @@ if (!Array.prototype.includes) {
// 8. Return false // 8. Return false
return false; return false;
} },
}); });
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment