GetIteratorFromMethod.js 737 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var Call = require('./Call');
  4. var GetV = require('./GetV');
  5. var IsCallable = require('./IsCallable');
  6. var isObject = require('../helpers/isObject');
  7. // https://262.ecma-international.org/14.0/#sec-getiteratorfrommethod
  8. module.exports = function GetIteratorFromMethod(obj, method) {
  9. if (!IsCallable(method)) {
  10. throw new $TypeError('method must be a function');
  11. }
  12. var iterator = Call(method, obj); // step 1
  13. if (!isObject(iterator)) {
  14. throw new $TypeError('iterator must return an object'); // step 2
  15. }
  16. var nextMethod = GetV(iterator, 'next'); // step 3
  17. return { // steps 4-5
  18. '[[Iterator]]': iterator,
  19. '[[NextMethod]]': nextMethod,
  20. '[[Done]]': false
  21. };
  22. };