8000 Fix isSupported() for Iterator helpers to accept native function-base… · github/browser-support@35eb283 · GitHub 7FFF
[go: up one dir, main page]

Skip to content

Commit 35eb283

Browse files
Copilotmattcosta7
andcommitted
Fix isSupported() for Iterator helpers to accept native function-based Iterator
The typeof check `typeof IteratorConstructor === 'object'` incorrectly returns false for native Iterator in Chromium where Iterator is a function. Changed to also accept 'function' type. Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
1 parent 0feec67 commit 35eb283

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/iterator-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export function isSupported(): boolean {
316316
'every' in IteratorPrototype &&
317317
'find' in IteratorPrototype &&
318318
IteratorConstructor !== undefined &&
319-
typeof IteratorConstructor === 'object' &&
319+
(typeof IteratorConstructor === 'object' || typeof IteratorConstructor === 'function') &&
320320
IteratorConstructor !== null &&
321321
'from' in IteratorConstructor
322322
)

test/iterator-helpers.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@ describe('Iterator helpers', () => {
1111
expect(isPolyfilled()).to.equal(false)
1212
})
1313

14+
it('isSupported returns true when Iterator is a function (native support)', () => {
15+
// Native Iterator in Chromium is a function, not an object.
16+
// Simulate this by temporarily replacing globalThis.Iterator with a function.
17+
const original = globalThis.Iterator
18+
try {
19+
apply() // ensure polyfill is applied first so prototype methods exist
20+
const proto = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))
21+
// Create a function-based Iterator with a `from` method, like native Chromium
22+
globalThis.Iterator = function Iterator() {}
23+
globalThis.Iterator.from = function from() {}
24+
// Assign all required methods to the iterator prototype
25+
for (const method of [
26+
'map',
27+
'filter',
28+
'take',
29+
'drop',
30+
'flatMap',
31+
'reduce',
32+
'toArray',
33+
'forEach',
34+
'some',
35+
'every',
36+
'find',
37+
]) {
38+
if (!(method in proto)) {
39+
proto[method] = function () {}
40+
}
41+
}
42+
expect(isSupported()).to.equal(true)
43+
} finally {
44+
globalThis.Iterator = original
45+
}
46+
})
47+
1448
// Helper to create an iterator from an array
1549
function* arrayIterator(arr) {
1650
for (const item of arr) {

0 commit comments

Comments
 (0)
0