esnext.string.code-points.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var createIteratorConstructor = require('../internals/create-iterator-constructor');
  4. var requireObjectCoercible = require('../internals/require-object-coercible');
  5. var InternalStateModule = require('../internals/internal-state');
  6. var StringMultibyteModule = require('../internals/string-multibyte');
  7. var codeAt = StringMultibyteModule.codeAt;
  8. var charAt = StringMultibyteModule.charAt;
  9. var STRING_ITERATOR = 'String Iterator';
  10. var setInternalState = InternalStateModule.set;
  11. var getInternalState = InternalStateModule.getterFor(STRING_ITERATOR);
  12. // TODO: unify with String#@@iterator
  13. var $StringIterator = createIteratorConstructor(function StringIterator(string) {
  14. setInternalState(this, {
  15. type: STRING_ITERATOR,
  16. string: string,
  17. index: 0
  18. });
  19. }, 'String', 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: { codePoint: codeAt(point, 0), position: index }, done: false };
  28. });
  29. // `String.prototype.codePoints` method
  30. // https://github.com/tc39/proposal-string-prototype-codepoints
  31. $({ target: 'String', proto: true }, {
  32. codePoints: function codePoints() {
  33. return new $StringIterator(String(requireObjectCoercible(this)));
  34. }
  35. });