Skip to content

Commit 5cb673a

Browse files
committed
Changeset: Turn stringAssembler() into a real class
1 parent 3a5266a commit 5cb673a

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
@@ -725,23 +725,20 @@ exports.stringIterator = (str) => {
725725

726726
/**
727727
* A custom made StringBuffer
728-
*
729-
* @typedef {object} StringAssembler
730-
* @property {Function} append -
731-
* @property {Function} toString -
732728
*/
729+
class StringAssembler {
730+
constructor() { this._str = ''; }
731+
/**
732+
* @param {string} x -
733+
*/
734+
append(x) { this._str += String(x); }
735+
toString() { return this._str; }
736+
}
733737

734738
/**
735739
* @returns {StringAssembler}
736740
*/
737-
exports.stringAssembler = () => ({
738-
_str: '',
739-
/**
740-
* @param {string} x -
741-
*/
742-
append(x) { this._str += String(x); },
743-
toString() { return this._str; },
744-
});
741+
exports.stringAssembler = () => new StringAssembler();
745742

746743
/**
747744
* @typedef {object} StringArrayLike
@@ -1170,7 +1167,7 @@ exports.applyToText = (cs, str) => {
11701167
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
11711168
const bankIter = exports.stringIterator(unpacked.charBank);
11721169
const strIter = exports.stringIterator(str);
1173-
const assem = exports.stringAssembler();
1170+
const assem = new StringAssembler();
11741171
for (const op of exports.deserializeOps(unpacked.ops)) {
11751172
switch (op.opcode) {
11761173
case '+':
@@ -1284,7 +1281,7 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
12841281
}
12851282
return '';
12861283
});
1287-
const buf = exports.stringAssembler();
1284+
const buf = new StringAssembler();
12881285
for (const att of sortAttribs([...atts])) {
12891286
buf.append('*');
12901287
buf.append(exports.numToString(pool.putAttrib(att)));
@@ -1509,7 +1506,7 @@ exports.compose = (cs1, cs2, pool) => {
15091506
const len3 = unpacked2.newLen;
15101507
const bankIter1 = exports.stringIterator(unpacked1.charBank);
15111508
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1512-
const bankAssem = exports.stringAssembler();
1509+
const bankAssem = new StringAssembler();
15131510

15141511
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
15151512
const op1code = op1.opcode;
@@ -1941,7 +1938,7 @@ class Builder {
19411938
constructor(oldLen) {
19421939
this._oldLen = oldLen;
19431940
this._ops = [];
1944-
this._charBank = exports.stringAssembler();
1941+
this._charBank = new StringAssembler();
19451942
}
19461943

19471944
/**
@@ -2184,7 +2181,7 @@ exports.inverse = (cs, lines, alines, pool) => {
21842181

21852182
const nextText = (numChars) => {
21862183
let len = 0;
2187-
const assem = exports.stringAssembler();
2184+
const assem = new StringAssembler();
21882185
const firstString = linesGet(curLine).substring(curChar);
21892186
len += firstString.length;
21902187
assem.append(firstString);
@@ -2410,7 +2407,7 @@ const followAttributes = (att1, att2, pool) => {
24102407
return '';
24112408
});
24122409
// we've only removed attributes, so they're already sorted
2413-
const buf = exports.stringAssembler();
2410+
const buf = new StringAssembler();
24142411
for (const att of atts) {
24152412
buf.append('*');
24162413
buf.append(exports.numToString(pool.putAttrib(att)));

0 commit comments

Comments
 (0)