103 lines
3.2 KiB
JavaScript
103 lines
3.2 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var Op_1 = __importDefault(require("./Op"));
|
|
var Iterator = /** @class */ (function () {
|
|
function Iterator(ops) {
|
|
this.ops = ops;
|
|
this.index = 0;
|
|
this.offset = 0;
|
|
}
|
|
Iterator.prototype.hasNext = function () {
|
|
return this.peekLength() < Infinity;
|
|
};
|
|
Iterator.prototype.next = function (length) {
|
|
if (!length) {
|
|
length = Infinity;
|
|
}
|
|
var nextOp = this.ops[this.index];
|
|
if (nextOp) {
|
|
var offset = this.offset;
|
|
var opLength = Op_1.default.length(nextOp);
|
|
if (length >= opLength - offset) {
|
|
length = opLength - offset;
|
|
this.index += 1;
|
|
this.offset = 0;
|
|
}
|
|
else {
|
|
this.offset += length;
|
|
}
|
|
if (typeof nextOp.delete === 'number') {
|
|
return { delete: length };
|
|
}
|
|
else {
|
|
var retOp = {};
|
|
if (nextOp.attributes) {
|
|
retOp.attributes = nextOp.attributes;
|
|
}
|
|
if (typeof nextOp.retain === 'number') {
|
|
retOp.retain = length;
|
|
}
|
|
else if (typeof nextOp.insert === 'string') {
|
|
retOp.insert = nextOp.insert.substr(offset, length);
|
|
}
|
|
else {
|
|
// offset should === 0, length should === 1
|
|
retOp.insert = nextOp.insert;
|
|
}
|
|
return retOp;
|
|
}
|
|
}
|
|
else {
|
|
return { retain: Infinity };
|
|
}
|
|
};
|
|
Iterator.prototype.peek = function () {
|
|
return this.ops[this.index];
|
|
};
|
|
Iterator.prototype.peekLength = function () {
|
|
if (this.ops[this.index]) {
|
|
// Should never return 0 if our index is being managed correctly
|
|
return Op_1.default.length(this.ops[this.index]) - this.offset;
|
|
}
|
|
else {
|
|
return Infinity;
|
|
}
|
|
};
|
|
Iterator.prototype.peekType = function () {
|
|
if (this.ops[this.index]) {
|
|
if (typeof this.ops[this.index].delete === 'number') {
|
|
return 'delete';
|
|
}
|
|
else if (typeof this.ops[this.index].retain === 'number') {
|
|
return 'retain';
|
|
}
|
|
else {
|
|
return 'insert';
|
|
}
|
|
}
|
|
return 'retain';
|
|
};
|
|
Iterator.prototype.rest = function () {
|
|
if (!this.hasNext()) {
|
|
return [];
|
|
}
|
|
else if (this.offset === 0) {
|
|
return this.ops.slice(this.index);
|
|
}
|
|
else {
|
|
var offset = this.offset;
|
|
var index = this.index;
|
|
var next = this.next();
|
|
var rest = this.ops.slice(this.index);
|
|
this.offset = offset;
|
|
this.index = index;
|
|
return [next].concat(rest);
|
|
}
|
|
};
|
|
return Iterator;
|
|
}());
|
|
exports.default = Iterator;
|
|
//# sourceMappingURL=Iterator.js.map
|