IteratorToList.js 882 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var callBound = require('call-bind/callBound');
  4. var $arrayPush = callBound('Array.prototype.push');
  5. var IteratorStep = require('./IteratorStep');
  6. var IteratorValue = require('./IteratorValue');
  7. var isIteratorRecord = require('../helpers/records/iterator-record');
  8. // https://262.ecma-international.org/14.0/#sec-iteratortolist
  9. module.exports = function IteratorToList(iteratorRecord) {
  10. if (!isIteratorRecord(iteratorRecord)) {
  11. throw new $TypeError('Assertion failed: `iteratorRecord` must be an Iterator Record'); // step 1
  12. }
  13. var values = []; // step 1
  14. var next = true; // step 2
  15. while (next) { // step 3
  16. next = IteratorStep(iteratorRecord); // step 3.a
  17. if (next) {
  18. var nextValue = IteratorValue(next); // step 3.b.i
  19. $arrayPush(values, nextValue); // step 3.b.ii
  20. }
  21. }
  22. return values; // step 4
  23. };