Skip to content

Commit

Permalink
[New] add Iterator.zip, Iterator.zipKeyed
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 4, 2024
1 parent dd14f1b commit d11073f
Show file tree
Hide file tree
Showing 23 changed files with 697 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"new-cap": [2, {
"capIsNewExceptions": [
"Call",
"CreateDataPropertyOrThrow",
"CreateIteratorFromClosure",
"CreateIterResultObject",
"GeneratorResume",
Expand All @@ -29,18 +30,25 @@
"GeneratorValidate",
"Get",
"GetIntrinsic",
"GetIterator",
"GetIteratorDirect",
"GetIteratorFlattenable",
"GetMethod",
"GetOptionsObject",
"IfAbruptCloseIterators",
"IsAccessorDescriptor",
"IsArray",
"IsCallable",
"IsDataDescriptor",
"IteratorClose",
"IteratorCloseAll",
"IteratorStep",
"IteratorStepValue",
"IteratorZip",
"NormalCompletion",
"OrdinaryHasInstance",
"OrdinaryObjectCreate",
"ReturnCompletion",
"StringToCodePoints",
"ThrowCompletion",
"ToBoolean",
Expand All @@ -64,5 +72,19 @@
"import/no-extraneous-dependencies": [2, { "devDependencies": true }],
},
},
{
"files": "Iterator.zip*/implementation.js",
"rules": {
"complexity": "off",
"max-depth": "off",
},
},
{
"files": "aos/IteratorZip.js",
"rules": {
"max-depth": "off",
"max-params": "off",
},
},
],
}
2 changes: 1 addition & 1 deletion Iterator.from/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function from(O) {
throw new $TypeError('`Iterator.from` is not a constructor');
}

var iteratorRecord = GetIteratorFlattenable(O, 'iterate-strings'); // step 1
var iteratorRecord = GetIteratorFlattenable(O, 'ITERATE-STRINGS'); // step 1

var hasInstance = OrdinaryHasInstance($Iterator, iteratorRecord['[[Iterator]]']); // step 2

