unit test + fixes + comments + simplication for diffDeletion
This commit is contained in:
parent
cef67d5784
commit
595dc82543
2 changed files with 51 additions and 21 deletions
|
@ -25,16 +25,6 @@ function firstDiff(a, b) {
|
|||
return compareLen;
|
||||
}
|
||||
|
||||
function lastDiff(a, b) {
|
||||
const compareLen = Math.min(a.length, b.length);
|
||||
for (let i = 0; i < compareLen; ++i) {
|
||||
if (a[a.length - i] !== b[b.length - i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return compareLen;
|
||||
}
|
||||
|
||||
function diffStringsAtEnd(oldStr, newStr) {
|
||||
const len = Math.min(oldStr.length, newStr.length);
|
||||
const startInCommon = oldStr.substr(0, len) === newStr.substr(0, len);
|
||||
|
@ -52,22 +42,14 @@ function diffStringsAtEnd(oldStr, newStr) {
|
|||
}
|
||||
}
|
||||
|
||||
// assumes only characters have been deleted at one location in the string, and none added
|
||||
export function diffDeletion(oldStr, newStr) {
|
||||
if (oldStr === newStr) {
|
||||
return {};
|
||||
}
|
||||
const firstDiffIdx = firstDiff(oldStr, newStr);
|
||||
const lastDiffIdx = oldStr.length - lastDiff(oldStr, newStr) + 1;
|
||||
return {at: firstDiffIdx, removed: oldStr.substring(firstDiffIdx, lastDiffIdx)};
|
||||
}
|
||||
|
||||
export function diffInsertion(oldStr, newStr) {
|
||||
const diff = diffDeletion(newStr, oldStr);
|
||||
if (diff.removed) {
|
||||
return {at: diff.at, added: diff.removed};
|
||||
} else {
|
||||
return diff;
|
||||
}
|
||||
const amount = oldStr.length - newStr.length;
|
||||
return {at: firstDiffIdx, removed: oldStr.substr(firstDiffIdx, amount)};
|
||||
}
|
||||
|
||||
export function diffAtCaret(oldValue, newValue, caretPosition) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue