You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getIteratorMethod.js 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var hasSymbols = require('has-symbols')();
  3. var GetIntrinsic = require('../GetIntrinsic');
  4. var callBound = require('./callBound');
  5. var $iterator = GetIntrinsic('%Symbol.iterator%', true);
  6. var $stringSlice = callBound('String.prototype.slice');
  7. module.exports = function getIteratorMethod(ES, iterable) {
  8. var usingIterator;
  9. if (hasSymbols) {
  10. usingIterator = ES.GetMethod(iterable, $iterator);
  11. } else if (ES.IsArray(iterable)) {
  12. usingIterator = function () {
  13. var i = -1;
  14. var arr = this; // eslint-disable-line no-invalid-this
  15. return {
  16. next: function () {
  17. i += 1;
  18. return {
  19. done: i >= arr.length,
  20. value: arr[i]
  21. };
  22. }
  23. };
  24. };
  25. } else if (ES.Type(iterable) === 'String') {
  26. usingIterator = function () {
  27. var i = 0;
  28. return {
  29. next: function () {
  30. var nextIndex = ES.AdvanceStringIndex(iterable, i, true);
  31. var value = $stringSlice(iterable, i, nextIndex);
  32. i = nextIndex;
  33. return {
  34. done: nextIndex > iterable.length,
  35. value: value
  36. };
  37. }
  38. };
  39. };
  40. }
  41. return usingIterator;
  42. };