mirror of
https://github.com/yjs/yjs.git
synced 2025-12-17 04:07:46 +01:00
20 lines
598 B
JavaScript
20 lines
598 B
JavaScript
|
|
|
||
|
|
export default function simpleDiff (a, b) {
|
||
|
|
let left = 0 // number of same characters counting from left
|
||
|
|
let right = 0 // number of same characters counting from right
|
||
|
|
while (left < a.length && left < b.length && a[left] === b[left]) {
|
||
|
|
left++
|
||
|
|
}
|
||
|
|
if (left !== a.length || left !== b.length) {
|
||
|
|
// Only check right if a !== b
|
||
|
|
while (right + left < a.length && right + left < b.length && a[a.length - right - 1] === b[b.length - right - 1]) {
|
||
|
|
right++
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
pos: left,
|
||
|
|
remove: a.length - left - right,
|
||
|
|
insert: b.slice(left, b.length - right)
|
||
|
|
}
|
||
|
|
}
|