[object Object] {
added: "<b> INSERT </b>",
end: 27,
start: 12
}
-
-
Save divanvisagie/68220d4fb7467a22c098472edb1e2263 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/nayedi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findDiff(original, newText) { | |
function getZip(originalList, newTextList){ | |
return originalList.map(function(char,i) { | |
return { | |
first: char, | |
second: newTextList[i] | |
} | |
}) | |
} | |
function findDiffPoint(zippedList) { | |
return zippedList.map(function(pair) { | |
return (pair.first == pair.second) | |
}).indexOf(false) | |
} | |
var zip = getZip( | |
original.split(''), | |
newText.split('') | |
) | |
var diffStart = findDiffPoint(zip) | |
var reverseZip = getZip( | |
original.split('').reverse(), | |
newText.split('').reverse() | |
) | |
var diffEnd = newText.length - findDiffPoint(reverseZip) | |
var added = newText.slice(diffStart,diffEnd) | |
return { | |
start: diffStart, | |
end: diffEnd, | |
added: added | |
} | |
} | |
var original = "first para\n Second para" | |
var newText = "first para\n <b> INSERT </b> Second para" | |
diff = findDiff(original,newText) | |
console.log( | |
diff | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment