Use more meaningful variable name

This commit is contained in:
YUKI "Piro" Hiroshi 2019-04-18 16:00:45 +09:00
parent 93e4cd727e
commit 955c81cacb

View file

@ -34,7 +34,7 @@ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
window.Diff = { window.Diff = {
diff(oldString, newString, options = {}) { diff(oldValues, newValues, options = {}) {
let callback = options.callback; let callback = options.callback;
if (typeof options === 'function') { if (typeof options === 'function') {
callback = options; callback = options;
@ -54,22 +54,22 @@ window.Diff = {
} }
// Allow subclasses to massage the input prior to running // Allow subclasses to massage the input prior to running
oldString = this.castInput(oldString); oldValues = this.castInput(oldValues);
newString = this.castInput(newString); newValues = this.castInput(newValues);
oldString = this.removeEmpty(this.tokenize(oldString)); oldValues = this.removeEmpty(this.tokenize(oldValues));
newString = this.removeEmpty(this.tokenize(newString)); newValues = this.removeEmpty(this.tokenize(newValues));
let newLen = newString.length, oldLen = oldString.length; let newLen = newValues.length, oldLen = oldValues.length;
let editLength = 1; let editLength = 1;
let maxEditLength = newLen + oldLen; let maxEditLength = newLen + oldLen;
let bestPath = [{ newPos: -1, components: [] }]; let bestPath = [{ newPos: -1, components: [] }];
// Seed editLength = 0, i.e. the content starts with the same values // Seed editLength = 0, i.e. the content starts with the same values
let oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); let oldPos = this.extractCommon(bestPath[0], newValues, oldValues, 0);
if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) { if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
// Identity per the equality and tokenizer // Identity per the equality and tokenizer
return done([{value: this.join(newString), count: newString.length}]); return done([{value: this.join(newValues), count: newValues.length}]);
} }
// Main worker method. checks all permutations of a given edit length for acceptance. // Main worker method. checks all permutations of a given edit length for acceptance.
@ -104,11 +104,11 @@ window.Diff = {
self.pushComponent(basePath.components, true, undefined); self.pushComponent(basePath.components, true, undefined);
} }
oldPos = self.extractCommon(basePath, newString, oldString, diagonalPath); oldPos = self.extractCommon(basePath, newValues, oldValues, diagonalPath);
// If we have hit the end of both strings, then we are done // If we have hit the end of both strings, then we are done
if (basePath.newPos + 1 >= newLen && oldPos + 1 >= oldLen) { if (basePath.newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
return done(this._buildValues(self, basePath.components, newString, oldString, self.useLongestToken)); return done(this._buildValues(self, basePath.components, newValues, oldValues, self.useLongestToken));
} else { } else {
// Otherwise track this path as a potential candidate and continue. // Otherwise track this path as a potential candidate and continue.
bestPath[diagonalPath] = basePath; bestPath[diagonalPath] = basePath;
@ -155,14 +155,14 @@ window.Diff = {
components.push({count: 1, added: added, removed: removed }); components.push({count: 1, added: added, removed: removed });
} }
}, },
extractCommon(basePath, newString, oldString, diagonalPath) { extractCommon(basePath, newValues, oldValues, diagonalPath) {
let newLen = newString.length, let newLen = newValues.length,
oldLen = oldString.length, oldLen = oldValues.length,
newPos = basePath.newPos, newPos = basePath.newPos,
oldPos = newPos - diagonalPath, oldPos = newPos - diagonalPath,
commonCount = 0; commonCount = 0;
while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(newString[newPos + 1], oldString[oldPos + 1])) { while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(newValues[newPos + 1], oldValues[oldPos + 1])) {
newPos++; newPos++;
oldPos++; oldPos++;
commonCount++; commonCount++;
@ -197,7 +197,7 @@ window.Diff = {
return value; return value;
}, },
_buildValues(diff, components, newString, oldString, useLongestToken) { _buildValues(diff, components, newValues, oldValues, useLongestToken) {
let componentPos = 0, let componentPos = 0,
componentLen = components.length, componentLen = components.length,
newPos = 0, newPos = 0,
@ -207,15 +207,15 @@ window.Diff = {
let component = components[componentPos]; let component = components[componentPos];
if (!component.removed) { if (!component.removed) {
if (!component.added && useLongestToken) { if (!component.added && useLongestToken) {
let value = newString.slice(newPos, newPos + component.count); let value = newValues.slice(newPos, newPos + component.count);
value = value.map(function(value, i) { value = value.map(function(value, i) {
let oldValue = oldString[oldPos + i]; let oldValue = oldValues[oldPos + i];
return oldValue.length > value.length ? oldValue : value; return oldValue.length > value.length ? oldValue : value;
}); });
component.value = diff.join(value); component.value = diff.join(value);
} else { } else {
component.value = diff.join(newString.slice(newPos, newPos + component.count)); component.value = diff.join(newValues.slice(newPos, newPos + component.count));
} }
newPos += component.count; newPos += component.count;
@ -224,7 +224,7 @@ window.Diff = {
oldPos += component.count; oldPos += component.count;
} }
} else { } else {
component.value = diff.join(oldString.slice(oldPos, oldPos + component.count)); component.value = diff.join(oldValues.slice(oldPos, oldPos + component.count));
oldPos += component.count; oldPos += component.count;
// Reverse add and remove so removes are output first to match common convention // Reverse add and remove so removes are output first to match common convention