esnext.string.code-points.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var createIteratorConstructor = require('../internals/iterator-create-constructor');
  4. var createIterResultObject = require('../internals/create-iter-result-object');
  5. var requireObjectCoercible = require('../internals/require-object-coercible');
  6. var toString = require('../internals/to-string');
  7. var InternalStateModule = require('../internals/internal-state');
  8. var StringMultibyteModule = require('../internals/string-multibyte');
  9. var codeAt = StringMultibyteModule.codeAt;
  10. var charAt = StringMultibyteModule.charAt;
  11. var STRING_ITERATOR = 'String Iterator';
  12. var setInternalState = InternalStateModule.set;
  13. var getInternalState = InternalStateModule.getterFor(STRING_ITERATOR);
  14. // TODO: unify with String#@@iterator
  15. var $StringIterator = createIteratorConstructor(function StringIterator(string) {
  16. setInternalState(this, {
  17. type: STRING_ITERATOR,
  18. string: string,
  19. index: 0
  20. });
  21. }, 'String', function next() {
  22. var state = getInternalState(this);
  23. var string = state.string;
  24. var index = state.index;
  25. var point;
  26. if (index >= string.length) return createIterResultObject(undefined, true);
  27. point = charAt(string, index);
  28. state.index += point.length;
  29. return createIterResultObject({ codePoint: codeAt(point, 0), position: index }, false);
  30. });
  31. // `String.prototype.codePoints` method
  32. // https://github.com/tc39/proposal-string-prototype-codepoints
  33. $({ target: 'String', proto: true, forced: true }, {
  34. codePoints: function codePoints() {
  35. return new $StringIterator(toString(requireObjectCoercible(this)));
  36. }
  37. });