Skip to content

Commit

Permalink
[New] add Iterator.prototype shim
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 30, 2023
1 parent 57bae8c commit c4a6203
Show file tree
Hide file tree
Showing 25 changed files with 143 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Iterator.prototype.constructor/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

var $Iterator = require('./implementation');

Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.drop/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeDrop() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.every/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeEvery() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.filter/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeFilter() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.find/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeFind() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.flatMap/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeFlatMap() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.forEach/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeForEach() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.map/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeMap() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.reduce/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeReduce() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.some/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeSome() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.take/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeTake() {
var polyfill = getPolyfill();
Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.toArray/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');
var $IteratorPrototype = require('../Iterator.prototype/implementation');

module.exports = function shimIteratorPrototypeToArray() {
var polyfill = getPolyfill();
Expand Down
3 changes: 3 additions & 0 deletions Iterator.prototype/auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./shim')();
3 changes: 3 additions & 0 deletions Iterator.prototype/implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('iterator.prototype');
12 changes: 12 additions & 0 deletions Iterator.prototype/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');

module.exports = {
__proto__: getPolyfill(),
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
};
7 changes: 7 additions & 0 deletions Iterator.prototype/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
return implementation;
};
20 changes: 20 additions & 0 deletions Iterator.prototype/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var getPolyfill = require('./polyfill');
var define = require('define-properties');

var getIteratorPolyfill = require('../Iterator/polyfill');

module.exports = function shimIteratorFrom() {
var $Iterator = getIteratorPolyfill();
var polyfill = getPolyfill();
define(
$Iterator,
{ prototype: polyfill },
{ prototype: function () { return $Iterator.prototype !== polyfill; } }
);

// TODO: install Symbol.toStringTag if needed, once https://bugs.chromium.org/p/chromium/issues/detail?id=1477372 is fixed?

return polyfill;
};
2 changes: 1 addition & 1 deletion IteratorHelperPrototype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var setToStringTag = require('es-set-tostringtag');
var hasProto = require('has-proto')();
var iterProto = require('iterator.prototype');
var iterProto = require('../Iterator.prototype/implementation');
var SLOT = require('internal-slot');

var CompletionRecord = require('es-abstract/2023/CompletionRecord');
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The main export of the package itself is simply an array of the available direct
## Supported things

- [`Iterator` constructor](https://tc39.es/proposal-iterator-helpers/#sec-iterator-constructor)
- [`Iterator.prototype`](https://tc39.es/proposal-iterator-helpers/#sec-iterator.prototype)
- [`Iterator.from`](https://tc39.es/proposal-iterator-helpers/#sec-iterator.from)
- [`Iterator.prototype.constructor`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.constructor)
- [`Iterator.prototype.drop`](https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.drop)
Expand Down
2 changes: 1 addition & 1 deletion WrapForValidIteratorPrototype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var GetMethod = require('es-abstract/2023/GetMethod');
var Type = require('es-abstract/2023/Type');

var SLOT = require('internal-slot');
var iterProto = require('iterator.prototype');
var iterProto = require('../Iterator.prototype/implementation');

// https://tc39.es/proposal-iterator-helpers/#sec-wrapforvaliditeratorprototype-object

Expand Down
1 change: 1 addition & 0 deletions index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"Iterator",
"Iterator.from",
"Iterator.prototype",
"Iterator.prototype.constructor",
"Iterator.prototype.drop",
"Iterator.prototype.every",
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"./Iterator/polyfill": "./Iterator/polyfill.js",
"./Iterator/implementation": "./Iterator/implementation.js",
"./Iterator/shim": "./Iterator/shim.js",
"./Iterator.prototype": "./Iterator.prototype/index.js",
"./Iterator.prototype/auto": "./Iterator.prototype/auto.js",
"./Iterator.prototype/polyfill": "./Iterator.prototype/polyfill.js",
"./Iterator.prototype/implementation": "./Iterator.prototype/implementation.js",
"./Iterator.prototype/shim": "./Iterator.prototype/shim.js",
"./Iterator.from": "./Iterator.from/index.js",
"./Iterator.from/auto": "./Iterator.from/auto.js",
"./Iterator.from/polyfill": "./Iterator.from/polyfill.js",
Expand Down Expand Up @@ -138,7 +143,7 @@
"has-proto": "^1.0.1",
"has-symbols": "^1.0.3",
"internal-slot": "^1.0.5",
"iterator.prototype": "^1.1.0",
"iterator.prototype": "^1.1.1",
"safe-array-concat": "^1.0.0"
},
"devDependencies": {
Expand All @@ -153,6 +158,7 @@
"for-each": "^0.3.3",
"functions-have-names": "^1.2.3",
"has-strict-mode": "^1.0.1",
"has-tostringtag": "^1.0.0",
"in-publish": "^2.0.1",
"iterate-iterator": "^1.0.2",
"make-generator-function": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var shimIterator = require('./Iterator/shim');
var shimIteratorFrom = require('./Iterator.from/shim');
var shimIteratorProto = require('./Iterator.prototype/shim');
var shimIteratorCtor = require('./Iterator.prototype.constructor/shim');
var shimIteratorDrop = require('./Iterator.prototype.drop/shim');
var shimIteratorEvery = require('./Iterator.prototype.every/shim');
Expand All @@ -18,6 +19,7 @@ var shimIteratorToArray = require('./Iterator.prototype.toArray/shim');
module.exports = function shimIteratorHelpers() {
shimIterator();
shimIteratorFrom();
shimIteratorProto();
shimIteratorCtor();
shimIteratorDrop();
shimIteratorEvery();
Expand Down
72 changes: 72 additions & 0 deletions test/Iterator.prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

var defineProperties = require('define-properties');
var test = require('tape');
var hasSymbols = require('has-symbols')();
var hasToStringTag = require('has-tostringtag');
var functionsHaveNames = require('functions-have-names')();
var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames();

var index = require('../Iterator.prototype');
var impl = require('../Iterator.prototype/implementation');

var isEnumerable = Object.prototype.propertyIsEnumerable;

var $Iterator = require('../Iterator/implementation');

module.exports = {
tests: function (proto, name, t) {
t.notEqual(proto, null, 'is not null');
t.equal(typeof proto, 'object', 'is an object');

t.test('Symbol.iterator', { skip: !hasSymbols }, function (st) {
st.equal(typeof proto[Symbol.iterator], 'function', 'has a `Symbol.iterator` method');
st.equal(
proto[Symbol.iterator].name,
'[Symbol.iterator]',
'has name "[Symbol.iterator]"',
{ skip: functionsHaveNames && !functionsHaveConfigurableNames }
);
st.equal(proto[Symbol.iterator](), proto, 'function returns proto');
st.equal(proto[Symbol.iterator].call($Iterator), $Iterator, 'function returns receiver');

st.end();
});

t.test(
'Symbol.toStringTag',
{ skip: !hasToStringTag || 'temporarily skipped pending https://bugs.chromium.org/p/chromium/issues/detail?id=1477372' },
function (st) {
st.equal(proto[Symbol.toStringTag], 'Iterator', 'has a `Symbol.toStringTag` property');

st.end();
}
);
},
index: function () {
test('Iterator.prototype: index', function (t) {
module.exports.tests(index, 'Iterator.prototype', t);

t.end();
});
},
implementation: function () {
test('Iterator.prototype: implementation', function (t) {
module.exports.tests(impl, 'Iterator.prototype', t);

t.end();
});
},
shimmed: function () {
test('Iterator.prototype: shimmed', function (t) {
t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(Iterator, 'prototype'), 'Iterator.prototype is not enumerable');
et.end();
});

module.exports.tests(Iterator.prototype, 'Iterator.prototype', t);

t.end();
});
}
};
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ forEach(shims, function (shim) {
try {
shimTests = require('./' + shim); // eslint-disable-line global-require
} catch (e) {
console.error(e);
test(shim + ': index', { todo: true });
}
if (shimTests) {
Expand Down

0 comments on commit c4a6203

Please sign in to comment.