mirror of
https://github.com/joelwmale/webhook-action.git
synced 2026-08-27 06:47:18 -05:00
feat: updates & github event support
This commit is contained in:
-2
@@ -1,2 +0,0 @@
|
||||
language: node_js
|
||||
node_js: '0.10'
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
# Fast Diff [](https://travis-ci.org/jhchen/fast-diff)
|
||||
# Fast Diff 
|
||||
|
||||
This is a simplified import of the excellent [diff-match-patch](https://code.google.com/p/google-diff-match-patch/) library by [Neil Fraser](https://neil.fraser.name/) into the Node.js environment. The match and patch parts are removed, as well as all the extra diff options. What remains is incredibly fast diffing between two strings.
|
||||
|
||||
|
||||
+12
-11
@@ -1,20 +1,21 @@
|
||||
declare function diff(
|
||||
text1: string,
|
||||
text2: string,
|
||||
cursorPos?: number | diff.CursorInfo
|
||||
text1: string,
|
||||
text2: string,
|
||||
cursorPos?: number | diff.CursorInfo,
|
||||
cleanup?: boolean
|
||||
): diff.Diff[];
|
||||
|
||||
declare namespace diff {
|
||||
type Diff = [-1 | 0 | 1, string];
|
||||
type Diff = [-1 | 0 | 1, string];
|
||||
|
||||
const DELETE: -1;
|
||||
const INSERT: 1;
|
||||
const EQUAL: 0;
|
||||
const DELETE: -1;
|
||||
const INSERT: 1;
|
||||
const EQUAL: 0;
|
||||
|
||||
interface CursorInfo {
|
||||
oldRange: { index: number; length: number };
|
||||
newRange: { index: number; length: number };
|
||||
}
|
||||
interface CursorInfo {
|
||||
oldRange: { index: number; length: number };
|
||||
newRange: { index: number; length: number };
|
||||
}
|
||||
}
|
||||
|
||||
export = diff;
|
||||
|
||||
+431
-67
@@ -23,7 +23,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The data structure representing a diff is an array of tuples:
|
||||
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
|
||||
@@ -33,16 +32,16 @@ var DIFF_DELETE = -1;
|
||||
var DIFF_INSERT = 1;
|
||||
var DIFF_EQUAL = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Find the differences between two texts. Simplifies the problem by stripping
|
||||
* any common prefix or suffix off the texts before diffing.
|
||||
* @param {string} text1 Old string to be diffed.
|
||||
* @param {string} text2 New string to be diffed.
|
||||
* @param {Int|Object} [cursor_pos] Edit position in text1 or object with more info
|
||||
* @param {boolean} [cleanup] Apply semantic cleanup before returning.
|
||||
* @return {Array} Array of diff tuples.
|
||||
*/
|
||||
function diff_main(text1, text2, cursor_pos, _fix_unicode) {
|
||||
function diff_main(text1, text2, cursor_pos, cleanup, _fix_unicode) {
|
||||
// Check for equality
|
||||
if (text1 === text2) {
|
||||
if (text1) {
|
||||
@@ -81,9 +80,11 @@ function diff_main(text1, text2, cursor_pos, _fix_unicode) {
|
||||
diffs.push([DIFF_EQUAL, commonsuffix]);
|
||||
}
|
||||
diff_cleanupMerge(diffs, _fix_unicode);
|
||||
if (cleanup) {
|
||||
diff_cleanupSemantic(diffs);
|
||||
}
|
||||
return diffs;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the differences between two texts. Assumes that the texts do not
|
||||
@@ -113,7 +114,7 @@ function diff_compute_(text1, text2) {
|
||||
diffs = [
|
||||
[DIFF_INSERT, longtext.substring(0, i)],
|
||||
[DIFF_EQUAL, shorttext],
|
||||
[DIFF_INSERT, longtext.substring(i + shorttext.length)]
|
||||
[DIFF_INSERT, longtext.substring(i + shorttext.length)],
|
||||
];
|
||||
// Swap insertions for deletions if diff is reversed.
|
||||
if (text1.length > text2.length) {
|
||||
@@ -125,7 +126,10 @@ function diff_compute_(text1, text2) {
|
||||
if (shorttext.length === 1) {
|
||||
// Single character string.
|
||||
// After the previous speedup, the character can't be an equality.
|
||||
return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
||||
return [
|
||||
[DIFF_DELETE, text1],
|
||||
[DIFF_INSERT, text2],
|
||||
];
|
||||
}
|
||||
|
||||
// Check to see if the problem can be split in two.
|
||||
@@ -145,8 +149,7 @@ function diff_compute_(text1, text2) {
|
||||
}
|
||||
|
||||
return diff_bisect_(text1, text2);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the 'middle snake' of a diff, split the problem in two
|
||||
@@ -177,7 +180,7 @@ function diff_bisect_(text1, text2) {
|
||||
var delta = text1_length - text2_length;
|
||||
// If the total number of characters is odd, then the front path will collide
|
||||
// with the reverse path.
|
||||
var front = (delta % 2 !== 0);
|
||||
var front = delta % 2 !== 0;
|
||||
// Offsets for start and end of k loop.
|
||||
// Prevents mapping of space beyond the grid.
|
||||
var k1start = 0;
|
||||
@@ -196,7 +199,8 @@ function diff_bisect_(text1, text2) {
|
||||
}
|
||||
var y1 = x1 - k1;
|
||||
while (
|
||||
x1 < text1_length && y1 < text2_length &&
|
||||
x1 < text1_length &&
|
||||
y1 < text2_length &&
|
||||
text1.charAt(x1) === text2.charAt(y1)
|
||||
) {
|
||||
x1++;
|
||||
@@ -233,8 +237,10 @@ function diff_bisect_(text1, text2) {
|
||||
}
|
||||
var y2 = x2 - k2;
|
||||
while (
|
||||
x2 < text1_length && y2 < text2_length &&
|
||||
text1.charAt(text1_length - x2 - 1) === text2.charAt(text2_length - y2 - 1)
|
||||
x2 < text1_length &&
|
||||
y2 < text2_length &&
|
||||
text1.charAt(text1_length - x2 - 1) ===
|
||||
text2.charAt(text2_length - y2 - 1)
|
||||
) {
|
||||
x2++;
|
||||
y2++;
|
||||
@@ -263,9 +269,11 @@ function diff_bisect_(text1, text2) {
|
||||
}
|
||||
// Diff took too long and hit the deadline or
|
||||
// number of diffs equals number of characters, no commonality at all.
|
||||
return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
||||
};
|
||||
|
||||
return [
|
||||
[DIFF_DELETE, text1],
|
||||
[DIFF_INSERT, text2],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the location of the 'middle snake', split the diff in two parts
|
||||
@@ -287,8 +295,7 @@ function diff_bisectSplit_(text1, text2, x, y) {
|
||||
var diffsb = diff_main(text1b, text2b);
|
||||
|
||||
return diffs.concat(diffsb);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the common prefix of two strings.
|
||||
@@ -326,8 +333,57 @@ function diff_commonPrefix(text1, text2) {
|
||||
}
|
||||
|
||||
return pointermid;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the suffix of one string is the prefix of another.
|
||||
* @param {string} text1 First string.
|
||||
* @param {string} text2 Second string.
|
||||
* @return {number} The number of characters common to the end of the first
|
||||
* string and the start of the second string.
|
||||
* @private
|
||||
*/
|
||||
function diff_commonOverlap_(text1, text2) {
|
||||
// Cache the text lengths to prevent multiple calls.
|
||||
var text1_length = text1.length;
|
||||
var text2_length = text2.length;
|
||||
// Eliminate the null case.
|
||||
if (text1_length == 0 || text2_length == 0) {
|
||||
return 0;
|
||||
}
|
||||
// Truncate the longer string.
|
||||
if (text1_length > text2_length) {
|
||||
text1 = text1.substring(text1_length - text2_length);
|
||||
} else if (text1_length < text2_length) {
|
||||
text2 = text2.substring(0, text1_length);
|
||||
}
|
||||
var text_length = Math.min(text1_length, text2_length);
|
||||
// Quick check for the worst case.
|
||||
if (text1 == text2) {
|
||||
return text_length;
|
||||
}
|
||||
|
||||
// Start by looking for a single character match
|
||||
// and increase length until no match is found.
|
||||
// Performance analysis: http://neil.fraser.name/news/2010/11/04/
|
||||
var best = 0;
|
||||
var length = 1;
|
||||
while (true) {
|
||||
var pattern = text1.substring(text_length - length);
|
||||
var found = text2.indexOf(pattern);
|
||||
if (found == -1) {
|
||||
return best;
|
||||
}
|
||||
length += found;
|
||||
if (
|
||||
found == 0 ||
|
||||
text1.substring(text_length - length) == text2.substring(0, length)
|
||||
) {
|
||||
best = length;
|
||||
length++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the common suffix of two strings.
|
||||
@@ -364,8 +420,7 @@ function diff_commonSuffix(text1, text2) {
|
||||
}
|
||||
|
||||
return pointermid;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the two texts share a substring which is at least half the length of the
|
||||
@@ -381,7 +436,7 @@ function diff_halfMatch_(text1, text2) {
|
||||
var longtext = text1.length > text2.length ? text1 : text2;
|
||||
var shorttext = text1.length > text2.length ? text2 : text1;
|
||||
if (longtext.length < 4 || shorttext.length * 2 < longtext.length) {
|
||||
return null; // Pointless.
|
||||
return null; // Pointless.
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -400,16 +455,21 @@ function diff_halfMatch_(text1, text2) {
|
||||
// Start with a 1/4 length substring at position i as a seed.
|
||||
var seed = longtext.substring(i, i + Math.floor(longtext.length / 4));
|
||||
var j = -1;
|
||||
var best_common = '';
|
||||
var best_common = "";
|
||||
var best_longtext_a, best_longtext_b, best_shorttext_a, best_shorttext_b;
|
||||
while ((j = shorttext.indexOf(seed, j + 1)) !== -1) {
|
||||
var prefixLength = diff_commonPrefix(
|
||||
longtext.substring(i), shorttext.substring(j));
|
||||
longtext.substring(i),
|
||||
shorttext.substring(j)
|
||||
);
|
||||
var suffixLength = diff_commonSuffix(
|
||||
longtext.substring(0, i), shorttext.substring(0, j));
|
||||
longtext.substring(0, i),
|
||||
shorttext.substring(0, j)
|
||||
);
|
||||
if (best_common.length < suffixLength + prefixLength) {
|
||||
best_common = shorttext.substring(
|
||||
j - suffixLength, j) + shorttext.substring(j, j + prefixLength);
|
||||
best_common =
|
||||
shorttext.substring(j - suffixLength, j) +
|
||||
shorttext.substring(j, j + prefixLength);
|
||||
best_longtext_a = longtext.substring(0, i - suffixLength);
|
||||
best_longtext_b = longtext.substring(i + prefixLength);
|
||||
best_shorttext_a = shorttext.substring(0, j - suffixLength);
|
||||
@@ -418,8 +478,11 @@ function diff_halfMatch_(text1, text2) {
|
||||
}
|
||||
if (best_common.length * 2 >= longtext.length) {
|
||||
return [
|
||||
best_longtext_a, best_longtext_b,
|
||||
best_shorttext_a, best_shorttext_b, best_common
|
||||
best_longtext_a,
|
||||
best_longtext_b,
|
||||
best_shorttext_a,
|
||||
best_shorttext_b,
|
||||
best_common,
|
||||
];
|
||||
} else {
|
||||
return null;
|
||||
@@ -427,9 +490,17 @@ function diff_halfMatch_(text1, text2) {
|
||||
}
|
||||
|
||||
// First check if the second quarter is the seed for a half-match.
|
||||
var hm1 = diff_halfMatchI_(longtext, shorttext, Math.ceil(longtext.length / 4));
|
||||
var hm1 = diff_halfMatchI_(
|
||||
longtext,
|
||||
shorttext,
|
||||
Math.ceil(longtext.length / 4)
|
||||
);
|
||||
// Check again based on the third quarter.
|
||||
var hm2 = diff_halfMatchI_(longtext, shorttext, Math.ceil(longtext.length / 2));
|
||||
var hm2 = diff_halfMatchI_(
|
||||
longtext,
|
||||
shorttext,
|
||||
Math.ceil(longtext.length / 2)
|
||||
);
|
||||
var hm;
|
||||
if (!hm1 && !hm2) {
|
||||
return null;
|
||||
@@ -457,8 +528,267 @@ function diff_halfMatch_(text1, text2) {
|
||||
}
|
||||
var mid_common = hm[4];
|
||||
return [text1_a, text1_b, text2_a, text2_b, mid_common];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduce the number of edits by eliminating semantically trivial equalities.
|
||||
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
||||
*/
|
||||
function diff_cleanupSemantic(diffs) {
|
||||
var changes = false;
|
||||
var equalities = []; // Stack of indices where equalities are found.
|
||||
var equalitiesLength = 0; // Keeping our own length var is faster in JS.
|
||||
/** @type {?string} */
|
||||
var lastequality = null;
|
||||
// Always equal to diffs[equalities[equalitiesLength - 1]][1]
|
||||
var pointer = 0; // Index of current position.
|
||||
// Number of characters that changed prior to the equality.
|
||||
var length_insertions1 = 0;
|
||||
var length_deletions1 = 0;
|
||||
// Number of characters that changed after the equality.
|
||||
var length_insertions2 = 0;
|
||||
var length_deletions2 = 0;
|
||||
while (pointer < diffs.length) {
|
||||
if (diffs[pointer][0] == DIFF_EQUAL) {
|
||||
// Equality found.
|
||||
equalities[equalitiesLength++] = pointer;
|
||||
length_insertions1 = length_insertions2;
|
||||
length_deletions1 = length_deletions2;
|
||||
length_insertions2 = 0;
|
||||
length_deletions2 = 0;
|
||||
lastequality = diffs[pointer][1];
|
||||
} else {
|
||||
// An insertion or deletion.
|
||||
if (diffs[pointer][0] == DIFF_INSERT) {
|
||||
length_insertions2 += diffs[pointer][1].length;
|
||||
} else {
|
||||
length_deletions2 += diffs[pointer][1].length;
|
||||
}
|
||||
// Eliminate an equality that is smaller or equal to the edits on both
|
||||
// sides of it.
|
||||
if (
|
||||
lastequality &&
|
||||
lastequality.length <=
|
||||
Math.max(length_insertions1, length_deletions1) &&
|
||||
lastequality.length <= Math.max(length_insertions2, length_deletions2)
|
||||
) {
|
||||
// Duplicate record.
|
||||
diffs.splice(equalities[equalitiesLength - 1], 0, [
|
||||
DIFF_DELETE,
|
||||
lastequality,
|
||||
]);
|
||||
// Change second copy to insert.
|
||||
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT;
|
||||
// Throw away the equality we just deleted.
|
||||
equalitiesLength--;
|
||||
// Throw away the previous equality (it needs to be reevaluated).
|
||||
equalitiesLength--;
|
||||
pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1;
|
||||
length_insertions1 = 0; // Reset the counters.
|
||||
length_deletions1 = 0;
|
||||
length_insertions2 = 0;
|
||||
length_deletions2 = 0;
|
||||
lastequality = null;
|
||||
changes = true;
|
||||
}
|
||||
}
|
||||
pointer++;
|
||||
}
|
||||
|
||||
// Normalize the diff.
|
||||
if (changes) {
|
||||
diff_cleanupMerge(diffs);
|
||||
}
|
||||
diff_cleanupSemanticLossless(diffs);
|
||||
|
||||
// Find any overlaps between deletions and insertions.
|
||||
// e.g: <del>abcxxx</del><ins>xxxdef</ins>
|
||||
// -> <del>abc</del>xxx<ins>def</ins>
|
||||
// e.g: <del>xxxabc</del><ins>defxxx</ins>
|
||||
// -> <ins>def</ins>xxx<del>abc</del>
|
||||
// Only extract an overlap if it is as big as the edit ahead or behind it.
|
||||
pointer = 1;
|
||||
while (pointer < diffs.length) {
|
||||
if (
|
||||
diffs[pointer - 1][0] == DIFF_DELETE &&
|
||||
diffs[pointer][0] == DIFF_INSERT
|
||||
) {
|
||||
var deletion = diffs[pointer - 1][1];
|
||||
var insertion = diffs[pointer][1];
|
||||
var overlap_length1 = diff_commonOverlap_(deletion, insertion);
|
||||
var overlap_length2 = diff_commonOverlap_(insertion, deletion);
|
||||
if (overlap_length1 >= overlap_length2) {
|
||||
if (
|
||||
overlap_length1 >= deletion.length / 2 ||
|
||||
overlap_length1 >= insertion.length / 2
|
||||
) {
|
||||
// Overlap found. Insert an equality and trim the surrounding edits.
|
||||
diffs.splice(pointer, 0, [
|
||||
DIFF_EQUAL,
|
||||
insertion.substring(0, overlap_length1),
|
||||
]);
|
||||
diffs[pointer - 1][1] = deletion.substring(
|
||||
0,
|
||||
deletion.length - overlap_length1
|
||||
);
|
||||
diffs[pointer + 1][1] = insertion.substring(overlap_length1);
|
||||
pointer++;
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
overlap_length2 >= deletion.length / 2 ||
|
||||
overlap_length2 >= insertion.length / 2
|
||||
) {
|
||||
// Reverse overlap found.
|
||||
// Insert an equality and swap and trim the surrounding edits.
|
||||
diffs.splice(pointer, 0, [
|
||||
DIFF_EQUAL,
|
||||
deletion.substring(0, overlap_length2),
|
||||
]);
|
||||
diffs[pointer - 1][0] = DIFF_INSERT;
|
||||
diffs[pointer - 1][1] = insertion.substring(
|
||||
0,
|
||||
insertion.length - overlap_length2
|
||||
);
|
||||
diffs[pointer + 1][0] = DIFF_DELETE;
|
||||
diffs[pointer + 1][1] = deletion.substring(overlap_length2);
|
||||
pointer++;
|
||||
}
|
||||
}
|
||||
pointer++;
|
||||
}
|
||||
pointer++;
|
||||
}
|
||||
}
|
||||
|
||||
var nonAlphaNumericRegex_ = /[^a-zA-Z0-9]/;
|
||||
var whitespaceRegex_ = /\s/;
|
||||
var linebreakRegex_ = /[\r\n]/;
|
||||
var blanklineEndRegex_ = /\n\r?\n$/;
|
||||
var blanklineStartRegex_ = /^\r?\n\r?\n/;
|
||||
|
||||
/**
|
||||
* Look for single edits surrounded on both sides by equalities
|
||||
* which can be shifted sideways to align the edit to a word boundary.
|
||||
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
|
||||
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
||||
*/
|
||||
function diff_cleanupSemanticLossless(diffs) {
|
||||
/**
|
||||
* Given two strings, compute a score representing whether the internal
|
||||
* boundary falls on logical boundaries.
|
||||
* Scores range from 6 (best) to 0 (worst).
|
||||
* Closure, but does not reference any external variables.
|
||||
* @param {string} one First string.
|
||||
* @param {string} two Second string.
|
||||
* @return {number} The score.
|
||||
* @private
|
||||
*/
|
||||
function diff_cleanupSemanticScore_(one, two) {
|
||||
if (!one || !two) {
|
||||
// Edges are the best.
|
||||
return 6;
|
||||
}
|
||||
|
||||
// Each port of this function behaves slightly differently due to
|
||||
// subtle differences in each language's definition of things like
|
||||
// 'whitespace'. Since this function's purpose is largely cosmetic,
|
||||
// the choice has been made to use each language's native features
|
||||
// rather than force total conformity.
|
||||
var char1 = one.charAt(one.length - 1);
|
||||
var char2 = two.charAt(0);
|
||||
var nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
|
||||
var nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
|
||||
var whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_);
|
||||
var whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_);
|
||||
var lineBreak1 = whitespace1 && char1.match(linebreakRegex_);
|
||||
var lineBreak2 = whitespace2 && char2.match(linebreakRegex_);
|
||||
var blankLine1 = lineBreak1 && one.match(blanklineEndRegex_);
|
||||
var blankLine2 = lineBreak2 && two.match(blanklineStartRegex_);
|
||||
|
||||
if (blankLine1 || blankLine2) {
|
||||
// Five points for blank lines.
|
||||
return 5;
|
||||
} else if (lineBreak1 || lineBreak2) {
|
||||
// Four points for line breaks.
|
||||
return 4;
|
||||
} else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
|
||||
// Three points for end of sentences.
|
||||
return 3;
|
||||
} else if (whitespace1 || whitespace2) {
|
||||
// Two points for whitespace.
|
||||
return 2;
|
||||
} else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
|
||||
// One point for non-alphanumeric.
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
var pointer = 1;
|
||||
// Intentionally ignore the first and last element (don't need checking).
|
||||
while (pointer < diffs.length - 1) {
|
||||
if (
|
||||
diffs[pointer - 1][0] == DIFF_EQUAL &&
|
||||
diffs[pointer + 1][0] == DIFF_EQUAL
|
||||
) {
|
||||
// This is a single edit surrounded by equalities.
|
||||
var equality1 = diffs[pointer - 1][1];
|
||||
var edit = diffs[pointer][1];
|
||||
var equality2 = diffs[pointer + 1][1];
|
||||
|
||||
// First, shift the edit as far left as possible.
|
||||
var commonOffset = diff_commonSuffix(equality1, edit);
|
||||
if (commonOffset) {
|
||||
var commonString = edit.substring(edit.length - commonOffset);
|
||||
equality1 = equality1.substring(0, equality1.length - commonOffset);
|
||||
edit = commonString + edit.substring(0, edit.length - commonOffset);
|
||||
equality2 = commonString + equality2;
|
||||
}
|
||||
|
||||
// Second, step character by character right, looking for the best fit.
|
||||
var bestEquality1 = equality1;
|
||||
var bestEdit = edit;
|
||||
var bestEquality2 = equality2;
|
||||
var bestScore =
|
||||
diff_cleanupSemanticScore_(equality1, edit) +
|
||||
diff_cleanupSemanticScore_(edit, equality2);
|
||||
while (edit.charAt(0) === equality2.charAt(0)) {
|
||||
equality1 += edit.charAt(0);
|
||||
edit = edit.substring(1) + equality2.charAt(0);
|
||||
equality2 = equality2.substring(1);
|
||||
var score =
|
||||
diff_cleanupSemanticScore_(equality1, edit) +
|
||||
diff_cleanupSemanticScore_(edit, equality2);
|
||||
// The >= encourages trailing rather than leading whitespace on edits.
|
||||
if (score >= bestScore) {
|
||||
bestScore = score;
|
||||
bestEquality1 = equality1;
|
||||
bestEdit = edit;
|
||||
bestEquality2 = equality2;
|
||||
}
|
||||
}
|
||||
|
||||
if (diffs[pointer - 1][1] != bestEquality1) {
|
||||
// We have an improvement, save it back to the diff.
|
||||
if (bestEquality1) {
|
||||
diffs[pointer - 1][1] = bestEquality1;
|
||||
} else {
|
||||
diffs.splice(pointer - 1, 1);
|
||||
pointer--;
|
||||
}
|
||||
diffs[pointer][1] = bestEdit;
|
||||
if (bestEquality2) {
|
||||
diffs[pointer + 1][1] = bestEquality2;
|
||||
} else {
|
||||
diffs.splice(pointer + 1, 1);
|
||||
pointer--;
|
||||
}
|
||||
}
|
||||
}
|
||||
pointer++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorder and merge like edit sections. Merge equalities.
|
||||
@@ -467,12 +797,12 @@ function diff_halfMatch_(text1, text2) {
|
||||
* @param {boolean} fix_unicode Whether to normalize to a unicode-correct diff
|
||||
*/
|
||||
function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
diffs.push([DIFF_EQUAL, '']); // Add a dummy entry at the end.
|
||||
diffs.push([DIFF_EQUAL, ""]); // Add a dummy entry at the end.
|
||||
var pointer = 0;
|
||||
var count_delete = 0;
|
||||
var count_insert = 0;
|
||||
var text_delete = '';
|
||||
var text_insert = '';
|
||||
var text_delete = "";
|
||||
var text_insert = "";
|
||||
var commonlength;
|
||||
while (pointer < diffs.length) {
|
||||
if (pointer < diffs.length - 1 && !diffs[pointer][1]) {
|
||||
@@ -481,7 +811,6 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
}
|
||||
switch (diffs[pointer][0]) {
|
||||
case DIFF_INSERT:
|
||||
|
||||
count_insert++;
|
||||
text_insert += diffs[pointer][1];
|
||||
pointer++;
|
||||
@@ -504,9 +833,15 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
// inserting 'AC', and then the common suffix 'AC' will be eliminated. in this
|
||||
// particular case, both equalities go away, we absorb any previous inequalities,
|
||||
// and we keep scanning for the next equality before rewriting the tuples.
|
||||
if (previous_equality >= 0 && ends_with_pair_start(diffs[previous_equality][1])) {
|
||||
if (
|
||||
previous_equality >= 0 &&
|
||||
ends_with_pair_start(diffs[previous_equality][1])
|
||||
) {
|
||||
var stray = diffs[previous_equality][1].slice(-1);
|
||||
diffs[previous_equality][1] = diffs[previous_equality][1].slice(0, -1);
|
||||
diffs[previous_equality][1] = diffs[previous_equality][1].slice(
|
||||
0,
|
||||
-1
|
||||
);
|
||||
text_delete = stray + text_delete;
|
||||
text_insert = stray + text_insert;
|
||||
if (!diffs[previous_equality][1]) {
|
||||
@@ -546,9 +881,15 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
commonlength = diff_commonPrefix(text_insert, text_delete);
|
||||
if (commonlength !== 0) {
|
||||
if (previous_equality >= 0) {
|
||||
diffs[previous_equality][1] += text_insert.substring(0, commonlength);
|
||||
diffs[previous_equality][1] += text_insert.substring(
|
||||
0,
|
||||
commonlength
|
||||
);
|
||||
} else {
|
||||
diffs.splice(0, 0, [DIFF_EQUAL, text_insert.substring(0, commonlength)]);
|
||||
diffs.splice(0, 0, [
|
||||
DIFF_EQUAL,
|
||||
text_insert.substring(0, commonlength),
|
||||
]);
|
||||
pointer++;
|
||||
}
|
||||
text_insert = text_insert.substring(commonlength);
|
||||
@@ -558,9 +899,16 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
commonlength = diff_commonSuffix(text_insert, text_delete);
|
||||
if (commonlength !== 0) {
|
||||
diffs[pointer][1] =
|
||||
text_insert.substring(text_insert.length - commonlength) + diffs[pointer][1];
|
||||
text_insert = text_insert.substring(0, text_insert.length - commonlength);
|
||||
text_delete = text_delete.substring(0, text_delete.length - commonlength);
|
||||
text_insert.substring(text_insert.length - commonlength) +
|
||||
diffs[pointer][1];
|
||||
text_insert = text_insert.substring(
|
||||
0,
|
||||
text_insert.length - commonlength
|
||||
);
|
||||
text_delete = text_delete.substring(
|
||||
0,
|
||||
text_delete.length - commonlength
|
||||
);
|
||||
}
|
||||
}
|
||||
// Delete the offending records and add the merged ones.
|
||||
@@ -575,7 +923,12 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
diffs.splice(pointer - n, n, [DIFF_DELETE, text_delete]);
|
||||
pointer = pointer - n + 1;
|
||||
} else {
|
||||
diffs.splice(pointer - n, n, [DIFF_DELETE, text_delete], [DIFF_INSERT, text_insert]);
|
||||
diffs.splice(
|
||||
pointer - n,
|
||||
n,
|
||||
[DIFF_DELETE, text_delete],
|
||||
[DIFF_INSERT, text_insert]
|
||||
);
|
||||
pointer = pointer - n + 2;
|
||||
}
|
||||
}
|
||||
@@ -588,13 +941,13 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
}
|
||||
count_insert = 0;
|
||||
count_delete = 0;
|
||||
text_delete = '';
|
||||
text_insert = '';
|
||||
text_delete = "";
|
||||
text_insert = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (diffs[diffs.length - 1][1] === '') {
|
||||
diffs.pop(); // Remove the dummy entry at the end.
|
||||
if (diffs[diffs.length - 1][1] === "") {
|
||||
diffs.pop(); // Remove the dummy entry at the end.
|
||||
}
|
||||
|
||||
// Second pass: look for single edits surrounded on both sides by equalities
|
||||
@@ -604,20 +957,30 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
pointer = 1;
|
||||
// Intentionally ignore the first and last element (don't need checking).
|
||||
while (pointer < diffs.length - 1) {
|
||||
if (diffs[pointer - 1][0] === DIFF_EQUAL &&
|
||||
diffs[pointer + 1][0] === DIFF_EQUAL) {
|
||||
if (
|
||||
diffs[pointer - 1][0] === DIFF_EQUAL &&
|
||||
diffs[pointer + 1][0] === DIFF_EQUAL
|
||||
) {
|
||||
// This is a single edit surrounded by equalities.
|
||||
if (diffs[pointer][1].substring(diffs[pointer][1].length -
|
||||
diffs[pointer - 1][1].length) === diffs[pointer - 1][1]) {
|
||||
if (
|
||||
diffs[pointer][1].substring(
|
||||
diffs[pointer][1].length - diffs[pointer - 1][1].length
|
||||
) === diffs[pointer - 1][1]
|
||||
) {
|
||||
// Shift the edit over the previous equality.
|
||||
diffs[pointer][1] = diffs[pointer - 1][1] +
|
||||
diffs[pointer][1].substring(0, diffs[pointer][1].length -
|
||||
diffs[pointer - 1][1].length);
|
||||
diffs[pointer][1] =
|
||||
diffs[pointer - 1][1] +
|
||||
diffs[pointer][1].substring(
|
||||
0,
|
||||
diffs[pointer][1].length - diffs[pointer - 1][1].length
|
||||
);
|
||||
diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1];
|
||||
diffs.splice(pointer - 1, 1);
|
||||
changes = true;
|
||||
} else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) ==
|
||||
diffs[pointer + 1][1]) {
|
||||
} else if (
|
||||
diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) ==
|
||||
diffs[pointer + 1][1]
|
||||
) {
|
||||
// Shift the edit over the next equality.
|
||||
diffs[pointer - 1][1] += diffs[pointer + 1][1];
|
||||
diffs[pointer][1] =
|
||||
@@ -633,14 +996,14 @@ function diff_cleanupMerge(diffs, fix_unicode) {
|
||||
if (changes) {
|
||||
diff_cleanupMerge(diffs, fix_unicode);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function is_surrogate_pair_start(charCode) {
|
||||
return charCode >= 0xD800 && charCode <= 0xDBFF;
|
||||
return charCode >= 0xd800 && charCode <= 0xdbff;
|
||||
}
|
||||
|
||||
function is_surrogate_pair_end(charCode) {
|
||||
return charCode >= 0xDC00 && charCode <= 0xDFFF;
|
||||
return charCode >= 0xdc00 && charCode <= 0xdfff;
|
||||
}
|
||||
|
||||
function starts_with_pair_end(str) {
|
||||
@@ -669,16 +1032,17 @@ function make_edit_splice(before, oldMiddle, newMiddle, after) {
|
||||
[DIFF_EQUAL, before],
|
||||
[DIFF_DELETE, oldMiddle],
|
||||
[DIFF_INSERT, newMiddle],
|
||||
[DIFF_EQUAL, after]
|
||||
[DIFF_EQUAL, after],
|
||||
]);
|
||||
}
|
||||
|
||||
function find_cursor_edit_diff(oldText, newText, cursor_pos) {
|
||||
// note: this runs after equality check has ruled out exact equality
|
||||
var oldRange = typeof cursor_pos === 'number' ?
|
||||
{ index: cursor_pos, length: 0 } : cursor_pos.oldRange;
|
||||
var newRange = typeof cursor_pos === 'number' ?
|
||||
null : cursor_pos.newRange;
|
||||
var oldRange =
|
||||
typeof cursor_pos === "number"
|
||||
? { index: cursor_pos, length: 0 }
|
||||
: cursor_pos.oldRange;
|
||||
var newRange = typeof cursor_pos === "number" ? null : cursor_pos.newRange;
|
||||
// take into account the old and new selection to generate the best diff
|
||||
// possible for a text edit. for example, a text change from "xxx" to "xx"
|
||||
// could be a delete or forwards-delete of any one of the x's, or the
|
||||
@@ -761,10 +1125,10 @@ function find_cursor_edit_diff(oldText, newText, cursor_pos) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function diff(text1, text2, cursor_pos) {
|
||||
function diff(text1, text2, cursor_pos, cleanup) {
|
||||
// only pass fix_unicode=true at the top level, not when diff_main is
|
||||
// recursively invoked
|
||||
return diff_main(text1, text2, cursor_pos, true);
|
||||
return diff_main(text1, text2, cursor_pos, cleanup, true);
|
||||
}
|
||||
|
||||
diff.INSERT = DIFF_INSERT;
|
||||
|
||||
+8
-4
@@ -1,13 +1,17 @@
|
||||
{
|
||||
"name": "fast-diff",
|
||||
"version": "1.2.0",
|
||||
"version": "1.3.0",
|
||||
"description": "Fast Javascript text diff",
|
||||
"author": "Jason Chen <jhchen7@gmail.com>",
|
||||
"main": "diff.js",
|
||||
"types": "diff.d.ts",
|
||||
"files": [
|
||||
"diff.d.ts"
|
||||
],
|
||||
"devDependencies": {
|
||||
"lodash": "~3.9.3",
|
||||
"seedrandom": "~2.4.0"
|
||||
"lodash": "~4.17.21",
|
||||
"nyc": "~15.1.0",
|
||||
"seedrandom": "~3.0.5"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -17,7 +21,7 @@
|
||||
"url": "https://github.com/jhchen/fast-diff/issues"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node test.js"
|
||||
"test": "nyc node test.js"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"keywords": [
|
||||
|
||||
-334
@@ -1,334 +0,0 @@
|
||||
var _ = require('lodash');
|
||||
var seedrandom = require('seedrandom');
|
||||
var diff = require('./diff.js');
|
||||
|
||||
var ITERATIONS = 10000;
|
||||
var ALPHABET = 'GATTACA';
|
||||
var LENGTH = 100;
|
||||
var EMOJI_MAX_LENGTH = 50;
|
||||
|
||||
var seed = Math.floor(Math.random() * 10000);
|
||||
var random = seedrandom(seed);
|
||||
|
||||
console.log('Running regression tests...');
|
||||
[
|
||||
['GAATAAAAAAAGATTAACAT', 'AAAAACTTGTAATTAACAAC'],
|
||||
['🔘🤘🔗🔗', '🔗🤗🤗__🤗🤘🤘🤗🔗🤘🔗'],
|
||||
['🔗🤗🤗__🤗🤘🤘🤗🔗🤘🔗', '🤗🤘🔘'],
|
||||
['🤘🤘🔘🔘_🔘🔗🤘🤗🤗__🔗🤘', '🤘🔘🤘🔗🤘🤘🔗🤗🤘🔘🔘'],
|
||||
['🤗🤘🤗🔘🤘🔘🤗_🤗🔗🤘🤗_🤘🔗🤗🤘🔗🤘🤘🤘🔗🤗🔗🔗🔗🤗_🤘🔗🤗🤗🔘🤗🤗🤘🤗',
|
||||
'_🤗🤘_🤘🤘🔘🤗🔘🤘_🔘🤗🔗🔘🔗🤘🔗🤘🤗🔗🔗🔗🤘🔘_🤗🤘🤘🤘__🤘_🔘🤘🤘_🔗🤘🔘'],
|
||||
['🔗🤘🤗🔘🔘🤗', '🤘🤘🤘🤗🔘🔗🔗'],
|
||||
['🔘_🔗🔗🔗🤗🔗', '🤘🤗🔗🤗_🤘🔘_'],
|
||||
].forEach(function (data) {
|
||||
var result = diff(data[0], data[1]);
|
||||
applyDiff(result, data[0], data[1]);
|
||||
});
|
||||
|
||||
console.log('Running computing ' + ITERATIONS + ' diffs with seed ' + seed + '...');
|
||||
|
||||
console.log('Generating strings...');
|
||||
var strings = [];
|
||||
for (var i = 0; i <= ITERATIONS; ++i) {
|
||||
var chars = [];
|
||||
for (var l = 0; l < LENGTH; ++l) {
|
||||
var letter = ALPHABET.substr(Math.floor(random() * ALPHABET.length), 1);
|
||||
chars.push(letter);
|
||||
}
|
||||
strings.push(chars.join(''));
|
||||
}
|
||||
|
||||
console.log('Running fuzz tests *without* cursor information...');
|
||||
for (var i = 0; i < ITERATIONS; ++i) {
|
||||
var result = diff(strings[i], strings[i + 1]);
|
||||
applyDiff(result, strings[i], strings[i + 1]);
|
||||
}
|
||||
|
||||
console.log('Running fuzz tests *with* cursor information');
|
||||
for (var i = 0; i < ITERATIONS; ++i) {
|
||||
var cursor_pos = Math.floor(random() * strings[i].length + 1);
|
||||
var diffs = diff(strings[i], strings[i + 1], cursor_pos);
|
||||
applyDiff(diffs, strings[i], strings[i + 1]);
|
||||
}
|
||||
|
||||
function parseDiff(str) {
|
||||
if (!str) {
|
||||
return [];
|
||||
}
|
||||
return str.split(/(?=[+\-=])/).map(function (piece) {
|
||||
var symbol = piece.charAt(0);
|
||||
var text = piece.slice(1);
|
||||
return [
|
||||
symbol === '+' ? diff.INSERT : symbol === '-' ? diff.DELETE : diff.EQUAL,
|
||||
text
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Running cursor tests');
|
||||
[
|
||||
['', 0, '', null, ''],
|
||||
|
||||
['', 0, 'a', null, '+a'],
|
||||
['a', 0, 'aa', null, '+a=a'],
|
||||
['a', 1, 'aa', null, '=a+a'],
|
||||
['aa', 0, 'aaa', null, '+a=aa'],
|
||||
['aa', 1, 'aaa', null, '=a+a=a'],
|
||||
['aa', 2, 'aaa', null, '=aa+a'],
|
||||
['aaa', 0, 'aaaa', null, '+a=aaa'],
|
||||
['aaa', 1, 'aaaa', null, '=a+a=aa'],
|
||||
['aaa', 2, 'aaaa', null, '=aa+a=a'],
|
||||
['aaa', 3, 'aaaa', null, '=aaa+a'],
|
||||
|
||||
['a', 0, '', null, '-a'],
|
||||
['a', 1, '', null, '-a'],
|
||||
['aa', 0, 'a', null, '-a=a'],
|
||||
['aa', 1, 'a', null, '-a=a'],
|
||||
['aa', 2, 'a', null, '=a-a'],
|
||||
['aaa', 0, 'aa', null, '-a=aa'],
|
||||
['aaa', 1, 'aa', null, '-a=aa'],
|
||||
['aaa', 2, 'aa', null, '=a-a=a'],
|
||||
['aaa', 3, 'aa', null, '=aa-a'],
|
||||
|
||||
['', 0, '', 0, ''],
|
||||
|
||||
['', 0, 'a', 1, '+a'],
|
||||
['a', 0, 'aa', 1, '+a=a'],
|
||||
['a', 1, 'aa', 2, '=a+a'],
|
||||
['aa', 0, 'aaa', 1, '+a=aa'],
|
||||
['aa', 1, 'aaa', 2, '=a+a=a'],
|
||||
['aa', 2, 'aaa', 3, '=aa+a'],
|
||||
['aaa', 0, 'aaaa', 1, '+a=aaa'],
|
||||
['aaa', 1, 'aaaa', 2, '=a+a=aa'],
|
||||
['aaa', 2, 'aaaa', 3, '=aa+a=a'],
|
||||
['aaa', 3, 'aaaa', 4, '=aaa+a'],
|
||||
|
||||
['a', 1, '', 0, '-a'],
|
||||
['aa', 1, 'a', 0, '-a=a'],
|
||||
['aa', 2, 'a', 1, '=a-a'],
|
||||
['aaa', 1, 'aa', 0, '-a=aa'],
|
||||
['aaa', 2, 'aa', 1, '=a-a=a'],
|
||||
['aaa', 3, 'aa', 2, '=aa-a'],
|
||||
|
||||
['a', 1, '', 0, '-a'],
|
||||
['aa', 1, 'a', 0, '-a=a'],
|
||||
['aa', 2, 'a', 1, '=a-a'],
|
||||
['aaa', 1, 'aa', 0, '-a=aa'],
|
||||
['aaa', 2, 'aa', 1, '=a-a=a'],
|
||||
['aaa', 3, 'aa', 2, '=aa-a'],
|
||||
|
||||
// forward-delete
|
||||
['a', 0, '', 0, '-a'],
|
||||
['aa', 0, 'a', 0, '-a=a'],
|
||||
['aa', 1, 'a', 1, '=a-a'],
|
||||
['aaa', 0, 'aa', 0, '-a=aa'],
|
||||
['aaa', 1, 'aa', 1, '=a-a=a'],
|
||||
['aaa', 2, 'aa', 2, '=aa-a'],
|
||||
|
||||
['bob', 0, 'bobob', null, '+bo=bob'],
|
||||
['bob', 1, 'bobob', null, '=b+ob=ob'],
|
||||
['bob', 2, 'bobob', null, '=bo+bo=b'],
|
||||
['bob', 3, 'bobob', null, '=bob+ob'],
|
||||
['bob', 0, 'bobob', 2, '+bo=bob'],
|
||||
['bob', 1, 'bobob', 3, '=b+ob=ob'],
|
||||
['bob', 2, 'bobob', 4, '=bo+bo=b'],
|
||||
['bob', 3, 'bobob', 5, '=bob+ob'],
|
||||
['bobob', 2, 'bob', null, '-bo=bob'],
|
||||
['bobob', 3, 'bob', null, '=b-ob=ob'],
|
||||
['bobob', 4, 'bob', null, '=bo-bo=b'],
|
||||
['bobob', 5, 'bob', null, '=bob-ob'],
|
||||
['bobob', 2, 'bob', 0, '-bo=bob'],
|
||||
['bobob', 3, 'bob', 1, '=b-ob=ob'],
|
||||
['bobob', 4, 'bob', 2, '=bo-bo=b'],
|
||||
['bobob', 5, 'bob', 3, '=bob-ob'],
|
||||
|
||||
['bob', 1, 'b', null, '=b-ob'],
|
||||
|
||||
['hello', [0, 5], 'h', 1, '-hello+h'],
|
||||
['yay', [0, 3], 'y', 1, '-yay+y'],
|
||||
['bobob', [1, 4], 'bob', 2, '=b-obo+o=b'],
|
||||
|
||||
].forEach(function (data) {
|
||||
var oldText = data[0];
|
||||
var newText = data[2];
|
||||
var oldRange = typeof data[1] === 'number' ?
|
||||
{ index: data[1], length: 0 } :
|
||||
{ index: data[1][0], length: data[1][1] - data[1][0] };
|
||||
var newRange = typeof data[3] === 'number' ?
|
||||
{ index: data[3], length: 0 } :
|
||||
data[3] === null ? null : { index: data[3][0], length: data[3][1] - data[3][0] };
|
||||
var expected = parseDiff(data[4]);
|
||||
if (newRange === null && typeof data[1] !== 'number') {
|
||||
throw new Error('invalid test case');
|
||||
}
|
||||
var cursorInfo = newRange === null ? data[1] : {
|
||||
oldRange: oldRange,
|
||||
newRange: newRange,
|
||||
};
|
||||
doCursorTest(oldText, newText, cursorInfo, expected);
|
||||
doCursorTest('x' + oldText, 'x' + newText, shiftCursorInfo(cursorInfo, 1), diffPrepend(expected, 'x'));
|
||||
doCursorTest(oldText + 'x', newText + 'x', cursorInfo, diffAppend(expected, 'x'));
|
||||
});
|
||||
|
||||
function diffPrepend(tuples, text) {
|
||||
if (tuples.length > 0 && tuples[0][0] === diff.EQUAL) {
|
||||
return [[diff.EQUAL, text + tuples[0][1]]].concat(tuples.slice(1));
|
||||
} else {
|
||||
return [[diff.EQUAL, text]].concat(tuples);
|
||||
}
|
||||
}
|
||||
|
||||
function diffAppend(tuples, text) {
|
||||
var lastTuple = tuples[tuples.length - 1];
|
||||
if (lastTuple && lastTuple[0] === diff.EQUAL) {
|
||||
return tuples.slice(0, -1).concat([[diff.EQUAL, lastTuple[1] + text]]);
|
||||
} else {
|
||||
return tuples.concat([[diff.EQUAL, text]]);
|
||||
}
|
||||
}
|
||||
|
||||
function shiftCursorInfo(cursorInfo, amount) {
|
||||
if (typeof cursorInfo === 'number') {
|
||||
return cursorInfo + amount;
|
||||
} else {
|
||||
return {
|
||||
oldRange: {
|
||||
index: cursorInfo.oldRange.index + amount,
|
||||
length: cursorInfo.oldRange.length,
|
||||
},
|
||||
newRange: {
|
||||
index: cursorInfo.newRange.index + amount,
|
||||
length: cursorInfo.newRange.length,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function doCursorTest(oldText, newText, cursorInfo, expected) {
|
||||
var result = diff(oldText, newText, cursorInfo);
|
||||
if (!_.isEqual(result, expected)) {
|
||||
console.log([oldText, newText, cursorInfo]);
|
||||
console.log(result, '!==', expected);
|
||||
throw new Error('cursor test failed');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Running emoji tests');
|
||||
[
|
||||
['🐶', '🐯', '-🐶+🐯'],
|
||||
['👨🏽', '👩🏽', '-👨+👩=🏽'],
|
||||
['👩🏼', '👩🏽', '=👩-🏼+🏽'],
|
||||
|
||||
['🍏🍎', '🍎', '-🍏=🍎'],
|
||||
['🍎', '🍏🍎', '+🍏=🍎'],
|
||||
|
||||
].forEach(function (data) {
|
||||
var oldText = data[0];
|
||||
var newText = data[1];
|
||||
var expected = parseDiff(data[2]);
|
||||
doEmojiTest(oldText, newText, expected);
|
||||
doEmojiTest('x' + oldText, 'x' + newText, diffPrepend(expected, 'x'));
|
||||
doEmojiTest(oldText + 'x', newText + 'x', diffAppend(expected, 'x'));
|
||||
});
|
||||
|
||||
function doEmojiTest(oldText, newText, expected) {
|
||||
var result = diff(oldText, newText);
|
||||
if (!_.isEqual(result, expected)) {
|
||||
console.log(oldText, newText, expected);
|
||||
console.log(result, '!==', expected);
|
||||
throw new Error('Emoji simple test case failed');
|
||||
}
|
||||
}
|
||||
|
||||
// emojis chosen to share high and low surrogates!
|
||||
var EMOJI_ALPHABET = ['_', '🤗', '🔗', '🤘', '🔘'];
|
||||
|
||||
console.log('Generating emoji strings...');
|
||||
var emoji_strings = [];
|
||||
for (var i = 0; i <= ITERATIONS; ++i) {
|
||||
var letters = [];
|
||||
var len = Math.floor(random() * EMOJI_MAX_LENGTH);
|
||||
for (var l = 0; l < len; ++l) {
|
||||
var letter = EMOJI_ALPHABET[Math.floor(random() * EMOJI_ALPHABET.length)];
|
||||
letters.push(letter);
|
||||
}
|
||||
emoji_strings.push(letters.join(''));
|
||||
}
|
||||
|
||||
console.log('Running emoji fuzz tests...');
|
||||
for (var i = 0; i < ITERATIONS; ++i) {
|
||||
var oldText = emoji_strings[i];
|
||||
var newText = emoji_strings[i + 1];
|
||||
var result = diff(oldText, newText);
|
||||
applyDiff(result, oldText, newText);
|
||||
}
|
||||
|
||||
// Applies a diff to text, throwing an error if diff is invalid or incorrect
|
||||
function applyDiff(diffs, text, expectedResult) {
|
||||
var pos = 0;
|
||||
function throwError(message) {
|
||||
console.log(diffs, text, expectedResult);
|
||||
throw new Error(message);
|
||||
}
|
||||
function expect(expected) {
|
||||
var found = text.substr(pos, expected.length);
|
||||
if (found !== expected) {
|
||||
throwError('Expected "' + expected + '", found "' + found + '"');
|
||||
}
|
||||
}
|
||||
var result = '';
|
||||
var inserts_since_last_equality = 0;
|
||||
var deletes_since_last_equality = 0;
|
||||
for (var i = 0; i < diffs.length; i++) {
|
||||
var d = diffs[i];
|
||||
if (!d[1]) {
|
||||
throwError('Empty tuple in diff')
|
||||
}
|
||||
var firstCharCode = d[1].charCodeAt(0);
|
||||
var lastCharCode = d[1].slice(-1).charCodeAt(0);
|
||||
if (firstCharCode >= 0xDC00 && firstCharCode <= 0xDFFF ||
|
||||
lastCharCode >= 0xD800 && lastCharCode <= 0xDBFF) {
|
||||
throwError('Bad unicode diff tuple')
|
||||
}
|
||||
switch (d[0]) {
|
||||
case diff.EQUAL:
|
||||
if (i !== 0 && !inserts_since_last_equality && !deletes_since_last_equality) {
|
||||
throwError('two consecutive equalities in diff');
|
||||
}
|
||||
inserts_since_last_equality = 0;
|
||||
deletes_since_last_equality = 0;
|
||||
expect(d[1]);
|
||||
result += d[1];
|
||||
pos += d[1].length;
|
||||
break;
|
||||
case diff.DELETE:
|
||||
if (deletes_since_last_equality) {
|
||||
throwError('multiple deletes between equalities')
|
||||
}
|
||||
if (inserts_since_last_equality) {
|
||||
throwError('delete following insert in diff')
|
||||
}
|
||||
deletes_since_last_equality++;
|
||||
expect(d[1]);
|
||||
pos += d[1].length;
|
||||
break
|
||||
case diff.INSERT:
|
||||
if (inserts_since_last_equality) {
|
||||
throwError('multiple inserts between equalities')
|
||||
}
|
||||
inserts_since_last_equality++;
|
||||
result += d[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos !== text.length) {
|
||||
throwError('Diff did not consume entire input text');
|
||||
}
|
||||
if (result !== expectedResult) {
|
||||
console.log(diffs, text, expectedResult, result);
|
||||
throw new Error('Diff not correct')
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
console.log("Success!");
|
||||
Reference in New Issue
Block a user