mirror of
https://github.com/shimataro/ssh-key-action.git
synced 2025-06-19 22:52:10 +10:00
* first action! (#1)
This commit is contained in:
parent
8deacc95b1
commit
ace1e6a69a
3750 changed files with 1155519 additions and 0 deletions
2
node_modules/fast-diff/.travis.yml
generated
vendored
Normal file
2
node_modules/fast-diff/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
language: node_js
|
||||
node_js: '0.10'
|
24
node_modules/fast-diff/README.md
generated
vendored
Normal file
24
node_modules/fast-diff/README.md
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Fast Diff [](https://travis-ci.org/jhchen/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.
|
||||
|
||||
The diff function is an implementation of ["An O(ND) Difference Algorithm and its Variations" (Myers, 1986)](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.4.6927&rep=rep1&type=pdf) with the suggested divide and conquer strategy along with several [optimizations](http://neil.fraser.name/news/2007/10/09/) Neil added.
|
||||
|
||||
```js
|
||||
var diff = require('fast-diff');
|
||||
|
||||
var good = 'Good dog';
|
||||
var bad = 'Bad dog';
|
||||
|
||||
var result = diff(good, bad);
|
||||
// [[-1, "Goo"], [1, "Ba"], [0, "d dog"]]
|
||||
|
||||
// Respect suggested edit location (cursor position), added in v1.1
|
||||
diff('aaa', 'aaaa', 1)
|
||||
// [[0, "a"], [1, "a"], [0, "aa"]]
|
||||
|
||||
// For convenience
|
||||
diff.INSERT === 1;
|
||||
diff.EQUAL === 0;
|
||||
diff.DELETE === -1;
|
||||
```
|
20
node_modules/fast-diff/diff.d.ts
generated
vendored
Normal file
20
node_modules/fast-diff/diff.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
declare function diff(
|
||||
text1: string,
|
||||
text2: string,
|
||||
cursorPos?: number | diff.CursorInfo
|
||||
): diff.Diff[];
|
||||
|
||||
declare namespace diff {
|
||||
type Diff = [-1 | 0 | 1, string];
|
||||
|
||||
const DELETE: -1;
|
||||
const INSERT: 1;
|
||||
const EQUAL: 0;
|
||||
|
||||
interface CursorInfo {
|
||||
oldRange: { index: number; length: number };
|
||||
newRange: { index: number; length: number };
|
||||
}
|
||||
}
|
||||
|
||||
export = diff;
|
774
node_modules/fast-diff/diff.js
generated
vendored
Normal file
774
node_modules/fast-diff/diff.js
generated
vendored
Normal file
|
@ -0,0 +1,774 @@
|
|||
/**
|
||||
* This library modifies the diff-patch-match library by Neil Fraser
|
||||
* by removing the patch and match functionality and certain advanced
|
||||
* options in the diff function. The original license is as follows:
|
||||
*
|
||||
* ===
|
||||
*
|
||||
* Diff Match and Patch
|
||||
*
|
||||
* Copyright 2006 Google Inc.
|
||||
* http://code.google.com/p/google-diff-match-patch/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The data structure representing a diff is an array of tuples:
|
||||
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
|
||||
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
|
||||
*/
|
||||
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
|
||||
* @return {Array} Array of diff tuples.
|
||||
*/
|
||||
function diff_main(text1, text2, cursor_pos, _fix_unicode) {
|
||||
// Check for equality
|
||||
if (text1 === text2) {
|
||||
if (text1) {
|
||||
return [[DIFF_EQUAL, text1]];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
if (cursor_pos != null) {
|
||||
var editdiff = find_cursor_edit_diff(text1, text2, cursor_pos);
|
||||
if (editdiff) {
|
||||
return editdiff;
|
||||
}
|
||||
}
|
||||
|
||||
// Trim off common prefix (speedup).
|
||||
var commonlength = diff_commonPrefix(text1, text2);
|
||||
var commonprefix = text1.substring(0, commonlength);
|
||||
text1 = text1.substring(commonlength);
|
||||
text2 = text2.substring(commonlength);
|
||||
|
||||
// Trim off common suffix (speedup).
|
||||
commonlength = diff_commonSuffix(text1, text2);
|
||||
var commonsuffix = text1.substring(text1.length - commonlength);
|
||||
text1 = text1.substring(0, text1.length - commonlength);
|
||||
text2 = text2.substring(0, text2.length - commonlength);
|
||||
|
||||
// Compute the diff on the middle block.
|
||||
var diffs = diff_compute_(text1, text2);
|
||||
|
||||
// Restore the prefix and suffix.
|
||||
if (commonprefix) {
|
||||
diffs.unshift([DIFF_EQUAL, commonprefix]);
|
||||
}
|
||||
if (commonsuffix) {
|
||||
diffs.push([DIFF_EQUAL, commonsuffix]);
|
||||
}
|
||||
diff_cleanupMerge(diffs, _fix_unicode);
|
||||
return diffs;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Find the differences between two texts. Assumes that the texts do not
|
||||
* have any common prefix or suffix.
|
||||
* @param {string} text1 Old string to be diffed.
|
||||
* @param {string} text2 New string to be diffed.
|
||||
* @return {Array} Array of diff tuples.
|
||||
*/
|
||||
function diff_compute_(text1, text2) {
|
||||
var diffs;
|
||||
|
||||
if (!text1) {
|
||||
// Just add some text (speedup).
|
||||
return [[DIFF_INSERT, text2]];
|
||||
}
|
||||
|
||||
if (!text2) {
|
||||
// Just delete some text (speedup).
|
||||
return [[DIFF_DELETE, text1]];
|
||||
}
|
||||
|
||||
var longtext = text1.length > text2.length ? text1 : text2;
|
||||
var shorttext = text1.length > text2.length ? text2 : text1;
|
||||
var i = longtext.indexOf(shorttext);
|
||||
if (i !== -1) {
|
||||
// Shorter text is inside the longer text (speedup).
|
||||
diffs = [
|
||||
[DIFF_INSERT, longtext.substring(0, i)],
|
||||
[DIFF_EQUAL, shorttext],
|
||||
[DIFF_INSERT, longtext.substring(i + shorttext.length)]
|
||||
];
|
||||
// Swap insertions for deletions if diff is reversed.
|
||||
if (text1.length > text2.length) {
|
||||
diffs[0][0] = diffs[2][0] = DIFF_DELETE;
|
||||
}
|
||||
return diffs;
|
||||
}
|
||||
|
||||
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]];
|
||||
}
|
||||
|
||||
// Check to see if the problem can be split in two.
|
||||
var hm = diff_halfMatch_(text1, text2);
|
||||
if (hm) {
|
||||
// A half-match was found, sort out the return data.
|
||||
var text1_a = hm[0];
|
||||
var text1_b = hm[1];
|
||||
var text2_a = hm[2];
|
||||
var text2_b = hm[3];
|
||||
var mid_common = hm[4];
|
||||
// Send both pairs off for separate processing.
|
||||
var diffs_a = diff_main(text1_a, text2_a);
|
||||
var diffs_b = diff_main(text1_b, text2_b);
|
||||
// Merge the results.
|
||||
return diffs_a.concat([[DIFF_EQUAL, mid_common]], diffs_b);
|
||||
}
|
||||
|
||||
return diff_bisect_(text1, text2);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Find the 'middle snake' of a diff, split the problem in two
|
||||
* and return the recursively constructed diff.
|
||||
* See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
|
||||
* @param {string} text1 Old string to be diffed.
|
||||
* @param {string} text2 New string to be diffed.
|
||||
* @return {Array} Array of diff tuples.
|
||||
* @private
|
||||
*/
|
||||
function diff_bisect_(text1, text2) {
|
||||
// Cache the text lengths to prevent multiple calls.
|
||||
var text1_length = text1.length;
|
||||
var text2_length = text2.length;
|
||||
var max_d = Math.ceil((text1_length + text2_length) / 2);
|
||||
var v_offset = max_d;
|
||||
var v_length = 2 * max_d;
|
||||
var v1 = new Array(v_length);
|
||||
var v2 = new Array(v_length);
|
||||
// Setting all elements to -1 is faster in Chrome & Firefox than mixing
|
||||
// integers and undefined.
|
||||
for (var x = 0; x < v_length; x++) {
|
||||
v1[x] = -1;
|
||||
v2[x] = -1;
|
||||
}
|
||||
v1[v_offset + 1] = 0;
|
||||
v2[v_offset + 1] = 0;
|
||||
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);
|
||||
// Offsets for start and end of k loop.
|
||||
// Prevents mapping of space beyond the grid.
|
||||
var k1start = 0;
|
||||
var k1end = 0;
|
||||
var k2start = 0;
|
||||
var k2end = 0;
|
||||
for (var d = 0; d < max_d; d++) {
|
||||
// Walk the front path one step.
|
||||
for (var k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
||||
var k1_offset = v_offset + k1;
|
||||
var x1;
|
||||
if (k1 === -d || (k1 !== d && v1[k1_offset - 1] < v1[k1_offset + 1])) {
|
||||
x1 = v1[k1_offset + 1];
|
||||
} else {
|
||||
x1 = v1[k1_offset - 1] + 1;
|
||||
}
|
||||
var y1 = x1 - k1;
|
||||
while (
|
||||
x1 < text1_length && y1 < text2_length &&
|
||||
text1.charAt(x1) === text2.charAt(y1)
|
||||
) {
|
||||
x1++;
|
||||
y1++;
|
||||
}
|
||||
v1[k1_offset] = x1;
|
||||
if (x1 > text1_length) {
|
||||
// Ran off the right of the graph.
|
||||
k1end += 2;
|
||||
} else if (y1 > text2_length) {
|
||||
// Ran off the bottom of the graph.
|
||||
k1start += 2;
|
||||
} else if (front) {
|
||||
var k2_offset = v_offset + delta - k1;
|
||||
if (k2_offset >= 0 && k2_offset < v_length && v2[k2_offset] !== -1) {
|
||||
// Mirror x2 onto top-left coordinate system.
|
||||
var x2 = text1_length - v2[k2_offset];
|
||||
if (x1 >= x2) {
|
||||
// Overlap detected.
|
||||
return diff_bisectSplit_(text1, text2, x1, y1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Walk the reverse path one step.
|
||||
for (var k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
|
||||
var k2_offset = v_offset + k2;
|
||||
var x2;
|
||||
if (k2 === -d || (k2 !== d && v2[k2_offset - 1] < v2[k2_offset + 1])) {
|
||||
x2 = v2[k2_offset + 1];
|
||||
} else {
|
||||
x2 = v2[k2_offset - 1] + 1;
|
||||
}
|
||||
var y2 = x2 - k2;
|
||||
while (
|
||||
x2 < text1_length && y2 < text2_length &&
|
||||
text1.charAt(text1_length - x2 - 1) === text2.charAt(text2_length - y2 - 1)
|
||||
) {
|
||||
x2++;
|
||||
y2++;
|
||||
}
|
||||
v2[k2_offset] = x2;
|
||||
if (x2 > text1_length) {
|
||||
// Ran off the left of the graph.
|
||||
k2end += 2;
|
||||
} else if (y2 > text2_length) {
|
||||
// Ran off the top of the graph.
|
||||
k2start += 2;
|
||||
} else if (!front) {
|
||||
var k1_offset = v_offset + delta - k2;
|
||||
if (k1_offset >= 0 && k1_offset < v_length && v1[k1_offset] !== -1) {
|
||||
var x1 = v1[k1_offset];
|
||||
var y1 = v_offset + x1 - k1_offset;
|
||||
// Mirror x2 onto top-left coordinate system.
|
||||
x2 = text1_length - x2;
|
||||
if (x1 >= x2) {
|
||||
// Overlap detected.
|
||||
return diff_bisectSplit_(text1, text2, x1, y1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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]];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Given the location of the 'middle snake', split the diff in two parts
|
||||
* and recurse.
|
||||
* @param {string} text1 Old string to be diffed.
|
||||
* @param {string} text2 New string to be diffed.
|
||||
* @param {number} x Index of split point in text1.
|
||||
* @param {number} y Index of split point in text2.
|
||||
* @return {Array} Array of diff tuples.
|
||||
*/
|
||||
function diff_bisectSplit_(text1, text2, x, y) {
|
||||
var text1a = text1.substring(0, x);
|
||||
var text2a = text2.substring(0, y);
|
||||
var text1b = text1.substring(x);
|
||||
var text2b = text2.substring(y);
|
||||
|
||||
// Compute both diffs serially.
|
||||
var diffs = diff_main(text1a, text2a);
|
||||
var diffsb = diff_main(text1b, text2b);
|
||||
|
||||
return diffs.concat(diffsb);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Determine the common prefix of two strings.
|
||||
* @param {string} text1 First string.
|
||||
* @param {string} text2 Second string.
|
||||
* @return {number} The number of characters common to the start of each
|
||||
* string.
|
||||
*/
|
||||
function diff_commonPrefix(text1, text2) {
|
||||
// Quick check for common null cases.
|
||||
if (!text1 || !text2 || text1.charAt(0) !== text2.charAt(0)) {
|
||||
return 0;
|
||||
}
|
||||
// Binary search.
|
||||
// Performance analysis: http://neil.fraser.name/news/2007/10/09/
|
||||
var pointermin = 0;
|
||||
var pointermax = Math.min(text1.length, text2.length);
|
||||
var pointermid = pointermax;
|
||||
var pointerstart = 0;
|
||||
while (pointermin < pointermid) {
|
||||
if (
|
||||
text1.substring(pointerstart, pointermid) ==
|
||||
text2.substring(pointerstart, pointermid)
|
||||
) {
|
||||
pointermin = pointermid;
|
||||
pointerstart = pointermin;
|
||||
} else {
|
||||
pointermax = pointermid;
|
||||
}
|
||||
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
||||
}
|
||||
|
||||
if (is_surrogate_pair_start(text1.charCodeAt(pointermid - 1))) {
|
||||
pointermid--;
|
||||
}
|
||||
|
||||
return pointermid;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Determine the common suffix of two strings.
|
||||
* @param {string} text1 First string.
|
||||
* @param {string} text2 Second string.
|
||||
* @return {number} The number of characters common to the end of each string.
|
||||
*/
|
||||
function diff_commonSuffix(text1, text2) {
|
||||
// Quick check for common null cases.
|
||||
if (!text1 || !text2 || text1.slice(-1) !== text2.slice(-1)) {
|
||||
return 0;
|
||||
}
|
||||
// Binary search.
|
||||
// Performance analysis: http://neil.fraser.name/news/2007/10/09/
|
||||
var pointermin = 0;
|
||||
var pointermax = Math.min(text1.length, text2.length);
|
||||
var pointermid = pointermax;
|
||||
var pointerend = 0;
|
||||
while (pointermin < pointermid) {
|
||||
if (
|
||||
text1.substring(text1.length - pointermid, text1.length - pointerend) ==
|
||||
text2.substring(text2.length - pointermid, text2.length - pointerend)
|
||||
) {
|
||||
pointermin = pointermid;
|
||||
pointerend = pointermin;
|
||||
} else {
|
||||
pointermax = pointermid;
|
||||
}
|
||||
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
||||
}
|
||||
|
||||
if (is_surrogate_pair_end(text1.charCodeAt(text1.length - pointermid))) {
|
||||
pointermid--;
|
||||
}
|
||||
|
||||
return pointermid;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Do the two texts share a substring which is at least half the length of the
|
||||
* longer text?
|
||||
* This speedup can produce non-minimal diffs.
|
||||
* @param {string} text1 First string.
|
||||
* @param {string} text2 Second string.
|
||||
* @return {Array.<string>} Five element Array, containing the prefix of
|
||||
* text1, the suffix of text1, the prefix of text2, the suffix of
|
||||
* text2 and the common middle. Or null if there was no match.
|
||||
*/
|
||||
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.
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a substring of shorttext exist within longtext such that the substring
|
||||
* is at least half the length of longtext?
|
||||
* Closure, but does not reference any external variables.
|
||||
* @param {string} longtext Longer string.
|
||||
* @param {string} shorttext Shorter string.
|
||||
* @param {number} i Start index of quarter length substring within longtext.
|
||||
* @return {Array.<string>} Five element Array, containing the prefix of
|
||||
* longtext, the suffix of longtext, the prefix of shorttext, the suffix
|
||||
* of shorttext and the common middle. Or null if there was no match.
|
||||
* @private
|
||||
*/
|
||||
function diff_halfMatchI_(longtext, shorttext, i) {
|
||||
// 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_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));
|
||||
var suffixLength = diff_commonSuffix(
|
||||
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_longtext_a = longtext.substring(0, i - suffixLength);
|
||||
best_longtext_b = longtext.substring(i + prefixLength);
|
||||
best_shorttext_a = shorttext.substring(0, j - suffixLength);
|
||||
best_shorttext_b = shorttext.substring(j + prefixLength);
|
||||
}
|
||||
}
|
||||
if (best_common.length * 2 >= longtext.length) {
|
||||
return [
|
||||
best_longtext_a, best_longtext_b,
|
||||
best_shorttext_a, best_shorttext_b, best_common
|
||||
];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// First check if the second quarter is the seed for a half-match.
|
||||
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 hm;
|
||||
if (!hm1 && !hm2) {
|
||||
return null;
|
||||
} else if (!hm2) {
|
||||
hm = hm1;
|
||||
} else if (!hm1) {
|
||||
hm = hm2;
|
||||
} else {
|
||||
// Both matched. Select the longest.
|
||||
hm = hm1[4].length > hm2[4].length ? hm1 : hm2;
|
||||
}
|
||||
|
||||
// A half-match was found, sort out the return data.
|
||||
var text1_a, text1_b, text2_a, text2_b;
|
||||
if (text1.length > text2.length) {
|
||||
text1_a = hm[0];
|
||||
text1_b = hm[1];
|
||||
text2_a = hm[2];
|
||||
text2_b = hm[3];
|
||||
} else {
|
||||
text2_a = hm[0];
|
||||
text2_b = hm[1];
|
||||
text1_a = hm[2];
|
||||
text1_b = hm[3];
|
||||
}
|
||||
var mid_common = hm[4];
|
||||
return [text1_a, text1_b, text2_a, text2_b, mid_common];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Reorder and merge like edit sections. Merge equalities.
|
||||
* Any edit section can move as long as it doesn't cross an equality.
|
||||
* @param {Array} diffs Array of diff tuples.
|
||||
* @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.
|
||||
var pointer = 0;
|
||||
var count_delete = 0;
|
||||
var count_insert = 0;
|
||||
var text_delete = '';
|
||||
var text_insert = '';
|
||||
var commonlength;
|
||||
while (pointer < diffs.length) {
|
||||
if (pointer < diffs.length - 1 && !diffs[pointer][1]) {
|
||||
diffs.splice(pointer, 1);
|
||||
continue;
|
||||
}
|
||||
switch (diffs[pointer][0]) {
|
||||
case DIFF_INSERT:
|
||||
|
||||
count_insert++;
|
||||
text_insert += diffs[pointer][1];
|
||||
pointer++;
|
||||
break;
|
||||
case DIFF_DELETE:
|
||||
count_delete++;
|
||||
text_delete += diffs[pointer][1];
|
||||
pointer++;
|
||||
break;
|
||||
case DIFF_EQUAL:
|
||||
var previous_equality = pointer - count_insert - count_delete - 1;
|
||||
if (fix_unicode) {
|
||||
// prevent splitting of unicode surrogate pairs. when fix_unicode is true,
|
||||
// we assume that the old and new text in the diff are complete and correct
|
||||
// unicode-encoded JS strings, but the tuple boundaries may fall between
|
||||
// surrogate pairs. we fix this by shaving off stray surrogates from the end
|
||||
// of the previous equality and the beginning of this equality. this may create
|
||||
// empty equalities or a common prefix or suffix. for example, if AB and AC are
|
||||
// emojis, `[[0, 'A'], [-1, 'BA'], [0, 'C']]` would turn into deleting 'ABAC' and
|
||||
// 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])) {
|
||||
var stray = diffs[previous_equality][1].slice(-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]) {
|
||||
// emptied out previous equality, so delete it and include previous delete/insert
|
||||
diffs.splice(previous_equality, 1);
|
||||
pointer--;
|
||||
var k = previous_equality - 1;
|
||||
if (diffs[k] && diffs[k][0] === DIFF_INSERT) {
|
||||
count_insert++;
|
||||
text_insert = diffs[k][1] + text_insert;
|
||||
k--;
|
||||
}
|
||||
if (diffs[k] && diffs[k][0] === DIFF_DELETE) {
|
||||
count_delete++;
|
||||
text_delete = diffs[k][1] + text_delete;
|
||||
k--;
|
||||
}
|
||||
previous_equality = k;
|
||||
}
|
||||
}
|
||||
if (starts_with_pair_end(diffs[pointer][1])) {
|
||||
var stray = diffs[pointer][1].charAt(0);
|
||||
diffs[pointer][1] = diffs[pointer][1].slice(1);
|
||||
text_delete += stray;
|
||||
text_insert += stray;
|
||||
}
|
||||
}
|
||||
if (pointer < diffs.length - 1 && !diffs[pointer][1]) {
|
||||
// for empty equality not at end, wait for next equality
|
||||
diffs.splice(pointer, 1);
|
||||
break;
|
||||
}
|
||||
if (text_delete.length > 0 || text_insert.length > 0) {
|
||||
// note that diff_commonPrefix and diff_commonSuffix are unicode-aware
|
||||
if (text_delete.length > 0 && text_insert.length > 0) {
|
||||
// Factor out any common prefixes.
|
||||
commonlength = diff_commonPrefix(text_insert, text_delete);
|
||||
if (commonlength !== 0) {
|
||||
if (previous_equality >= 0) {
|
||||
diffs[previous_equality][1] += text_insert.substring(0, commonlength);
|
||||
} else {
|
||||
diffs.splice(0, 0, [DIFF_EQUAL, text_insert.substring(0, commonlength)]);
|
||||
pointer++;
|
||||
}
|
||||
text_insert = text_insert.substring(commonlength);
|
||||
text_delete = text_delete.substring(commonlength);
|
||||
}
|
||||
// Factor out any common suffixes.
|
||||
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);
|
||||
}
|
||||
}
|
||||
// Delete the offending records and add the merged ones.
|
||||
var n = count_insert + count_delete;
|
||||
if (text_delete.length === 0 && text_insert.length === 0) {
|
||||
diffs.splice(pointer - n, n);
|
||||
pointer = pointer - n;
|
||||
} else if (text_delete.length === 0) {
|
||||
diffs.splice(pointer - n, n, [DIFF_INSERT, text_insert]);
|
||||
pointer = pointer - n + 1;
|
||||
} else if (text_insert.length === 0) {
|
||||
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]);
|
||||
pointer = pointer - n + 2;
|
||||
}
|
||||
}
|
||||
if (pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL) {
|
||||
// Merge this equality with the previous one.
|
||||
diffs[pointer - 1][1] += diffs[pointer][1];
|
||||
diffs.splice(pointer, 1);
|
||||
} else {
|
||||
pointer++;
|
||||
}
|
||||
count_insert = 0;
|
||||
count_delete = 0;
|
||||
text_delete = '';
|
||||
text_insert = '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
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
|
||||
// which can be shifted sideways to eliminate an equality.
|
||||
// e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
|
||||
var changes = false;
|
||||
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.
|
||||
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][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]) {
|
||||
// Shift the edit over the next equality.
|
||||
diffs[pointer - 1][1] += diffs[pointer + 1][1];
|
||||
diffs[pointer][1] =
|
||||
diffs[pointer][1].substring(diffs[pointer + 1][1].length) +
|
||||
diffs[pointer + 1][1];
|
||||
diffs.splice(pointer + 1, 1);
|
||||
changes = true;
|
||||
}
|
||||
}
|
||||
pointer++;
|
||||
}
|
||||
// If shifts were made, the diff needs reordering and another shift sweep.
|
||||
if (changes) {
|
||||
diff_cleanupMerge(diffs, fix_unicode);
|
||||
}
|
||||
};
|
||||
|
||||
function is_surrogate_pair_start(charCode) {
|
||||
return charCode >= 0xD800 && charCode <= 0xDBFF;
|
||||
}
|
||||
|
||||
function is_surrogate_pair_end(charCode) {
|
||||
return charCode >= 0xDC00 && charCode <= 0xDFFF;
|
||||
}
|
||||
|
||||
function starts_with_pair_end(str) {
|
||||
return is_surrogate_pair_end(str.charCodeAt(0));
|
||||
}
|
||||
|
||||
function ends_with_pair_start(str) {
|
||||
return is_surrogate_pair_start(str.charCodeAt(str.length - 1));
|
||||
}
|
||||
|
||||
function remove_empty_tuples(tuples) {
|
||||
var ret = [];
|
||||
for (var i = 0; i < tuples.length; i++) {
|
||||
if (tuples[i][1].length > 0) {
|
||||
ret.push(tuples[i]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
function make_edit_splice(before, oldMiddle, newMiddle, after) {
|
||||
if (ends_with_pair_start(before) || starts_with_pair_end(after)) {
|
||||
return null;
|
||||
}
|
||||
return remove_empty_tuples([
|
||||
[DIFF_EQUAL, before],
|
||||
[DIFF_DELETE, oldMiddle],
|
||||
[DIFF_INSERT, newMiddle],
|
||||
[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;
|
||||
// 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
|
||||
// result of selecting two of the x's and typing "x".
|
||||
var oldLength = oldText.length;
|
||||
var newLength = newText.length;
|
||||
if (oldRange.length === 0 && (newRange === null || newRange.length === 0)) {
|
||||
// see if we have an insert or delete before or after cursor
|
||||
var oldCursor = oldRange.index;
|
||||
var oldBefore = oldText.slice(0, oldCursor);
|
||||
var oldAfter = oldText.slice(oldCursor);
|
||||
var maybeNewCursor = newRange ? newRange.index : null;
|
||||
editBefore: {
|
||||
// is this an insert or delete right before oldCursor?
|
||||
var newCursor = oldCursor + newLength - oldLength;
|
||||
if (maybeNewCursor !== null && maybeNewCursor !== newCursor) {
|
||||
break editBefore;
|
||||
}
|
||||
if (newCursor < 0 || newCursor > newLength) {
|
||||
break editBefore;
|
||||
}
|
||||
var newBefore = newText.slice(0, newCursor);
|
||||
var newAfter = newText.slice(newCursor);
|
||||
if (newAfter !== oldAfter) {
|
||||
break editBefore;
|
||||
}
|
||||
var prefixLength = Math.min(oldCursor, newCursor);
|
||||
var oldPrefix = oldBefore.slice(0, prefixLength);
|
||||
var newPrefix = newBefore.slice(0, prefixLength);
|
||||
if (oldPrefix !== newPrefix) {
|
||||
break editBefore;
|
||||
}
|
||||
var oldMiddle = oldBefore.slice(prefixLength);
|
||||
var newMiddle = newBefore.slice(prefixLength);
|
||||
return make_edit_splice(oldPrefix, oldMiddle, newMiddle, oldAfter);
|
||||
}
|
||||
editAfter: {
|
||||
// is this an insert or delete right after oldCursor?
|
||||
if (maybeNewCursor !== null && maybeNewCursor !== oldCursor) {
|
||||
break editAfter;
|
||||
}
|
||||
var cursor = oldCursor;
|
||||
var newBefore = newText.slice(0, cursor);
|
||||
var newAfter = newText.slice(cursor);
|
||||
if (newBefore !== oldBefore) {
|
||||
break editAfter;
|
||||
}
|
||||
var suffixLength = Math.min(oldLength - cursor, newLength - cursor);
|
||||
var oldSuffix = oldAfter.slice(oldAfter.length - suffixLength);
|
||||
var newSuffix = newAfter.slice(newAfter.length - suffixLength);
|
||||
if (oldSuffix !== newSuffix) {
|
||||
break editAfter;
|
||||
}
|
||||
var oldMiddle = oldAfter.slice(0, oldAfter.length - suffixLength);
|
||||
var newMiddle = newAfter.slice(0, newAfter.length - suffixLength);
|
||||
return make_edit_splice(oldBefore, oldMiddle, newMiddle, oldSuffix);
|
||||
}
|
||||
}
|
||||
if (oldRange.length > 0 && newRange && newRange.length === 0) {
|
||||
replaceRange: {
|
||||
// see if diff could be a splice of the old selection range
|
||||
var oldPrefix = oldText.slice(0, oldRange.index);
|
||||
var oldSuffix = oldText.slice(oldRange.index + oldRange.length);
|
||||
var prefixLength = oldPrefix.length;
|
||||
var suffixLength = oldSuffix.length;
|
||||
if (newLength < prefixLength + suffixLength) {
|
||||
break replaceRange;
|
||||
}
|
||||
var newPrefix = newText.slice(0, prefixLength);
|
||||
var newSuffix = newText.slice(newLength - suffixLength);
|
||||
if (oldPrefix !== newPrefix || oldSuffix !== newSuffix) {
|
||||
break replaceRange;
|
||||
}
|
||||
var oldMiddle = oldText.slice(prefixLength, oldLength - suffixLength);
|
||||
var newMiddle = newText.slice(prefixLength, newLength - suffixLength);
|
||||
return make_edit_splice(oldPrefix, oldMiddle, newMiddle, oldSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function diff(text1, text2, cursor_pos) {
|
||||
// only pass fix_unicode=true at the top level, not when diff_main is
|
||||
// recursively invoked
|
||||
return diff_main(text1, text2, cursor_pos, true);
|
||||
}
|
||||
|
||||
diff.INSERT = DIFF_INSERT;
|
||||
diff.DELETE = DIFF_DELETE;
|
||||
diff.EQUAL = DIFF_EQUAL;
|
||||
|
||||
module.exports = diff;
|
55
node_modules/fast-diff/package.json
generated
vendored
Normal file
55
node_modules/fast-diff/package.json
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
{
|
||||
"_from": "fast-diff@^1.2.0",
|
||||
"_id": "fast-diff@1.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==",
|
||||
"_location": "/fast-diff",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "fast-diff@^1.2.0",
|
||||
"name": "fast-diff",
|
||||
"escapedName": "fast-diff",
|
||||
"rawSpec": "^1.2.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.2.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/npm-check-updates"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz",
|
||||
"_shasum": "73ee11982d86caaf7959828d519cfe927fac5f03",
|
||||
"_spec": "fast-diff@^1.2.0",
|
||||
"_where": "/home/shimataro/projects/actions/ssh-key-action/node_modules/npm-check-updates",
|
||||
"author": {
|
||||
"name": "Jason Chen",
|
||||
"email": "jhchen7@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/jhchen/fast-diff/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "Fast Javascript text diff",
|
||||
"devDependencies": {
|
||||
"lodash": "~3.9.3",
|
||||
"seedrandom": "~2.4.0"
|
||||
},
|
||||
"homepage": "https://github.com/jhchen/fast-diff#readme",
|
||||
"keywords": [
|
||||
"diff"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"main": "diff.js",
|
||||
"name": "fast-diff",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/jhchen/fast-diff.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node test.js"
|
||||
},
|
||||
"types": "diff.d.ts",
|
||||
"version": "1.2.0"
|
||||
}
|
334
node_modules/fast-diff/test.js
generated
vendored
Normal file
334
node_modules/fast-diff/test.js
generated
vendored
Normal file
|
@ -0,0 +1,334 @@
|
|||
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!");
|
Loading…
Add table
Add a link
Reference in a new issue