Expand Down
2 changes: 1 addition & 1 deletion Iterator.prototype.flatMap/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = function flatMap(mapper) {
try {
var mapped = Call(mapper, void undefined, [value, counter]); // step 5.b.iv
// yield mapped // step 5.b.vi
innerIterator = GetIteratorFlattenable(mapped, 'reject-strings'); // step 5.b.vi
innerIterator = GetIteratorFlattenable(mapped, 'REJECT-STRINGS'); // step 5.b.vi
} catch (e) {
innerAlive = false;
innerIterator = sentinel;
Expand Down
3 changes: 3 additions & 0 deletions Iterator.zip/auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

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

var $TypeError = require('es-errors/type');

var Get = require('es-abstract/2024/Get');
var GetIterator = require('es-abstract/2024/GetIterator');
var GetIteratorFlattenable = require('../aos/GetIteratorFlattenable');
var GetOptionsObject = require('../aos/GetOptionsObject');
var IfAbruptCloseIterators = require('../aos/IfAbruptCloseIterators');
var IteratorClose = require('es-abstract/2024/IteratorClose');
var IteratorStepValue = require('es-abstract/2024/IteratorStepValue');
var IteratorZip = require('../aos/IteratorZip');
var NormalCompletion = require('es-abstract/2024/NormalCompletion');
var ThrowCompletion = require('es-abstract/2024/ThrowCompletion');
var Type = require('es-abstract/2024/Type');

module.exports = function zip(iterables) {
if (this instanceof zip) {
throw new $TypeError('`Iterator.zip` is not a constructor');
}

if (Type(iterables) !== 'Object') {
throw new $TypeError('`iterables` must be an Object'); // step 1
}

var options = GetOptionsObject(arguments.length > 1 ? arguments[1] : undefined); // step 2

var mode = Get(options, 'mode'); // step 3

if (typeof mode === 'undefined') {
mode = 'shortest'; // step 4
}

if (mode !== 'shortest' && mode !== 'longest' && mode !== 'strict') {
throw new $TypeError('`mode` must be one of "shortest", "longest", or "strict"'); // step 5
}

var paddingOption; // step 6

if (mode === 'longest') {
paddingOption = Get(options, 'padding'); // step 7
if (typeof paddingOption !== 'undefined' && Type(paddingOption) !== 'Object') {
throw new $TypeError('`padding` option must be an Object'); // step 7.1
}
}

var iters = []; // step 8

var padding = []; // step 9

var inputIter = GetIterator(iterables, 'SYNC'); // step 10

var next; // = 'NOT-STARTED'; // step 11

while (!inputIter['[[Done]]']) { // step 12
try {
next = IteratorStepValue(inputIter); // step 12.a
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 12.b
}

if (!inputIter['[[Done]]']) { // step 12.c
var iter;
try {
iter = GetIteratorFlattenable(next, 'REJECT-STRINGS'); // step 12.c.i
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), [].concat(inputIter, iters)); // step 12.c.ii
}
iters[iters.length] = iter; // step 12.c.iii
}
}

var iterCount = iters.length; // step 13

if (mode === 'longest') { // step 14
if (typeof paddingOption === 'undefined') { // step 14.a
for (var i = 0; i < iterCount; i++) { // step 14.a.i
padding[padding.length] = void undefined; // step 14.a.i.1
}
} else { // step 14.b
var paddingIter;
try {
paddingIter = GetIterator(paddingOption, 'SYNC'); // step 14.b.i
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.ii
}
var usingIterator = true; // step 14.b.iii
for (var j = 0; j < iterCount; j++) { // step 14.b.iv
if (usingIterator) { // step 14.b.iv.1
try {
next = IteratorStepValue(paddingIter); // step 14.b.iv.1.a
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.iv.1.b
}
if (paddingIter['[[Done]]']) { // step 14.b.iv.1.c
usingIterator = false; // step 14.b.iv.1.c.i
} else { // step 14.b.iv.1.d
padding[padding.length] = next; // step 14.b.iv.1.d.i
}
}
if (!usingIterator) {
padding[padding.length] = void undefined; // step 14.b.iv.2
}
}

if (usingIterator) { // step 14.b.v
try {
IteratorClose(paddingIter, NormalCompletion(undefined)); // step 14.b.v.1
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.v.2
}
}
}
}

// eslint-disable-next-line no-sequences
var finishResults = (0, function (results) { // step 15
return results; // step 15.1
});

return IteratorZip(iters, mode, padding, finishResults); // step 16
};
5 changes: 5 additions & 0 deletions Iterator.zip/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

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

module.exports = getPolyfill();
9 changes: 9 additions & 0 deletions Iterator.zip/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

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

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

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

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

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

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

return polyfill;
};
3 changes: 3 additions & 0 deletions Iterator.zipKeyed/auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

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

var $TypeError = require('es-errors/type');

var Call = require('es-abstract/2024/Call');
var CreateDataPropertyOrThrow = require('es-abstract/2024/CreateDataPropertyOrThrow');
var Get = require('es-abstract/2024/Get');
var GetIteratorFlattenable = require('../aos/GetIteratorFlattenable');
var GetOptionsObject = require('../aos/GetOptionsObject');
var IfAbruptCloseIterators = require('../aos/IfAbruptCloseIterators');
var IsAccessorDescriptor = require('es-abstract/2024/IsAccessorDescriptor');
var IsDataDescriptor = require('es-abstract/2024/IsDataDescriptor');
var IteratorZip = require('../aos/IteratorZip');
var OrdinaryObjectCreate = require('es-abstract/2024/OrdinaryObjectCreate');
var ThrowCompletion = require('es-abstract/2024/ThrowCompletion');
var Type = require('es-abstract/2024/Type');

var forEach = require('es-abstract/helpers/forEach');
var ownKeys = require('es-abstract/helpers/OwnPropertyKeys');

var gOPD = require('gopd');

