GetIterator.js 803 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var getIteratorMethod = require('../helpers/getIteratorMethod');
  4. var AdvanceStringIndex = require('./AdvanceStringIndex');
  5. var Call = require('./Call');
  6. var GetMethod = require('./GetMethod');
  7. var IsArray = require('./IsArray');
  8. var Type = require('./Type');
  9. // https://262.ecma-international.org/6.0/#sec-getiterator
  10. module.exports = function GetIterator(obj, method) {
  11. var actualMethod = method;
  12. if (arguments.length < 2) {
  13. actualMethod = getIteratorMethod(
  14. {
  15. AdvanceStringIndex: AdvanceStringIndex,
  16. GetMethod: GetMethod,
  17. IsArray: IsArray
  18. },
  19. obj
  20. );
  21. }
  22. var iterator = Call(actualMethod, obj);
  23. if (Type(iterator) !== 'Object') {
  24. throw new $TypeError('iterator must return an object');
  25. }
  26. return iterator;
  27. };