1
0

string.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. , Iterator = require('./')
  7. , defineProperty = Object.defineProperty
  8. , StringIterator;
  9. StringIterator = module.exports = function (str) {
  10. if (!(this instanceof StringIterator)) return new StringIterator(str);
  11. str = String(str);
  12. Iterator.call(this, str);
  13. defineProperty(this, '__length__', d('', str.length));
  14. };
  15. if (setPrototypeOf) setPrototypeOf(StringIterator, Iterator);
  16. StringIterator.prototype = Object.create(Iterator.prototype, {
  17. constructor: d(StringIterator),
  18. _next: d(function () {
  19. if (!this.__list__) return;
  20. if (this.__nextIndex__ < this.__length__) return this.__nextIndex__++;
  21. this._unBind();
  22. }),
  23. _resolve: d(function (i) {
  24. var char = this.__list__[i], code;
  25. if (this.__nextIndex__ === this.__length__) return char;
  26. code = char.charCodeAt(0);
  27. if ((code >= 0xD800) && (code <= 0xDBFF)) return char + this.__list__[this.__nextIndex__++];
  28. return char;
  29. }),
  30. toString: d(function () { return '[object String Iterator]'; })
  31. });