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

Tokenize Regex as Parameter #268

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ npm install diff --save
* `newStr` : New string value
* `oldHeader` : Additional information to include in the old file header
* `newHeader` : Additional information to include in the new file header
* `options` : An object with options. Currently, only `context` is supported and describes how many lines of context should be included.
* `options` : An object with options.
* `context` : describes how many lines of context should be included.
* `tokenizer` : Overrides the default regex used to split text into words. supported by `diffWords` and `diffWordsWithSpace`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the wrong place; you've documented it as a parameter of createTwoFilesPatch instead of diffWords.


* `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.

Expand Down
3 changes: 2 additions & 1 deletion src/diff/word.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ wordDiff.equals = function(left, right) {
return left === right || (this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right));
};
wordDiff.tokenize = function(value) {
let tokens = value.split(/(\s+|[()[\]{}'"]|\b)/);
const tokenizer = this.options.tokenizer || /(\s+|[()[\]{}'"]|\b)/; // Use the tokenizer regex in the options or use the default regex
const tokens = value.split(tokenizer); // Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set.

// Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set.
for (let i = 0; i < tokens.length - 1; i++) {
Expand Down
23 changes: 23 additions & 0 deletions test/diff/word.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ describe('WordDiff', function() {
done();
});
});

// With custom tokenizer
it('should utilize a custom tokenizer', function() {

const diff = diffWords('foo_bar', 'something_bar', {
tokenizer: /(\s+|[()[\]{}_'"]|\b)/
});

expect(diff).to.eql([{
count: 1,
added: undefined,
removed: true,
value: 'foo'
}, {
count: 1,
added: true,
removed: undefined,
value: 'something'
}, {
count: 2,
value: '_bar'
}]);
});
});

describe('#diffWordsWithSpace', function() {
Expand Down