Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes iterator helpers when interacting with other polyfills #80

Merged
merged 8 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions polyfills/Array/prototype/entries/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global Iterator */

it('is a function', function () {
proclaim.isFunction(Array.prototype.entries);
});
Expand Down Expand Up @@ -25,6 +27,11 @@ it('returns a next-able object', function () {
});
});

it('returns an `Iterator`', function () {
var iterator = [].entries();
proclaim.isInstanceOf(iterator, Iterator);
});

it('finally returns a done object', function () {
var array = ['val1', 'val2'];
var iterator = array.entries();
Expand Down
7 changes: 7 additions & 0 deletions polyfills/Array/prototype/keys/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global Iterator */

it('is a function', function () {
proclaim.isFunction(Array.prototype.keys);
});
Expand Down Expand Up @@ -25,6 +27,11 @@ it('returns a next-able object', function () {
});
});

it('returns an `Iterator`', function () {
var iterator = [].keys();
proclaim.isInstanceOf(iterator, Iterator);
});

it('finally returns a done object', function () {
var array = ['val1', 'val2'];
var iterator = array.keys();
Expand Down
7 changes: 7 additions & 0 deletions polyfills/Array/prototype/values/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global Iterator */

it('is a function', function () {
proclaim.isFunction(Array.prototype.values);
});
Expand Down Expand Up @@ -35,6 +37,11 @@ it('returns a next-able object', function () {
});
});

it('returns an `Iterator`', function () {
var iterator = [].values();
proclaim.isInstanceOf(iterator, Iterator);
});

