Changeset: Invert conditions to improve readability

This commit is contained in:
Richard Hansen 2021-09-27 17:25:31 -04:00
parent b29e59419e
commit 7fa9b07116

View file

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