Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected diffLines result on a basic example #324

Open
dperetti opened this issue May 30, 2021 · 3 comments
Open

Unexpected diffLines result on a basic example #324

dperetti opened this issue May 30, 2021 · 3 comments

Comments

@dperetti
Copy link

import * as Diff from 'diff'
const diff = Diff.diffLines('a\nb\nc', 'a\nb', { newlineIsToken: false })
console.log(diff)

yields:

[ { count: 1, value: 'a\n' },
  { count: 2, added: undefined, removed: true, value: 'b\nc' },
  { count: 1, added: true, removed: undefined, value: 'b' } ]
[ { count: 1, value: 'a\n' },
  { count: 2, added: undefined, removed: true, value: 'b\nc' },
  { count: 1, added: true, removed: undefined, value: 'b' } ]

I need to add a trailing \n to get the expected, simpler result.

import * as Diff from 'diff'
const diff = Diff.diffLines('a\nb\nc\n', 'a\nb\n', { newlineIsToken: false })
console.log(diff)
[ { count: 2, value: 'a\nb\n' },
  { count: 1, added: undefined, removed: true, value: 'c\n' } ]
@ExplodingCabbage
Copy link
Collaborator

Interesting. #254 was about the same behaviour in the context of jsdiff's patch-generation functions, and I closed it saying the behaviour is correct. I think I agree with myself with regards to patches. When it comes to change objects as returned from diffLines, though, I'm less certain, but still inclined towards the same answer - that this is how things should work. (Though I should probably note it in the docs.)

How do you reckon things ought to behave, here? It would be weird if your first example returned literally the exact same result as your second one since then it'd be adding in an extra \n character after the c that wasn't there in the original text.

@dperetti
Copy link
Author

The line with a "b" does not change : the newline character is a separator and should be treated at such when using diffLines.
So that second line should not be marked as being removed.
A computer doesn't care so it's not an issue if you generate patches.
But diffLines is probably targeted at humans, and as a human I don't like this 🙂.

@ExplodingCabbage
Copy link
Collaborator

Thinking about it, I figure it'd make sense to add an ignoreNewlineAtEof option for this. But I don't want to add that until #219 is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants