GetIterator.js 856 B

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