es.string.iterator.js 1.0 KB

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