Skip to content

diffWordsWithSpace - a char-by-char diff for spaces? #180

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

Closed
rlidwka opened this issue May 31, 2017 · 0 comments · Fixed by #217
Closed

diffWordsWithSpace - a char-by-char diff for spaces? #180

rlidwka opened this issue May 31, 2017 · 0 comments · Fixed by #217

Comments

@rlidwka
Copy link

rlidwka commented May 31, 2017

require('diff').convertChangesToXML(
  require('diff').diffWordsWithSpace('foo\nbar', 'foo\n\n\nbar')
)

Returns:

'foo<del>\n</del><ins>\n\n\n</ins>bar'

Desired output is:

'foo<ins>\n\n</ins>bar'

As far as I understand, diffWordsWithSpace currently treats any amount of spaces as a single token. Which makes sense for diffWords (where space isn't significant), but doesn't work well for diffWordsWithSpace.

Suggestion: maybe it should compare spaces between words on character-by-character basis?

Possible solution - change regexp in tokenizer like this:

<   let tokens = value.split(/(\s+|\b)/);
---
>   let tokens = this.options.ignoreWhitespace ?
>                value.split(/(\s+|\b)/) :
>                value.split(/(\s|\b)/);

Seems to work fine for me, but I'm not quite sure it's correct.

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

Successfully merging a pull request may close this issue.

1 participant