Skip to content

Commit e81979e

Browse files
committed
Changeset: Turn stringIterator() into a real class
1 parent 7b4ad85 commit e81979e

File tree

1 file changed

+45
-50
lines changed

1 file changed

+45
-50
lines changed

src/static/js/Changeset.js

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -670,57 +670,52 @@ exports.opAssembler = () => {
670670

671671
/**
672672
* A custom made String Iterator
673-
*
674-
* @typedef {object} StringIterator
675-
* @property {Function} newlines -
676-
* @property {Function} peek -
677-
* @property {Function} remaining -
678-
* @property {Function} skip -
679-
* @property {Function} take -
680673
*/
674+
class StringIterator {
675+
constructor(str) {
676+
this._str = str;
677+
this._curIndex = 0;
678+
// this._newLines is the number of \n between this._curIndex and this._str.length
679+
this._newLines = this._str.split('\n').length - 1;
680+
}
681681

682-
/**
683-
* @param {string} str - String to iterate over
684-
* @returns {StringIterator}
685-
*/
686-
exports.stringIterator = (str) => {
687-
let curIndex = 0;
688-
// newLines is the number of \n between curIndex and str.length
689-
let newLines = str.split('\n').length - 1;
690-
const getnewLines = () => newLines;
682+
newlines() {
683+
return this._newLines;
684+
}
691685

692-
const assertRemaining = (n) => {
693-
assert(n <= remaining(), `!(${n} <= ${remaining()})`);
694-
};
686+
_assertRemaining(n) {
687+
assert(n <= this.remaining(), `!(${n} <= ${this.remaining()})`);
688+
}
695689

696-
const take = (n) => {
697-
assertRemaining(n);
698-
const s = str.substr(curIndex, n);
699-
newLines -= s.split('\n').length - 1;
700-
curIndex += n;
690+
take(n) {
691+
this._assertRemaining(n);
692+
const s = this._str.substr(this._curIndex, n);
693+
this._newLines -= s.split('\n').length - 1;
694+
this._curIndex += n;
701695
return s;
702-
};
696+
}
703697

704-
const peek = (n) => {
705-
assertRemaining(n);
706-
const s = str.substr(curIndex, n);
698+
peek(n) {
699+
this._assertRemaining(n);
700+
const s = this._str.substr(this._curIndex, n);
707701
return s;
708-
};
702+
}
709703

710-
const skip = (n) => {
711-
assertRemaining(n);
712-
curIndex += n;
713-
};
704+
skip(n) {
705+
this._assertRemaining(n);
706+
this._curIndex += n;
707+
}
714708

715-
const remaining = () => str.length - curIndex;
716-
return {
717-
take,
718-
skip,
719-
remaining,
720-
peek,
721-
newlines: getnewLines,
722-
};
723-
};
709+
remaining() {
710+
return this._str.length - this._curIndex;
711+
}
712+
}
713+
714+
/**
715+
* @param {string} str - String to iterate over
716+
* @returns {StringIterator}
717+
*/
718+
exports.stringIterator = (str) => new StringIterator(str);
724719

725720
/**
726721
* A custom made StringBuffer
@@ -1167,8 +1162,8 @@ exports.pack = (oldLen, newLen, opsStr, bank) => {
11671162
exports.applyToText = (cs, str) => {
11681163
const unpacked = exports.unpack(cs);
11691164
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
1170-
const bankIter = exports.stringIterator(unpacked.charBank);
1171-
const strIter = exports.stringIterator(str);
1165+
const bankIter = new StringIterator(unpacked.charBank);
1166+
const strIter = new StringIterator(str);
11721167
let assem = '';
11731168
for (const op of exports.deserializeOps(unpacked.ops)) {
11741169
switch (op.opcode) {
@@ -1210,7 +1205,7 @@ exports.applyToText = (cs, str) => {
12101205
*/
12111206
exports.mutateTextLines = (cs, lines) => {
12121207
const unpacked = exports.unpack(cs);
1213-
const bankIter = exports.stringIterator(unpacked.charBank);
1208+
const bankIter = new StringIterator(unpacked.charBank);
12141209
const mut = new TextLinesMutator(lines);
12151210
for (const op of exports.deserializeOps(unpacked.ops)) {
12161211
switch (op.opcode) {
@@ -1514,8 +1509,8 @@ exports.compose = (cs1, cs2, pool) => {
15141509
const len2 = unpacked1.newLen;
15151510
assert(len2 === unpacked2.oldLen, 'mismatched composition of two changesets');
15161511
const len3 = unpacked2.newLen;
1517-
const bankIter1 = exports.stringIterator(unpacked1.charBank);
1518-
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1512+
const bankIter1 = new StringIterator(unpacked1.charBank);
1513+
const bankIter2 = new StringIterator(unpacked2.charBank);
15191514
let bankAssem = '';
15201515

15211516
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
@@ -1606,7 +1601,7 @@ const toSplices = (cs) => {
16061601
const splices = [];
16071602

16081603
let oldPos = 0;
1609-
const charIter = exports.stringIterator(unpacked.charBank);
1604+
const charIter = new StringIterator(unpacked.charBank);
16101605
let inSplice = false;
16111606
for (const op of exports.deserializeOps(unpacked.ops)) {
16121607
if (op.opcode === '=') {
@@ -2269,8 +2264,8 @@ exports.follow = (cs1, cs2, reverseInsertOrder, pool) => {
22692264
const len1 = unpacked1.oldLen;
22702265
const len2 = unpacked2.oldLen;
22712266
assert(len1 === len2, 'mismatched follow - cannot transform cs1 on top of cs2');
2272-
const chars1 = exports.stringIterator(unpacked1.charBank);
2273-
const chars2 = exports.stringIterator(unpacked2.charBank);
2267+
const chars1 = new StringIterator(unpacked1.charBank);
2268+
const chars2 = new StringIterator(unpacked2.charBank);
22742269

22752270
const oldLen = unpacked1.newLen;
22762271
let oldPos = 0;

0 commit comments

Comments
 (0)