From 42d4d8269c7e3c1323080aff3f49c2827f1c466b Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 21 Mar 2021 19:48:30 -0400 Subject: [PATCH] Changeset: Refactor `appendATextToAssembler()` for readability --- src/static/js/Changeset.js | 43 ++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index 395dbc2dd..9cf105760 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -1771,33 +1771,26 @@ exports.copyAText = (atext1, atext2) => { exports.appendATextToAssembler = (atext, assem) => { // intentionally skips last newline char of atext const iter = exports.opIterator(atext.attribs); + let lastOp = null; while (iter.hasNext()) { - const op = iter.next(); - if (!iter.hasNext()) { - // last op, exclude final newline - if (op.lines <= 1) { - op.lines = 0; - op.chars--; - if (op.chars) { - assem.append(op); - } - } else { - const nextToLastNewlineEnd = - atext.text.lastIndexOf('\n', atext.text.length - 2) + 1; - const lastLineLength = atext.text.length - nextToLastNewlineEnd - 1; - op.lines--; - op.chars -= (lastLineLength + 1); - assem.append(op); - op.lines = 0; - op.chars = lastLineLength; - if (op.chars) { - assem.append(op); - } - } - } else { - assem.append(op); - } + if (lastOp != null) assem.append(lastOp); + lastOp = iter.next(); } + if (lastOp == null) return; + // exclude final newline + if (lastOp.lines <= 1) { + lastOp.lines = 0; + lastOp.chars--; + } else { + const nextToLastNewlineEnd = atext.text.lastIndexOf('\n', atext.text.length - 2) + 1; + const lastLineLength = atext.text.length - nextToLastNewlineEnd - 1; + lastOp.lines--; + lastOp.chars -= (lastLineLength + 1); + assem.append(lastOp); + lastOp.lines = 0; + lastOp.chars = lastLineLength; + } + if (lastOp.chars) assem.append(lastOp); }; /**