GetIterator.js 872 B

1234567891011121314151617181920212223242526272829303132333435
  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. Type: Type
  20. },
  21. obj
  22. );
  23. }
  24. var iterator = Call(actualMethod, obj);
  25. if (Type(iterator) !== 'Object') {
  26. throw new $TypeError('iterator must return an object');
  27. }
  28. return iterator;
  29. };