Skip to content

Commit 6f5014a

Browse files
committed
Changeset: Turn stringAssembler() into a real class
1 parent afe8409 commit 6f5014a

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

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

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

745742
/**
746743
* @typedef {object} StringArrayLike
@@ -1169,7 +1166,7 @@ exports.applyToText = (cs, str) => {
11691166
assert(str.length === unpacked.oldLen, `mismatched apply: ${str.length} / ${unpacked.oldLen}`);
11701167
const bankIter = exports.stringIterator(unpacked.charBank);
11711168
const strIter = exports.stringIterator(str);
1172-
const assem = exports.stringAssembler();
1169+
const assem = new StringAssembler();
11731170
for (const op of exports.deserializeOps(unpacked.ops)) {
11741171
switch (op.opcode) {
11751172
case '+':
@@ -1283,7 +1280,7 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
12831280
}
12841281
return '';
12851282
});
1286-
const buf = exports.stringAssembler();
1283+
const buf = new StringAssembler();
12871284
for (const att of sortAttribs([...atts])) {
12881285
buf.append('*');
12891286
buf.append(exports.numToString(pool.putAttrib(att)));
@@ -1517,7 +1514,7 @@ exports.compose = (cs1, cs2, pool) => {
15171514
const len3 = unpacked2.newLen;
15181515
const bankIter1 = exports.stringIterator(unpacked1.charBank);
15191516
const bankIter2 = exports.stringIterator(unpacked2.charBank);
1520-
const bankAssem = exports.stringAssembler();
1517+
const bankAssem = new StringAssembler();
15211518

15221519
const newOps = applyZip(unpacked1.ops, unpacked2.ops, (op1, op2) => {
15231520
const op1code = op1.opcode;
@@ -1949,7 +1946,7 @@ class Builder {
19491946
constructor(oldLen) {
19501947
this._oldLen = oldLen;
19511948
this._ops = [];
1952-
this._charBank = exports.stringAssembler();
1949+
this._charBank = new StringAssembler();
19531950
}
19541951

19551952
/**
@@ -2204,7 +2201,7 @@ exports.inverse = (cs, lines, alines, pool) => {
22042201

22052202
const nextText = (numChars) => {
22062203
let len = 0;
2207-
const assem = exports.stringAssembler();
2204+
const assem = new StringAssembler();
22082205
const firstString = linesGet(curLine).substring(curChar);
22092206
len += firstString.length;
22102207
assem.append(firstString);
@@ -2430,7 +2427,7 @@ const followAttributes = (att1, att2, pool) => {
24302427
return '';
24312428
});
24322429
// we've only removed attributes, so they're already sorted
2433-
const buf = exports.stringAssembler();
2430+
const buf = new StringAssembler();
24342431
for (const att of atts) {
24352432
buf.append('*');
24362433
buf.append(exports.numToString(pool.putAttrib(att)));

0 commit comments

Comments
 (0)