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

Added variant of WordDiff that doesn't ignore whitespace differences #22

Merged
merged 1 commit into from
Sep 12, 2013

Conversation

papandreou
Copy link
Contributor

Right now mocha uses the DiffWords algorithm to back its string diffs feature: http://tjholowaychuk.com/post/18574009869/mocha-string-diffs

However, the fact that DiffWords ignores whitespace differences makes it extremely hard to debug problems with differences in eg. line endings:

require('diff').diffWords('foo\nbar', 'foo\r\nbar'); 
[ { value: 'foo\r\nbar',
    added: undefined,
    removed: undefined } ]

... because that makes the test framework render the text without any added/removed text highlighted.

DiffChar doesn't suffer from this problem, but for long texts it's very hard to make sense of a char-based diff, so there's a use case for a mode that diffs word-by-word, but doesn't ignore whitespace differences. Turns out that was extremely easy to achieve, so this PR gives you:

> require('diff').diffWordsWithSpace('foo\nbar', 'foo\r\nbar');
[ { value: 'foo',
    added: undefined,
    removed: undefined },
  { value: '\r\n',
    added: true,
    removed: undefined },
  { value: '\n',
    added: undefined,
    removed: true },
  { value: 'bar',
    added: undefined,
    removed: undefined } ]

Seems like vows has the same issue: vowsjs/vows#281

Compares word by word, but doesn't ignore whitespace differences in between.
@kpdecker
Copy link
Owner

Looks great! Thank you.

kpdecker added a commit that referenced this pull request Sep 12, 2013
Added variant of WordDiff that doesn't ignore whitespace differences
@kpdecker kpdecker merged commit e871cad into kpdecker:master Sep 12, 2013
@kpdecker
Copy link
Owner

Released in 1.0.7

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 this pull request may close these issues.

2 participants