UNPKG

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