From 8eab37e8a68275518d5bd117eea0486faf3fe4ff Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Wed, 16 Oct 2024 15:07:35 +0100 Subject: [PATCH] now? --- src/utils/array-polyfill.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/utils/array-polyfill.ts b/src/utils/array-polyfill.ts index 23f72fd68..f753631fb 100644 --- a/src/utils/array-polyfill.ts +++ b/src/utils/array-polyfill.ts @@ -16,25 +16,33 @@ if (!Array.from) { throw new TypeError('Array.from requires an array-like object - not null or undefined') } - const len = Math.min(Math.max(Number(items.length) || 0, 0), Number.MAX_SAFE_INTEGER) - const result: (T | U)[] = isFunction(C) ? Object(new (C as any)(len)) : new Array(len) + const len = Number(items.length) // Get the length as a number + + // Ensure length is a finite, positive integer, or set to 0 + if (isNaN(len) || len < 0 || !isFinite(len)) { + throw new RangeError('Array length must be a finite positive integer') + } + + const finalLen = Math.min(Math.max(len, 0), Number.MAX_SAFE_INTEGER) // Ensure within bounds + + const result: (T | U)[] = isFunction(C) ? Object(new (C as any)(finalLen)) : new Array(finalLen) let k = 0 if (isFunction(mapfn)) { - while (k < len) { + while (k < finalLen) { const kValue = items[k] result[k] = mapfn.call(thisArg, kValue, k) k++ } } else { - while (k < len) { + while (k < finalLen) { result[k] = items[k] k++ } } - result.length = len + result.length = finalLen return result } })()