lint: src/node/utils/padDiff.js

This commit is contained in:
John McLear 2021-01-21 21:06:52 +00:00 committed by Richard Hansen
parent 9759e09387
commit 4f7e322d53

View file

@ -1,3 +1,4 @@
'use strict';
const Changeset = require('../../static/js/Changeset'); const Changeset = require('../../static/js/Changeset');
const exportHtml = require('./ExportHtml'); const exportHtml = require('./ExportHtml');
@ -125,7 +126,7 @@ PadDiff.prototype._addAuthors = function (authors) {
// add to array if not in the array // add to array if not in the array
authors.forEach((author) => { authors.forEach((author) => {
if (self._authors.indexOf(author) == -1) { if (self._authors.indexOf(author) === -1) {
self._authors.push(author); self._authors.push(author);
} }
}); });
@ -138,7 +139,6 @@ PadDiff.prototype._createDiffAtext = async function () {
let atext = await this._createClearStartAtext(this._fromRev); let atext = await this._createClearStartAtext(this._fromRev);
let superChangeset = null; let superChangeset = null;
const rev = this._fromRev + 1;
for (let rev = this._fromRev + 1; rev <= this._toRev; rev += bulkSize) { for (let rev = this._fromRev + 1; rev <= this._toRev; rev += bulkSize) {
// get the bulk // get the bulk
@ -161,7 +161,7 @@ PadDiff.prototype._createDiffAtext = async function () {
addedAuthors.push(authors[i]); addedAuthors.push(authors[i]);
// compose it with the superChangset // compose it with the superChangset
if (superChangeset === null) { if (superChangeset == null) {
superChangeset = changeset; superChangeset = changeset;
} else { } else {
superChangeset = Changeset.composeWithDeletions(superChangeset, changeset, this._pad.pool); superChangeset = Changeset.composeWithDeletions(superChangeset, changeset, this._pad.pool);
@ -172,7 +172,8 @@ PadDiff.prototype._createDiffAtext = async function () {
this._addAuthors(addedAuthors); this._addAuthors(addedAuthors);
} }
// if there are only clearAuthorship changesets, we don't get a superChangeset, so we can skip this step // if there are only clearAuthorship changesets, we don't get a superChangeset,
// so we can skip this step
if (superChangeset) { if (superChangeset) {
const deletionChangeset = this._createDeletionChangeset(superChangeset, atext, this._pad.pool); const deletionChangeset = this._createDeletionChangeset(superChangeset, atext, this._pad.pool);
@ -205,7 +206,8 @@ PadDiff.prototype.getHtml = async function () {
}; };
PadDiff.prototype.getAuthors = async function () { PadDiff.prototype.getAuthors = async function () {
// check if html was already produced, if not produce it, this generates the author array at the same time // check if html was already produced, if not produce it, this generates
// the author array at the same time
if (this._html == null) { if (this._html == null) {
await this.getHtml(); await this.getHtml();
} }
@ -213,7 +215,7 @@ PadDiff.prototype.getAuthors = async function () {
return self._authors; return self._authors;
}; };
PadDiff.prototype._extendChangesetWithAuthor = function (changeset, author, apool) { PadDiff.prototype._extendChangesetWithAuthor = (changeset, author, apool) => {
// unpack // unpack
const unpacked = Changeset.unpack(changeset); const unpacked = Changeset.unpack(changeset);
@ -245,7 +247,8 @@ PadDiff.prototype._extendChangesetWithAuthor = function (changeset, author, apoo
return Changeset.pack(unpacked.oldLen, unpacked.newLen, assem.toString(), unpacked.charBank); return Changeset.pack(unpacked.oldLen, unpacked.newLen, assem.toString(), unpacked.charBank);
}; };
// this method is 80% like Changeset.inverse. I just changed so instead of reverting, it adds deletions and attribute changes to to the atext. // this method is 80% like Changeset.inverse. I just changed so instead of reverting,
// it adds deletions and attribute changes to to the atext.
PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) { PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
const lines = Changeset.splitTextLines(startAText.text); const lines = Changeset.splitTextLines(startAText.text);
const alines = Changeset.splitAttributionLines(startAText.attribs, startAText.text); const alines = Changeset.splitAttributionLines(startAText.attribs, startAText.text);
@ -254,21 +257,21 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
// They may be arrays or objects with .get(i) and .length methods. // They may be arrays or objects with .get(i) and .length methods.
// They include final newlines on lines. // They include final newlines on lines.
function lines_get(idx) { const linesGet = (idx) => {
if (lines.get) { if (lines.get) {
return lines.get(idx); return lines.get(idx);
} else { } else {
return lines[idx]; return lines[idx];
} }
} };
function alines_get(idx) { const aLinesGet = (idx) => {
if (alines.get) { if (alines.get) {
return alines.get(idx); return alines.get(idx);
} else { } else {
return alines[idx]; return alines[idx];
} }
} };
let curLine = 0; let curLine = 0;
let curChar = 0; let curChar = 0;
@ -280,10 +283,10 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
const csIter = Changeset.opIterator(unpacked.ops); const csIter = Changeset.opIterator(unpacked.ops);
const builder = Changeset.builder(unpacked.newLen); const builder = Changeset.builder(unpacked.newLen);
function consumeAttribRuns(numChars, func /* (len, attribs, endsLine)*/) { const consumeAttribRuns = (numChars, func /* (len, attribs, endsLine)*/) => {
if ((!curLineOpIter) || (curLineOpIterLine != curLine)) { if ((!curLineOpIter) || (curLineOpIterLine !== curLine)) {
// create curLineOpIter and advance it to curChar // create curLineOpIter and advance it to curChar
curLineOpIter = Changeset.opIterator(alines_get(curLine)); curLineOpIter = Changeset.opIterator(aLinesGet(curLine));
curLineOpIterLine = curLine; curLineOpIterLine = curLine;
let indexIntoLine = 0; let indexIntoLine = 0;
let done = false; let done = false;
@ -304,7 +307,7 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
curChar = 0; curChar = 0;
curLineOpIterLine = curLine; curLineOpIterLine = curLine;
curLineNextOp.chars = 0; curLineNextOp.chars = 0;
curLineOpIter = Changeset.opIterator(alines_get(curLine)); curLineOpIter = Changeset.opIterator(aLinesGet(curLine));
} }
if (!curLineNextOp.chars) { if (!curLineNextOp.chars) {
@ -313,7 +316,8 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
const charsToUse = Math.min(numChars, curLineNextOp.chars); const charsToUse = Math.min(numChars, curLineNextOp.chars);
func(charsToUse, curLineNextOp.attribs, charsToUse == curLineNextOp.chars && curLineNextOp.lines > 0); func(charsToUse, curLineNextOp.attribs,
charsToUse === curLineNextOp.chars && curLineNextOp.lines > 0);
numChars -= charsToUse; numChars -= charsToUse;
curLineNextOp.chars -= charsToUse; curLineNextOp.chars -= charsToUse;
curChar += charsToUse; curChar += charsToUse;
@ -323,64 +327,66 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
curLine++; curLine++;
curChar = 0; curChar = 0;
} }
} };
function skip(N, L) { const skip = (N, L) => {
if (L) { if (L) {
curLine += L; curLine += L;
curChar = 0; curChar = 0;
} else if (curLineOpIter && curLineOpIterLine == curLine) { } else if (curLineOpIter && curLineOpIterLine === curLine) {
consumeAttribRuns(N, () => {}); consumeAttribRuns(N, () => {});
} else { } else {
curChar += N; curChar += N;
} }
} };
function nextText(numChars) { const nextText = (numChars) => {
let len = 0; let len = 0;
const assem = Changeset.stringAssembler(); const assem = Changeset.stringAssembler();
const firstString = lines_get(curLine).substring(curChar); const firstString = linesGet(curLine).substring(curChar);
len += firstString.length; len += firstString.length;
assem.append(firstString); assem.append(firstString);
let lineNum = curLine + 1; let lineNum = curLine + 1;
while (len < numChars) { while (len < numChars) {
const nextString = lines_get(lineNum); const nextString = linesGet(lineNum);
len += nextString.length; len += nextString.length;
assem.append(nextString); assem.append(nextString);
lineNum++; lineNum++;
} }
return assem.toString().substring(0, numChars); return assem.toString().substring(0, numChars);
} };
function cachedStrFunc(func) { const cachedStrFunc = (func) => {
const cache = {}; const cache = {};
return function (s) { return (s) => {
if (!cache[s]) { if (!cache[s]) {
cache[s] = func(s); cache[s] = func(s);
} }
return cache[s]; return cache[s];
}; };
} };
const attribKeys = []; const attribKeys = [];
const attribValues = []; const attribValues = [];
// iterate over all operators of this changeset // iterate over all operators of this changeset
while (csIter.hasNext()) { while (csIter.hasNext()) {
var csOp = csIter.next(); const csOp = csIter.next();
if (csOp.opcode == '=') { if (csOp.opcode === '=') {
var textBank = nextText(csOp.chars); const textBank = nextText(csOp.chars);
// decide if this equal operator is an attribution change or not. We can see this by checkinf if attribs is set. // decide if this equal operator is an attribution change or not.
// If the text this operator applies to is only a star, than this is a false positive and should be ignored // We can see this by checkinf if attribs is set.
if (csOp.attribs && textBank != '*') { // If the text this operator applies to is only a star,
// than this is a false positive and should be ignored
if (csOp.attribs && textBank !== '*') {
const deletedAttrib = apool.putAttrib(['removed', true]); const deletedAttrib = apool.putAttrib(['removed', true]);
var authorAttrib = apool.putAttrib(['author', '']); let authorAttrib = apool.putAttrib(['author', '']);
attribKeys.length = 0; attribKeys.length = 0;
attribValues.length = 0; attribValues.length = 0;
@ -393,14 +399,14 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
} }
}); });
var undoBackToAttribs = cachedStrFunc((attribs) => { const undoBackToAttribs = cachedStrFunc((attribs) => {
const backAttribs = []; const backAttribs = [];
for (let i = 0; i < attribKeys.length; i++) { for (let i = 0; i < attribKeys.length; i++) {
const appliedKey = attribKeys[i]; const appliedKey = attribKeys[i];
const appliedValue = attribValues[i]; const appliedValue = attribValues[i];
const oldValue = Changeset.attribsAttributeValue(attribs, appliedKey, apool); const oldValue = Changeset.attribsAttributeValue(attribs, appliedKey, apool);
if (appliedValue != oldValue) { if (appliedValue !== oldValue) {
backAttribs.push([appliedKey, oldValue]); backAttribs.push([appliedKey, oldValue]);
} }
} }
@ -408,7 +414,8 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
return Changeset.makeAttribsString('=', backAttribs, apool); return Changeset.makeAttribsString('=', backAttribs, apool);
}); });
var oldAttribsAddition = `*${Changeset.numToString(deletedAttrib)}*${Changeset.numToString(authorAttrib)}`; const oldAttribsAddition =
`*${Changeset.numToString(deletedAttrib)}*${Changeset.numToString(authorAttrib)}`;
let textLeftToProcess = textBank; let textLeftToProcess = textBank;
@ -427,7 +434,7 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
} }
// get the text we want to procceed in this step // get the text we want to procceed in this step
var processText = textLeftToProcess.substr(0, lengthToProcess); const processText = textLeftToProcess.substr(0, lengthToProcess);
textLeftToProcess = textLeftToProcess.substr(lengthToProcess); textLeftToProcess = textLeftToProcess.substr(lengthToProcess);
@ -437,13 +444,14 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
// consume the attributes of this linebreak // consume the attributes of this linebreak
consumeAttribRuns(1, () => {}); consumeAttribRuns(1, () => {});
} else { } else {
// add the old text via an insert, but add a deletion attribute + the author attribute of the author who deleted it // add the old text via an insert, but add a deletion attribute +
var textBankIndex = 0; // the author attribute of the author who deleted it
let textBankIndex = 0;
consumeAttribRuns(lengthToProcess, (len, attribs, endsLine) => { consumeAttribRuns(lengthToProcess, (len, attribs, endsLine) => {
// get the old attributes back // get the old attributes back
var attribs = (undoBackToAttribs(attribs) || '') + oldAttribsAddition; const oldAttribs = (undoBackToAttribs(attribs) || '') + oldAttribsAddition;
builder.insert(processText.substr(textBankIndex, len), attribs); builder.insert(processText.substr(textBankIndex, len), oldAttribs);
textBankIndex += len; textBankIndex += len;
}); });
@ -454,11 +462,11 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
skip(csOp.chars, csOp.lines); skip(csOp.chars, csOp.lines);
builder.keep(csOp.chars, csOp.lines); builder.keep(csOp.chars, csOp.lines);
} }
} else if (csOp.opcode == '+') { } else if (csOp.opcode === '+') {
builder.keep(csOp.chars, csOp.lines); builder.keep(csOp.chars, csOp.lines);
} else if (csOp.opcode == '-') { } else if (csOp.opcode === '-') {
var textBank = nextText(csOp.chars); const textBank = nextText(csOp.chars);
var textBankIndex = 0; let textBankIndex = 0;
consumeAttribRuns(csOp.chars, (len, attribs, endsLine) => { consumeAttribRuns(csOp.chars, (len, attribs, endsLine) => {
builder.insert(textBank.substr(textBankIndex, len), attribs + csOp.attribs); builder.insert(textBank.substr(textBankIndex, len), attribs + csOp.attribs);