Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2016 08:50
Show Gist options
  • Save anonymous/d6f802a5a8183e828b1c09835a4a8e3d to your computer and use it in GitHub Desktop.
Save anonymous/d6f802a5a8183e828b1c09835a4a8e3d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/nayedi
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function findDiff(original, newText) {
function getZip(originalList, newTextList){
return originalList.map(function(char,i) {
return {
first: char,
second: newTextList[i]
}
})
}
function findDiffPoint(zippedList) {
var start = 0
zippedList.forEach(function(pair, index) {
if (start > 0) return
if (pair.first != pair.second) {
start = index
}
})
return start
}
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
)
</script>
<script id="jsbin-source-javascript" type="text/javascript">function findDiff(original, newText) {
function getZip(originalList, newTextList){
return originalList.map(function(char,i) {
return {
first: char,
second: newTextList[i]
}
})
}
function findDiffPoint(zippedList) {
var start = 0
zippedList.forEach(function(pair, index) {
if (start > 0) return
if (pair.first != pair.second) {
start = index
}
})
return start
}
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
)</script></body>
</html>
function findDiff(original, newText) {
function getZip(originalList, newTextList){
return originalList.map(function(char,i) {
return {
first: char,
second: newTextList[i]
}
})
}
function findDiffPoint(zippedList) {
var start = 0
zippedList.forEach(function(pair, index) {
if (start > 0) return
if (pair.first != pair.second) {
start = index
}
})
return start
}
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