it('finally returns a done object', function () {
var array = ['val1', 'val2'];
var iterator = array.values();
Expand Down
7 changes: 6 additions & 1 deletion polyfills/DOMTokenList/prototype/@@iterator/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Symbol */
/* global Iterator, Symbol */

function getDOMTokenList () {
var div = document.createElement('div');
Expand All @@ -25,6 +25,11 @@ it('returns a next-able object', function () {
});
});

it('returns an `Iterator`', function () {
var iterator = getDOMTokenList()[Symbol.iterator]();
proclaim.isInstanceOf(iterator, Iterator);
});

it('finally returns a done object', function () {
var tokenList = getDOMTokenList();
var iterator = tokenList[Symbol.iterator]();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Symbol */
/* global Iterator, Symbol */

function getHTMLCollection() {
var element = document.createElement('div');
Expand Down Expand Up @@ -26,6 +26,11 @@ it('returns a next-able object', function () {
});
});

it('returns an `Iterator`', function () {
var iterator = getHTMLCollection()[Symbol.iterator]();
proclaim.isInstanceOf(iterator, Iterator);
});

it('finally returns a done object', function () {
var htmlCollection = getHTMLCollection();
var iterator = htmlCollection[Symbol.iterator]();
Expand Down
23 changes: 23 additions & 0 deletions polyfills/Iterator/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
aliases = [ "es2025" ]
dependencies = [
"_ESAbstract.CreateMethodProperty",
]
license = "MIT"
spec = "https://tc39.es/ecma262/#sec-iterator-constructor"
docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator"

[browsers]
android = "*"
bb = "*"
chrome = "<122"
edge = "*"
edge_mob = "*"
firefox = "<131"
firefox_mob = "<17"
ie = "*"
ie_mob = "*"
opera = "<108"
op_mob = "<81"
safari = "*"
ios_saf = "*"
samsung_mob = "<26.0"
1 change: 1 addition & 0 deletions polyfills/Iterator/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Iterator" in self;
2 changes: 1 addition & 1 deletion polyfills/Iterator/from/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.1.2.2 Iterator.from ( O )
CreateMethodProperty(Iterator, "from", function from(O) {
return IteratorHelpersUtils.iterator.from.call(this, O);
return IteratorHelpersUtils.iterator.from(O);
});
27 changes: 27 additions & 0 deletions polyfills/Iterator/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* global CreateMethodProperty */
// 27.1.3.1 The Iterator Constructor
(function () {
function Iterator() {
if (
!(this instanceof Iterator) ||
this.constructor === Iterator ||
!Object.prototype.isPrototypeOf.call(Iterator, this.constructor)
) {
throw new TypeError(
"`Iterator` can not be called or constructed directly"
);
}
}

if (
self.Symbol &&
self.Symbol.iterator &&
Array.prototype[self.Symbol.iterator]
) {
Iterator.prototype = Object.getPrototypeOf(
Object.getPrototypeOf(Array.prototype[self.Symbol.iterator]())
);
}

CreateMethodProperty(self, "Iterator", Iterator);
})();
23 changes: 23 additions & 0 deletions polyfills/Iterator/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* global Iterator */

it("is a function", function () {
proclaim.isFunction(Iterator);
});

it("has correct arity", function () {
proclaim.arity(Iterator, 0);
});

it("has correct name", function () {
proclaim.hasName(Iterator, "Iterator");
});

it("is not enumerable", function () {
proclaim.isNotEnumerable(window, "Iterator");
});

it("cannot be constructed directly", function () {
proclaim.throws(function () {
new Iterator();
}, /can not be called or constructed directly/);
});
25 changes: 25 additions & 0 deletions polyfills/Iterator/prototype/@@iterator/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aliases = [ "es6", "es2015" ]
dependencies = [
"_ESAbstract.CreateMethodProperty",
"Iterator",
"Symbol.iterator",
]
license = "MIT"
spec = "https://tc39.es/ecma262/#sec-iterator.prototype-%symbol.iterator%"
docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/Symbol.iterator"

[browsers]
android = "<5.1"
bb = "10 - *"
chrome = "<49"
edge = "<13"
edge_mob = "<13"
firefox = "<17"
firefox_mob = "<17"
ie = "*"
ie_mob = "*"
opera = "<37"
op_mob = "<37"
safari = "<9.0"
ios_saf = "<9.0"
samsung_mob = "<5.0"
4 changes: 4 additions & 0 deletions polyfills/Iterator/prototype/@@iterator/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"Symbol" in self &&
"iterator" in self.Symbol &&
"Iterator" in self &&
!!self.Iterator.prototype[self.Symbol.iterator];
5 changes: 5 additions & 0 deletions polyfills/Iterator/prototype/@@iterator/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* global CreateMethodProperty, Iterator, Symbol */
// 27.1.4.13 Iterator.prototype [ %Symbol.iterator% ] ( )
CreateMethodProperty(Iterator.prototype, Symbol.iterator, function () {
return this;
});
33 changes: 33 additions & 0 deletions polyfills/Iterator/prototype/@@iterator/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* globals Iterator, Symbol */

it("is a function", function () {
proclaim.isFunction(Iterator.prototype[Symbol.iterator]);
});

it("has correct arity", function () {
proclaim.arity(Iterator.prototype[Symbol.iterator], 0);
});

// TODO: Look into this
// it('has correct name', function () {
// proclaim.hasName(Iterator.prototype[Symbol.iterator], '[Symbol.iterator]');
// });

it("is not enumerable", function () {
proclaim.isNotEnumerable(Iterator.prototype, Symbol.iterator);
});

it("returns itself", function () {
function TestIterator(arr) {
var i = -1;
this.next = function () {
i++;
return { value: arr[i], done: i >= arr.length };
};
}
TestIterator.prototype = Iterator.prototype;

var iterator = new TestIterator([1, 2, 3]);

proclaim.equal(iterator[Symbol.iterator](), iterator);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/drop/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.5 Iterator.prototype.drop ( limit )
CreateMethodProperty(Iterator.prototype, "drop", function drop(limit) {
return IteratorHelpersUtils.iteratorPrototype.drop.call(this, limit);
return IteratorHelpersUtils.iteratorPrototype.drop(this, limit);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/every/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.11 Iterator.prototype.every ( predicate )
CreateMethodProperty(Iterator.prototype, "every", function every(predicate) {
return IteratorHelpersUtils.iteratorPrototype.every.call(this, predicate);
return IteratorHelpersUtils.iteratorPrototype.every(this, predicate);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/filter/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.3 Iterator.prototype.filter ( predicate )
CreateMethodProperty(Iterator.prototype, "filter", function filter(predicate) {
return IteratorHelpersUtils.iteratorPrototype.filter.call(this, predicate);
return IteratorHelpersUtils.iteratorPrototype.filter(this, predicate);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/find/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.12 Iterator.prototype.find ( predicate )
CreateMethodProperty(Iterator.prototype, "find", function find(predicate) {
return IteratorHelpersUtils.iteratorPrototype.find.call(this, predicate);
return IteratorHelpersUtils.iteratorPrototype.find(this, predicate);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/flatMap/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.6 Iterator.prototype.flatMap ( mapper )
CreateMethodProperty(Iterator.prototype, "flatMap", function flatMap(mapper) {
return IteratorHelpersUtils.iteratorPrototype.flatMap.call(this, mapper);
return IteratorHelpersUtils.iteratorPrototype.flatMap(this, mapper);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/forEach/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.9 Iterator.prototype.forEach ( fn )
CreateMethodProperty(Iterator.prototype, "forEach", function forEach(fn) {
return IteratorHelpersUtils.iteratorPrototype.forEach.call(this, fn);
return IteratorHelpersUtils.iteratorPrototype.forEach(this, fn);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/map/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.2 Iterator.prototype.map ( mapper )
CreateMethodProperty(Iterator.prototype, "map", function map(mapper) {
return IteratorHelpersUtils.iteratorPrototype.map.call(this, mapper);
return IteratorHelpersUtils.iteratorPrototype.map(this, mapper);
});
11 changes: 9 additions & 2 deletions polyfills/Iterator/prototype/reduce/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
CreateMethodProperty(
Iterator.prototype,
"reduce",
// eslint-disable-next-line no-unused-vars
function reduce(reducer /* , initialValue */) {
return IteratorHelpersUtils.iteratorPrototype.reduce.apply(this, arguments);
if (arguments.length > 1) {
return IteratorHelpersUtils.iteratorPrototype.reduce(
this,
reducer,
arguments[1]
);
} else {
return IteratorHelpersUtils.iteratorPrototype.reduce(this, reducer);
}
}
);
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/some/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.10 Iterator.prototype.some ( predicate )
CreateMethodProperty(Iterator.prototype, "some", function some(predicate) {
return IteratorHelpersUtils.iteratorPrototype.some.call(this, predicate);
return IteratorHelpersUtils.iteratorPrototype.some(this, predicate);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/take/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.4 Iterator.prototype.take ( limit )
CreateMethodProperty(Iterator.prototype, "take", function take(limit) {
return IteratorHelpersUtils.iteratorPrototype.take.call(this, limit);
return IteratorHelpersUtils.iteratorPrototype.take(this, limit);
});
2 changes: 1 addition & 1 deletion polyfills/Iterator/prototype/toArray/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global CreateMethodProperty, Iterator, IteratorHelpersUtils */
// 3.1.3.8 Iterator.prototype.toArray ( )
CreateMethodProperty(Iterator.prototype, "toArray", function toArray() {
return IteratorHelpersUtils.iteratorPrototype.toArray.call(this);
return IteratorHelpersUtils.iteratorPrototype.toArray(this);
});
1 change: 1 addition & 0 deletions polyfills/Map/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"_ESAbstract.SameValueZero",
"_ESAbstract.ThrowCompletion",
"_ESAbstract.Type",
"Iterator",
"Symbol",
"Symbol.iterator",
"Symbol.species",
Expand Down
4 changes: 2 additions & 2 deletions polyfills/Map/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global CreateIterResultObject, CreateMethodProperty, GetIterator, IsCallable, IteratorClose, IteratorStepValue, OrdinaryCreateFromConstructor, SameValueZero, ThrowCompletion, Type, Symbol */
/* global CreateIterResultObject, CreateMethodProperty, GetIterator, IsCallable, Iterator, IteratorClose, IteratorStepValue, OrdinaryCreateFromConstructor, SameValueZero, ThrowCompletion, Type, Symbol */
(function (global) {
// Need an internal counter to assign unique IDs to a key map
var _uniqueHashId = 0;
Expand Down Expand Up @@ -532,7 +532,7 @@
}

// 23.1.5.2. The %MapIteratorPrototype% Object
var MapIteratorPrototype = {};
var MapIteratorPrototype = Object.create(Iterator.prototype);
// We use this as a quick way to check if an object is a Map Iterator instance.
Object.defineProperty(MapIteratorPrototype, 'isMapIterator', {
configurable: false,
Expand Down
4 changes: 3 additions & 1 deletion polyfills/Map/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

/* globals Map, Symbol */
/* globals Iterator, Map, Symbol */

it('is a function', function () {
proclaim.isFunction(Map);
Expand Down Expand Up @@ -566,6 +566,8 @@ describe('Map', function () {
proclaim.equal(lastResult.done, true);
proclaim.ok(Object.prototype.hasOwnProperty.call(lastResult, 'value'));
proclaim.equal(lastResult.value, void 0);

proclaim.isInstanceOf(new Map().keys(), Iterator);
});

if ('Symbol' in window && 'iterator' in Symbol) {
Expand Down
7 changes: 6 additions & 1 deletion polyfills/NodeList/prototype/@@iterator/polyfill.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Symbol */
/* global Iterator, Symbol */

function getNodeList() {
var fragment = document.createDocumentFragment();
Expand Down Expand Up @@ -26,6 +26,11 @@ it('returns a next-able object', function () {
});
});

it('returns an `Iterator`', function () {
var iterator = getNodeList()[Symbol.iterator]();
proclaim.isInstanceOf(iterator, Iterator);
});

it('finally returns a done object', function () {
var nodeList = getNodeList();
var iterator = nodeList[Symbol.iterator]();
Expand Down
1 change: 1 addition & 0 deletions polyfills/Set/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies = [
"_ESAbstract.OrdinaryCreateFromConstructor",
"_ESAbstract.SameValueZero",
"_ESAbstract.ThrowCompletion",
"Iterator",
"Symbol",
"Symbol.iterator",
"Symbol.species",
Expand Down
Loading
Loading