UNPKG

1.38 kBJavaScriptView Raw
1// Thanks @mathiasbynens
2// http://mathiasbynens.be/notes/javascript-unicode#iterating-over-symbols
3
4"use strict";
5
6var setPrototypeOf = require("es5-ext/object/set-prototype-of")
7 , d = require("d")
8 , Symbol = require("es6-symbol")
9 , Iterator = require("./");
10
11var defineProperty = Object.defineProperty, StringIterator;
12
13StringIterator = module.exports = function (str) {
14 if (!(this instanceof StringIterator)) throw new TypeError("Constructor requires 'new'");
15 str = String(str);
16 Iterator.call(this, str);
17 defineProperty(this, "__length__", d("", str.length));
18};
19if (setPrototypeOf) setPrototypeOf(StringIterator, Iterator);
20
21// Internal %ArrayIteratorPrototype% doesn't expose its constructor
22delete StringIterator.prototype.constructor;
23
24StringIterator.prototype = Object.create(Iterator.prototype, {
25 _next: d(function () {
26 if (!this.__list__) return undefined;
27 if (this.__nextIndex__ < this.__length__) return this.__nextIndex__++;
28 this._unBind();
29 return undefined;
30 }),
31 _resolve: d(function (i) {
32 var char = this.__list__[i], code;
33 if (this.__nextIndex__ === this.__length__) return char;
34 code = char.charCodeAt(0);
35 if (code >= 0xd800 && code <= 0xdbff) return char + this.__list__[this.__nextIndex__++];
36 return char;
37 })
38});
39defineProperty(StringIterator.prototype, Symbol.toStringTag, d("c", "String Iterator"));