mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-22 08:26:16 -04:00
Changeset: Invert conditions to improve readability
This commit is contained in:
parent
b29e59419e
commit
7fa9b07116
1 changed files with 169 additions and 202 deletions
|
@ -425,7 +425,7 @@ exports.mergingOpAssembler = () => {
|
|||
let bufOpAdditionalCharsAfterNewline = 0;
|
||||
|
||||
const flush = (isEndDocument) => {
|
||||
if (bufOp.opcode) {
|
||||
if (!bufOp.opcode) return;
|
||||
if (isEndDocument && bufOp.opcode === '=' && !bufOp.attribs) {
|
||||
// final merged keep, leave it implicit
|
||||
} else {
|
||||
|
@ -438,11 +438,10 @@ exports.mergingOpAssembler = () => {
|
|||
}
|
||||
}
|
||||
bufOp.opcode = '';
|
||||
}
|
||||
};
|
||||
|
||||
const append = (op) => {
|
||||
if (op.chars > 0) {
|
||||
if (op.chars <= 0) return;
|
||||
if (bufOp.opcode === op.opcode && bufOp.attribs === op.attribs) {
|
||||
if (op.lines > 0) {
|
||||
// bufOp and additional chars are all mergeable into a multi-line op
|
||||
|
@ -460,7 +459,6 @@ exports.mergingOpAssembler = () => {
|
|||
flush();
|
||||
copyOp(op, bufOp);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const endDocument = () => {
|
||||
|
@ -762,11 +760,9 @@ const textLinesMutator = (lines) => {
|
|||
* @param {boolean} includeInSplice - indicates if attributes are present
|
||||
*/
|
||||
const skipLines = (L, includeInSplice) => {
|
||||
if (L) {
|
||||
if (!L) return;
|
||||
if (includeInSplice) {
|
||||
if (!inSplice) {
|
||||
enterSplice();
|
||||
}
|
||||
if (!inSplice) enterSplice();
|
||||
// TODO(doc) should this count the number of characters that are skipped to check?
|
||||
for (let i = 0; i < L; i++) {
|
||||
curCol = 0;
|
||||
|
@ -786,7 +782,6 @@ const textLinesMutator = (lines) => {
|
|||
curCol = 0;
|
||||
}
|
||||
// tests case foo in remove(), which isn't otherwise covered in current impl
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -797,13 +792,11 @@ const textLinesMutator = (lines) => {
|
|||
* @param {boolean} includeInSplice - indicates if attributes are present
|
||||
*/
|
||||
const skip = (N, L, includeInSplice) => {
|
||||
if (N) {
|
||||
if (!N) return;
|
||||
if (L) {
|
||||
skipLines(L, includeInSplice);
|
||||
} else {
|
||||
if (includeInSplice && !inSplice) {
|
||||
enterSplice();
|
||||
}
|
||||
if (includeInSplice && !inSplice) enterSplice();
|
||||
if (inSplice) {
|
||||
// although the line is put into splice curLine is not increased, because
|
||||
// only some chars are skipped, not the whole line
|
||||
|
@ -811,7 +804,6 @@ const textLinesMutator = (lines) => {
|
|||
}
|
||||
curCol += N;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -821,11 +813,8 @@ const textLinesMutator = (lines) => {
|
|||
* @returns {string}
|
||||
*/
|
||||
const removeLines = (L) => {
|
||||
let removed = '';
|
||||
if (L) {
|
||||
if (!inSplice) {
|
||||
enterSplice();
|
||||
}
|
||||
if (!L) return '';
|
||||
if (!inSplice) enterSplice();
|
||||
|
||||
/**
|
||||
* Gets a string of joined lines after the end of the splice.
|
||||
|
@ -837,6 +826,8 @@ const textLinesMutator = (lines) => {
|
|||
const m = curSplice[0] + curSplice[1];
|
||||
return linesSlice(m, m + k).join('');
|
||||
};
|
||||
|
||||
let removed = '';
|
||||
if (isCurLineInSplice()) {
|
||||
if (curCol === 0) {
|
||||
removed = curSplice[curSplice.length - 1];
|
||||
|
@ -856,7 +847,6 @@ const textLinesMutator = (lines) => {
|
|||
removed = nextKLinesText(L);
|
||||
curSplice[1] += L;
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
};
|
||||
|
||||
|
@ -868,22 +858,15 @@ const textLinesMutator = (lines) => {
|
|||
* @returns {string}
|
||||
*/
|
||||
const remove = (N, L) => {
|
||||
let removed = '';
|
||||
if (N) {
|
||||
if (L) {
|
||||
return removeLines(L);
|
||||
} else {
|
||||
if (!inSplice) {
|
||||
enterSplice();
|
||||
}
|
||||
if (!N) return '';
|
||||
if (L) return removeLines(L);
|
||||
if (!inSplice) enterSplice();
|
||||
// although the line is put into splice, curLine is not increased, because
|
||||
// only some chars are removed not the whole line
|
||||
const sline = putCurLineInSplice();
|
||||
removed = curSplice[sline].substring(curCol, curCol + N);
|
||||
const removed = curSplice[sline].substring(curCol, curCol + N);
|
||||
curSplice[sline] = curSplice[sline].substring(0, curCol) +
|
||||
curSplice[sline].substring(curCol + N);
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
};
|
||||
|
||||
|
@ -894,10 +877,8 @@ const textLinesMutator = (lines) => {
|
|||
* @param {number} L - number of newlines in text
|
||||
*/
|
||||
const insert = (text, L) => {
|
||||
if (text) {
|
||||
if (!inSplice) {
|
||||
enterSplice();
|
||||
}
|
||||
if (!text) return;
|
||||
if (!inSplice) enterSplice();
|
||||
if (L) {
|
||||
const newLines = exports.splitTextLines(text);
|
||||
if (isCurLineInSplice()) {
|
||||
|
@ -932,7 +913,6 @@ const textLinesMutator = (lines) => {
|
|||
curSplice[sline].substring(curCol);
|
||||
curCol += text.length;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -1171,7 +1151,7 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
|
|||
let found = false;
|
||||
for (let i = 0; i < atts.length; i++) {
|
||||
const oldPair = atts[i];
|
||||
if (oldPair[0] === pair[0]) {
|
||||
if (oldPair[0] !== pair[0]) continue;
|
||||
if (pair[1] || resultIsMutation) {
|
||||
oldPair[1] = pair[1];
|
||||
} else {
|
||||
|
@ -1180,7 +1160,6 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
|
|||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((!found) && (pair[1] || resultIsMutation)) {
|
||||
atts.push(pair);
|
||||
}
|
||||
|
@ -1340,12 +1319,11 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
|
|||
lineAssem = exports.mergingOpAssembler();
|
||||
}
|
||||
lineAssem.append(op);
|
||||
if (op.lines > 0) {
|
||||
if (op.lines <= 0) return;
|
||||
assert(op.lines === 1, `Can't have op.lines of ${op.lines} in attribution lines`);
|
||||
// ship it to the mut
|
||||
mut.insert(lineAssem.toString(), 1);
|
||||
lineAssem = null;
|
||||
}
|
||||
};
|
||||
|
||||
const csOp = exports.newOp();
|
||||
|
@ -1505,16 +1483,11 @@ exports.compose = (cs1, cs2, pool) => {
|
|||
*/
|
||||
exports.attributeTester = (attribPair, pool) => {
|
||||
const never = (attribs) => false;
|
||||
if (!pool) {
|
||||
return never;
|
||||
}
|
||||
if (!pool) return never;
|
||||
const attribNum = pool.putAttrib(attribPair, true);
|
||||
if (attribNum < 0) {
|
||||
return never;
|
||||
} else {
|
||||
if (attribNum < 0) return never;
|
||||
const re = new RegExp(`\\*${exports.numToString(attribNum)}(?!\\w)`);
|
||||
return (attribs) => re.test(attribs);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -1796,14 +1769,11 @@ exports.applyToAText = (cs, atext, pool) => ({
|
|||
* @returns {AText}
|
||||
*/
|
||||
exports.cloneAText = (atext) => {
|
||||
if (atext) {
|
||||
if (!atext) error('atext is null');
|
||||
return {
|
||||
text: atext.text,
|
||||
attribs: atext.attribs,
|
||||
};
|
||||
} else {
|
||||
error('atext is null');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -1902,14 +1872,13 @@ exports.opAttributeValue = (op, key, pool) => exports.attribsAttributeValue(op.a
|
|||
* @returns {string}
|
||||
*/
|
||||
exports.attribsAttributeValue = (attribs, key, pool) => {
|
||||
if (!attribs) return '';
|
||||
let value = '';
|
||||
if (attribs) {
|
||||
exports.eachAttribNumber(attribs, (n) => {
|
||||
if (pool.getAttribKey(n) === key) {
|
||||
value = pool.getAttribValue(n);
|
||||
}
|
||||
});
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
|
@ -2038,7 +2007,7 @@ exports.subattribution = (astr, start, optEnd) => {
|
|||
const opOut = exports.newOp();
|
||||
|
||||
const doCsOp = () => {
|
||||
if (csOp.chars) {
|
||||
if (!csOp.chars) return;
|
||||
while (csOp.opcode && (attOp.opcode || iter.hasNext())) {
|
||||
if (!attOp.opcode) iter.next(attOp);
|
||||
|
||||
|
@ -2053,7 +2022,6 @@ exports.subattribution = (astr, start, optEnd) => {
|
|||
opOut.opcode = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
csOp.opcode = '-';
|
||||
|
@ -2399,14 +2367,13 @@ const followAttributes = (att1, att2, pool) => {
|
|||
const pair1 = pool.getAttrib(exports.parseNum(a));
|
||||
for (let i = 0; i < atts.length; i++) {
|
||||
const pair2 = atts[i];
|
||||
if (pair1[0] === pair2[0]) {
|
||||
if (pair1[0] !== pair2[0]) continue;
|
||||
if (pair1[1] <= pair2[1]) {
|
||||
// winner of merge is pair1, delete this attribute
|
||||
atts.splice(i, 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
});
|
||||
// we've only removed attributes, so they're already sorted
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue