wallet-core/node_modules/es6-iterator/string.js

40 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-08-14 05:01:11 +02:00
// Thanks @mathiasbynens
// http://mathiasbynens.be/notes/javascript-unicode#iterating-over-symbols
2017-12-10 21:51:33 +01:00
"use strict";
2017-08-14 05:01:11 +02:00
2017-12-10 21:51:33 +01:00
var setPrototypeOf = require("es5-ext/object/set-prototype-of")
, d = require("d")
, Symbol = require("es6-symbol")
, Iterator = require("./");
2017-08-14 05:01:11 +02:00
2017-12-10 21:51:33 +01:00
var defineProperty = Object.defineProperty, StringIterator;
2017-08-14 05:01:11 +02:00
StringIterator = module.exports = function (str) {
2017-12-10 21:51:33 +01:00
if (!(this instanceof StringIterator)) throw new TypeError("Constructor requires 'new'");
2017-08-14 05:01:11 +02:00
str = String(str);
Iterator.call(this, str);
2017-12-10 21:51:33 +01:00
defineProperty(this, "__length__", d("", str.length));
2017-08-14 05:01:11 +02:00
};
if (setPrototypeOf) setPrototypeOf(StringIterator, Iterator);
2017-12-10 21:51:33 +01:00
// Internal %ArrayIteratorPrototype% doesn't expose its constructor
delete StringIterator.prototype.constructor;
2017-08-14 05:01:11 +02:00
StringIterator.prototype = Object.create(Iterator.prototype, {
_next: d(function () {
2017-12-10 21:51:33 +01:00
if (!this.__list__) return undefined;
2017-08-14 05:01:11 +02:00
if (this.__nextIndex__ < this.__length__) return this.__nextIndex__++;
this._unBind();
2017-12-10 21:51:33 +01:00
return undefined;
2017-08-14 05:01:11 +02:00
}),
_resolve: d(function (i) {
var char = this.__list__[i], code;
if (this.__nextIndex__ === this.__length__) return char;
code = char.charCodeAt(0);
2017-12-10 21:51:33 +01:00
if (code >= 0xd800 && code <= 0xdbff) return char + this.__list__[this.__nextIndex__++];
2017-08-14 05:01:11 +02:00
return char;
2017-12-10 21:51:33 +01:00
})
2017-08-14 05:01:11 +02:00
});
2017-12-10 21:51:33 +01:00
defineProperty(StringIterator.prototype, Symbol.toStringTag, d("c", "String Iterator"));