Skip to content

Commit

Permalink
sanity check for longer characters
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Mar 17, 2023
1 parent 629c542 commit 0398712
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export const simpleDiffString = (a, b) => {
left++
}
// If the last same character is a high surrogate, we need to rollback to the previous character
if (highSurrogateRegex.test(a[left - 1])) left--
if (left > 0 && highSurrogateRegex.test(a[left - 1])) left--
while (right + left < a.length && right + left < b.length && a[a.length - right - 1] === b[b.length - right - 1]) {
right++
}
// If the last same character is a low surrogate, we need to rollback to the previous character
if (lowSurrogateRegex.test(a[a.length - right])) right--
if (right > 0 && lowSurrogateRegex.test(a[a.length - right])) right--
return {
index: left,
remove: a.length - left - right,
Expand Down
1 change: 1 addition & 0 deletions diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const testDiffing = tc => {
runDiffTest('\u{d83d}\u{dea7}\u{d83d}\u{dc77}'/* 'πŸš§πŸ‘·' */, '\u{d83d}\u{dc77}'/* 'πŸ‘·' */, { index: 0, remove: 2, insert: '' })
// These strings share low-surrogate characters
runDiffTest('\u{d83d}\u{dfe6}\u{d83d}\u{dfe6}'/* '🟦🟦' */, '\u{d83c}\u{dfe6}\u{d83d}\u{dfe6}'/* '🏦🟦' */, { index: 0, remove: 2, insert: '🏦' })
runDiffTest('πŸ‡¦πŸ‡¨', 'πŸ‡¦πŸ‡©', { index: 2, remove: 2, insert: 'πŸ‡©' })
}

/**
Expand Down

0 comments on commit 0398712

Please sign in to comment.