GetIterator.js 805 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 isObject = require('../helpers/isObject');
  9. var ES = {
  10. AdvanceStringIndex: AdvanceStringIndex,
  11. GetMethod: GetMethod,
  12. IsArray: IsArray
  13. };
  14. // https://262.ecma-international.org/6.0/#sec-getiterator
  15. module.exports = function GetIterator(obj, method) {
  16. var actualMethod = method;
  17. if (arguments.length < 2) {
  18. actualMethod = getIteratorMethod(ES, obj);
  19. }
  20. var iterator = Call(actualMethod, obj);
  21. if (!isObject(iterator)) {
  22. throw new $TypeError('iterator must return an object');
  23. }
  24. return iterator;
  25. };