module.exports = function zipKeyed(iterables) {
if (this instanceof zipKeyed) {
throw new $TypeError('`Iterator.zip` is not a constructor');
}

if (Type(iterables) !== 'Object') {
throw new $TypeError('`iterables` must be an Object'); // step 1
}

var options = GetOptionsObject(arguments.length > 1 ? arguments[1] : undefined); // step 2

var mode = Get(options, 'mode'); // step 3

if (typeof mode === 'undefined') {
mode = 'shortest'; // step 4
}

if (mode !== 'shortest' && mode !== 'longest' && mode !== 'strict') {
throw new $TypeError('`mode` must be one of "shortest", "longest", or "strict"'); // step 5
}

var paddingOption; // step 6

if (mode === 'longest') {
paddingOption = Get(options, 'padding'); // step 7
if (typeof paddingOption !== 'undefined' && Type(paddingOption) !== 'Object') {
throw new $TypeError('`padding` option must be an Object'); // step 7.1
}
}

var iters = []; // step 8

var padding = []; // step 9

var allKeys = ownKeys(iterables); // step 10

var keys = []; // step 11

forEach(allKeys, function (key) { // step 12
var desc;
try {
desc = gOPD(iterables, key); // step 12.a
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 12.b
}

if (typeof desc !== 'undefined' && desc['[[Enumerable]]'] === true) { // step 12.c
var value; // step 12.c.i
if (IsDataDescriptor(desc)) { // step 12.c.ii
value = desc['[[Value]]']; // step 12.c.ii.1
} else {
if (!IsAccessorDescriptor(desc)) {
throw new $TypeError('Assertion failed: IsAccessorDescriptor(desc) is not true'); // step 12.c.ii.1
}
var getter = desc['[[Get]]']; // step 12.c.iii.2
if (typeof getter !== 'undefined') { // step 12.c.iii.3
var getterResult;
try {
getterResult = Call(getter, iterables); // step 12.c.iii.3.a
} catch (e) {
// step 12.c.iii.3.b
// 2. IfAbruptCloseIterators(e, iters).
}
value = getterResult; // step 12.c.iii.3.c
}
}
if (typeof value !== 'undefined') { // step 12.c.iv
keys[keys.length] = key; // step 12.c.iv.1
var iter;
try {
iter = GetIteratorFlattenable(value, 'REJECT-STRINGS'); // step 12.c.iv.2
} catch (e) {
// step 12.c.iv.3
// 3. IfAbruptCloseIterators(e, iters).
}
iters[iters.length] = iter; // step 12.c.iv.4
}
}
});

var iterCount = iters.length; // step 13

if (mode === 'longest') { // step 14
if (typeof paddingOption === 'undefined') { // step 14.a
for (var j = 0; j < iterCount; j += 1) { // step 14.a.i
padding[padding.length] = void undefined; // step 14.a.i.1
}
} else { // step 14.b
forEach(keys, function (key) { // step 14.b.i
var value;
try {
value = Get(paddingOption, key); // step 14.b.i.1
} catch (e) {
IfAbruptCloseIterators(ThrowCompletion(e), iters); // step 14.b.i.2
}
padding[padding.length] = value; // step 14.b.i.3
});
}
}

// eslint-disable-next-line no-sequences
var finishResults = (0, function (results) { // step 15
var obj = OrdinaryObjectCreate(null); // step 15.a
for (var i = 0; i < iterCount; i += 1) { // step 15.b
CreateDataPropertyOrThrow(obj, keys[i], results[i]); // step 15.b.i
}
return obj; // step 15.c
});

return IteratorZip(iters, mode, padding, finishResults); // step 16
};
5 changes: 5 additions & 0 deletions Iterator.zipKeyed/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

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

module.exports = getPolyfill();
9 changes: 9 additions & 0 deletions Iterator.zipKeyed/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

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

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

module.exports = function getPolyfill() {
return typeof $Iterator.zipKeyed === 'function' ? $Iterator.zipKeyed : implementation;
};
Loading

0 comments on commit d11073f

Please sign in to comment.