es.string.iterator.js 1.1 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var charAt = require('../internals/string-multibyte').charAt;
  3. var toString = require('../internals/to-string');
  4. var InternalStateModule = require('../internals/internal-state');
  5. var defineIterator = require('../internals/define-iterator');
  6. var STRING_ITERATOR = 'String Iterator';
  7. var setInternalState = InternalStateModule.set;
  8. var getInternalState = InternalStateModule.getterFor(STRING_ITERATOR);
  9. // `String.prototype[@@iterator]` method
  10. // https://tc39.es/ecma262/#sec-string.prototype-@@iterator
  11. defineIterator(String, 'String', function (iterated) {
  12. setInternalState(this, {
  13. type: STRING_ITERATOR,
  14. string: toString(iterated),
  15. index: 0
  16. });
  17. // `%StringIteratorPrototype%.next` method
  18. // https://tc39.es/ecma262/#sec-%stringiteratorprototype%.next
  19. }, function next() {
  20. var state = getInternalState(this);
  21. var string = state.string;
  22. var index = state.index;
  23. var point;
  24. if (index >= string.length) return { value: undefined, done: true };
  25. point = charAt(string, index);
  26. state.index += point.length;
  27. return { value: point, done: false };
  28. });