Skip to content

Instantly share code, notes, and snippets.

@intact
Created November 21, 2019 16:03
Show Gist options
  • Save intact/16ea3d115adcd51506ee23627bc8bb24 to your computer and use it in GitHub Desktop.
Save intact/16ea3d115adcd51506ee23627bc8bb24 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
var root = void 0;
try {
root = self;
} catch (e) {
try {
root = global;
} catch (e) {
root = window;
}
}
function _classCallCheck$1(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Iterator = function () {
Iterator.prototype.next = function next() {
this.done = this.cursor === this.collection.length;
var result = { done: this.done, value: this.done ? undefined : this.kind === "value" || this.kind === "key" ? this.collection[this.cursor] : [this.cursor, this.collection[this.cursor]] };
this.cursor++;
return result;
};
function Iterator(collection, kind) {
_classCallCheck$1(this, Iterator);
this.cursor = 0;
this.collection = collection;
this.kind = kind;
}
return Iterator;
}();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var StringIterator = function (_Iterator) {
_inherits(StringIterator, _Iterator);
function StringIterator(collection, kind) {
_classCallCheck(this, StringIterator);
var _this = _possibleConstructorReturn(this, _Iterator.call(this, collection, kind));
_this;
return _this;
}
StringIterator.prototype.toString = function toString() {
return "[object String Iterator]";
};
return StringIterator;
}(Iterator);
function _classCallCheck$2(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn$1(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits$1(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ArrayIterator = function (_Iterator) {
_inherits$1(ArrayIterator, _Iterator);
function ArrayIterator(collection, kind) {
_classCallCheck$2(this, ArrayIterator);
var _this = _possibleConstructorReturn$1(this, _Iterator.call(this, collection, kind));
_this;
return _this;
}
ArrayIterator.prototype.toString = function toString() {
return "[object Array Iterator]";
};
return ArrayIterator;
}(Iterator);
// $FlowFixMe
if (!(typeof Array.prototype[Symbol.iterator] === "function")) {
var HAS_VALUES_METHOD = "values" in Array.prototype;
var HAS_ENTRIES_METHOD = "entries" in Array.prototype;
var HAS_KEYS_METHOD = "keys" in Array.prototype;
// $FlowFixMe
if (!HAS_VALUES_METHOD) Array.prototype.values = function values() {
return new ArrayIterator(this, "value");
};
// $FlowFixMe
if (!HAS_ENTRIES_METHOD) Array.prototype.entries = function entries() {
return new ArrayIterator(this, "key+value");
};
// $FlowFixMe
if (!HAS_KEYS_METHOD) Array.prototype.keys = function keys() {
return new ArrayIterator(this, "key");
};
// $FlowFixMe
Array.prototype[Symbol.iterator] = Array.prototype.values;
}
// $FlowFixMe
if (!(typeof String.prototype[Symbol.iterator] === "function")) {
// $FlowFixMe
String.prototype[Symbol.iterator] = function iterator() {
return new StringIterator(this, "value");
};
}
// $FlowFixMe
if (!(typeof NodeList.prototype[Symbol.iterator] === "function")) {
var _HAS_VALUES_METHOD = "values" in NodeList.prototype;
var _HAS_ENTRIES_METHOD = "entries" in NodeList.prototype;
var _HAS_KEYS_METHOD = "keys" in NodeList.prototype;
// $FlowFixMe
if (!_HAS_VALUES_METHOD) NodeList.prototype.values = function values() {
return new ArrayIterator(this, "value");
};
// $FlowFixMe
if (!_HAS_ENTRIES_METHOD) NodeList.prototype.entries = function entries() {
return new ArrayIterator(this, "key+value");
};
// $FlowFixMe
if (!_HAS_KEYS_METHOD) NodeList.prototype.keys = function keys() {
return new ArrayIterator(this, "key");
};
// $FlowFixMe
NodeList.prototype[Symbol.iterator] = Array.prototype.values;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment