6e054e097cf649fdacf8534a5134c1918b9fc0b69e774d23c510bd788b7dfa590b3a03a8a49f7ce1026b0b90f50ddc667134a97b711462a102ca3a84864410 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const Op_1 = require("./Op");
  4. class Iterator {
  5. constructor(ops) {
  6. this.ops = ops;
  7. this.index = 0;
  8. this.offset = 0;
  9. }
  10. hasNext() {
  11. return this.peekLength() < Infinity;
  12. }
  13. next(length) {
  14. if (!length) {
  15. length = Infinity;
  16. }
  17. const nextOp = this.ops[this.index];
  18. if (nextOp) {
  19. const offset = this.offset;
  20. const opLength = Op_1.default.length(nextOp);
  21. if (length >= opLength - offset) {
  22. length = opLength - offset;
  23. this.index += 1;
  24. this.offset = 0;
  25. }
  26. else {
  27. this.offset += length;
  28. }
  29. if (typeof nextOp.delete === 'number') {
  30. return { delete: length };
  31. }
  32. else {
  33. const retOp = {};
  34. if (nextOp.attributes) {
  35. retOp.attributes = nextOp.attributes;
  36. }
  37. if (typeof nextOp.retain === 'number') {
  38. retOp.retain = length;
  39. }
  40. else if (typeof nextOp.retain === 'object' &&
  41. nextOp.retain !== null) {
  42. // offset should === 0, length should === 1
  43. retOp.retain = nextOp.retain;
  44. }
  45. else if (typeof nextOp.insert === 'string') {
  46. retOp.insert = nextOp.insert.substr(offset, length);
  47. }
  48. else {
  49. // offset should === 0, length should === 1
  50. retOp.insert = nextOp.insert;
  51. }
  52. return retOp;
  53. }
  54. }
  55. else {
  56. return { retain: Infinity };
  57. }
  58. }
  59. peek() {
  60. return this.ops[this.index];
  61. }
  62. peekLength() {
  63. if (this.ops[this.index]) {
  64. // Should never return 0 if our index is being managed correctly
  65. return Op_1.default.length(this.ops[this.index]) - this.offset;
  66. }
  67. else {
  68. return Infinity;
  69. }
  70. }
  71. peekType() {
  72. const op = this.ops[this.index];
  73. if (op) {
  74. if (typeof op.delete === 'number') {
  75. return 'delete';
  76. }
  77. else if (typeof op.retain === 'number' ||
  78. (typeof op.retain === 'object' && op.retain !== null)) {
  79. return 'retain';
  80. }
  81. else {
  82. return 'insert';
  83. }
  84. }
  85. return 'retain';
  86. }
  87. rest() {
  88. if (!this.hasNext()) {
  89. return [];
  90. }
  91. else if (this.offset === 0) {
  92. return this.ops.slice(this.index);
  93. }
  94. else {
  95. const offset = this.offset;
  96. const index = this.index;
  97. const next = this.next();
  98. const rest = this.ops.slice(this.index);
  99. this.offset = offset;
  100. this.index = index;
  101. return [next].concat(rest);
  102. }
  103. }
  104. }
  105. exports.default = Iterator;
  106. //# sourceMappingURL=OpIterator.js.map