esnext.string.code-points.js 1.5 KB

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