From 9e7b142bb7c631e599987651e5bbeda607b35444 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 18 Nov 2021 18:59:17 -0500 Subject: [PATCH] Invert conditions to improve readability --- src/static/js/AttributeManager.js | 59 ++++++++++++------------------- src/static/js/broadcast.js | 14 +++----- src/static/js/linestylefilter.js | 38 ++++++++++---------- 3 files changed, 46 insertions(+), 65 deletions(-) diff --git a/src/static/js/AttributeManager.js b/src/static/js/AttributeManager.js index 124434031..d5b650bd5 100644 --- a/src/static/js/AttributeManager.js +++ b/src/static/js/AttributeManager.js @@ -147,13 +147,10 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({ getAttributeOnLine(lineNum, attributeName) { // get `attributeName` attribute of first char of line const aline = this.rep.alines[lineNum]; - if (aline) { - const opIter = Changeset.opIterator(aline); - if (opIter.hasNext()) { - return Changeset.opAttributeValue(opIter.next(), attributeName, this.rep.apool) || ''; - } - } - return ''; + if (!aline) return ''; + const opIter = Changeset.opIterator(aline); + if (!opIter.hasNext()) return ''; + return Changeset.opAttributeValue(opIter.next(), attributeName, this.rep.apool) || ''; }, /* @@ -163,21 +160,16 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({ getAttributesOnLine(lineNum) { // get attributes of first char of line const aline = this.rep.alines[lineNum]; + if (!aline) return []; + const opIter = Changeset.opIterator(aline); + if (!opIter.hasNext()) return []; + const op = opIter.next(); + if (!op.attribs) return []; const attributes = []; - if (aline) { - const opIter = Changeset.opIterator(aline); - let op; - if (opIter.hasNext()) { - op = opIter.next(); - if (!op.attribs) return []; - - Changeset.eachAttribNumber(op.attribs, (n) => { - attributes.push([this.rep.apool.getAttribKey(n), this.rep.apool.getAttribValue(n)]); - }); - return attributes; - } - } - return []; + Changeset.eachAttribNumber(op.attribs, (n) => { + attributes.push([this.rep.apool.getAttribKey(n), this.rep.apool.getAttribValue(n)]); + }); + return attributes; }, /* @@ -278,27 +270,22 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({ // we need to sum up how much characters each operations take until the wanted position let currentPointer = 0; - const attributes = []; let currentOperation; while (opIter.hasNext()) { currentOperation = opIter.next(); currentPointer += currentOperation.chars; - - if (currentPointer > column) { - // we got the operation of the wanted position, now collect all its attributes - Changeset.eachAttribNumber(currentOperation.attribs, (n) => { - attributes.push([ - this.rep.apool.getAttribKey(n), - this.rep.apool.getAttribValue(n), - ]); - }); - - // skip the loop - return attributes; - } + if (currentPointer <= column) continue; + const attributes = []; + Changeset.eachAttribNumber(currentOperation.attribs, (n) => { + attributes.push([ + this.rep.apool.getAttribKey(n), + this.rep.apool.getAttribValue(n), + ]); + }); + return attributes; } - return attributes; + return []; }, /* diff --git a/src/static/js/broadcast.js b/src/static/js/broadcast.js index 6ba2ef0ab..c778f6909 100644 --- a/src/static/js/broadcast.js +++ b/src/static/js/broadcast.js @@ -119,15 +119,11 @@ const loadBroadcastJS = (socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro const alines = this.alines; for (let i = 0; i < alines.length; i++) { Changeset.eachAttribNumber(alines[i], (n) => { - if (!seenNums[n]) { - seenNums[n] = true; - if (this.apool.getAttribKey(n) === 'author') { - const a = this.apool.getAttribValue(n); - if (a) { - authors.push(a); - } - } - } + if (seenNums[n]) return; + seenNums[n] = true; + if (this.apool.getAttribKey(n) !== 'author') return; + const a = this.apool.getAttribValue(n); + if (a) authors.push(a); }); } authors.sort(); diff --git a/src/static/js/linestylefilter.js b/src/static/js/linestylefilter.js index 84668ea46..c0a81e9da 100644 --- a/src/static/js/linestylefilter.js +++ b/src/static/js/linestylefilter.js @@ -77,26 +77,24 @@ linestylefilter.getLineStyleFilter = (lineLength, aline, textAndClassFunc, apool Changeset.eachAttribNumber(attribs, (n) => { // Give us this attributes key const key = apool.getAttribKey(n); - if (key) { - const value = apool.getAttribValue(n); - if (value) { - if (!isLineAttribMarker && AttributeManager.lineAttributes.indexOf(key) >= 0) { - isLineAttribMarker = true; - } - if (key === 'author') { - classes += ` ${linestylefilter.getAuthorClassName(value)}`; - } else if (key === 'list') { - classes += ` list:${value}`; - } else if (key === 'start') { - // Needed to introduce the correct Ordered list item start number on import - classes += ` start:${value}`; - } else if (linestylefilter.ATTRIB_CLASSES[key]) { - classes += ` ${linestylefilter.ATTRIB_CLASSES[key]}`; - } else { - const results = hooks.callAll('aceAttribsToClasses', {linestylefilter, key, value}); - classes += ` ${results.join(' ')}`; - } - } + if (!key) return; + const value = apool.getAttribValue(n); + if (!value) return; + if (!isLineAttribMarker && AttributeManager.lineAttributes.indexOf(key) >= 0) { + isLineAttribMarker = true; + } + if (key === 'author') { + classes += ` ${linestylefilter.getAuthorClassName(value)}`; + } else if (key === 'list') { + classes += ` list:${value}`; + } else if (key === 'start') { + // Needed to introduce the correct Ordered list item start number on import + classes += ` start:${value}`; + } else if (linestylefilter.ATTRIB_CLASSES[key]) { + classes += ` ${linestylefilter.ATTRIB_CLASSES[key]}`; + } else { + const results = hooks.callAll('aceAttribsToClasses', {linestylefilter, key, value}); + classes += ` ${results.join(' ')}`; } });