diff --git a/src/iterator-helpers.ts b/src/iterator-helpers.ts index ef54c58..af98ee5 100644 --- a/src/iterator-helpers.ts +++ b/src/iterator-helpers.ts @@ -316,7 +316,7 @@ export function isSupported(): boolean { 'every' in IteratorPrototype && 'find' in IteratorPrototype && IteratorConstructor !== undefined && - typeof IteratorConstructor === 'object' && + (typeof IteratorConstructor === 'object' || typeof IteratorConstructor === 'function') && IteratorConstructor !== null && 'from' in IteratorConstructor ) diff --git a/test/iterator-helpers.js b/test/iterator-helpers.js index 78c7687..1c717e8 100644 --- a/test/iterator-helpers.js +++ b/test/iterator-helpers.js @@ -11,6 +11,40 @@ describe('Iterator helpers', () => { expect(isPolyfilled()).to.equal(false) }) + it('isSupported returns true when Iterator is a function (native support)', () => { + // Native Iterator in Chromium is a function, not an object. + // Simulate this by temporarily replacing globalThis.Iterator with a function. + const original = globalThis.Iterator + try { + apply() // ensure polyfill is applied first so prototype methods exist + const proto = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())) + // Create a function-based Iterator with a `from` method, like native Chromium + globalThis.Iterator = function Iterator() {} + globalThis.Iterator.from = function from() {} + // Assign all required methods to the iterator prototype + for (const method of [ + 'map', + 'filter', + 'take', + 'drop', + 'flatMap', + 'reduce', + 'toArray', + 'forEach', + 'some', + 'every', + 'find', + ]) { + if (!(method in proto)) { + proto[method] = function () {} + } + } + expect(isSupported()).to.equal(true) + } finally { + globalThis.Iterator = original + } + }) + // Helper to create an iterator from an array function* arrayIterator(arr) { for (const item of arr) {