string.js 1.3 KB

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