Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Forked from anonymous/index.html
Last active June 21, 2016 18:08
Show Gist options
  • Save divanvisagie/68220d4fb7467a22c098472edb1e2263 to your computer and use it in GitHub Desktop.
Save divanvisagie/68220d4fb7467a22c098472edb1e2263 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/nayedi
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
)
[object Object] {
  added: "<b> INSERT </b>",
  end: 27,
  start: 12
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment