Skip to content

Commit 9a35f16

Browse files
committed
Changeset: Move stringAssembler() logic to a new class
1 parent 4bac8b5 commit 9a35f16

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/static/js/Changeset.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -717,23 +717,20 @@ exports.stringIterator = (str) => {
717717

718718
/**
719719
* A custom made StringBuffer
720-
*
721-
* @typedef {object} StringAssembler
722-
* @property {Function} append -
723-
* @property {Function} toString -
724720
*/
721+
class StringAssembler {
722+
constructor() { this._str = ''; }
723+
/**
724+
* @param {string} x -
725+
*/
726+
append(x) { this._str += String(x); }
727+
toString() { return this._str; }
728+
}
725729

726730
/**
727731
* @returns {StringAssembler}
728732
*/
729-
exports.stringAssembler = () => ({
730-
_str: '',
731-
/**
732-
* @param {string} x -
733-
*/
734-
append(x) { this._str += String(x); },
735-
toString() { return this._str; },
736-
});
733+
exports.stringAssembler = () => new StringAssembler();
737734

738735
/**
739736
* Class to iterate and modify texts which have several lines. It is used for applying Changesets on
@@ -1145,7 +1142,7 @@ exports.applyToText = (cs, str) => {
11451142
assert(str.length === unpacked.oldLen, 'mismatched apply: ', str.length, ' / ', unpacked.oldLen);
11461143
const bankIter = exports.stringIterator(unpacked.charBank);
11471144
const strIter = exports.stringIterator(str);
1148-
const assem = exports.stringAssembler();
1145+
const assem = new StringAssembler();
11491146
for (const op of new exports.OpIter(unpacked.ops)) {
11501147
switch (op.opcode) {
11511148
case '+':
@@ -1250,7 +1247,7 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
12501247
}
12511248
return '';
12521249
});
1253-
const buf = exports.stringAssembler();
1250+
const buf = new StringAssembler();
12541251
for (const att of [...atts].sort((a, b) => (a[0] > b[0]) - (a[0] < b[0]))) {
12551252
buf.append('*');
12561253
buf.append(exports.numToString(pool.putAttrib(att)));
@@ -1471,7 +1468,7 @@ exports.compose = (cs1, cs2, pool) => {
14711468
const len3 = unpacked2.newLen;
14721469
const bankIter1 = exports.stringIterator(unpacked1.charBank);
14731470
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1474-
const bankAssem = exports.stringAssembler();
1471+
const bankAssem = new StringAssembler();
14751472

14761473
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
14771474
const op1code = op1.opcode;
@@ -1905,7 +1902,7 @@ exports.Builder = class {
19051902
constructor(oldLen) {
19061903
this._oldLen = oldLen;
19071904
this._ops = [];
1908-
this._charBank = exports.stringAssembler();
1905+
this._charBank = new StringAssembler();
19091906
}
19101907

19111908
/**
@@ -2106,7 +2103,7 @@ exports.inverse = (cs, lines, alines, pool) => {
21062103

21072104
const nextText = (numChars) => {
21082105
let len = 0;
2109-
const assem = exports.stringAssembler();
2106+
const assem = new StringAssembler();
21102107
const firstString = linesGet(curLine).substring(curChar);
21112108
len += firstString.length;
21122109
assem.append(firstString);
@@ -2332,7 +2329,7 @@ const followAttributes = (att1, att2, pool) => {
23322329
return '';
23332330
});
23342331
// we've only removed attributes, so they're already sorted
2335-
const buf = exports.stringAssembler();
2332+
const buf = new StringAssembler();
23362333
for (const att of atts) {
23372334
buf.append('*');
23382335
buf.append(exports.numToString(pool.putAttrib(att)));

0 commit comments

Comments
 (0)