Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
3652181
Fix assorted trivial capitalisation typos (#410)
ExplodingCabbage Dec 14, 2023
ff14775
Upgrade packages that Dependabot has open PRs about (#415)
ExplodingCabbage Dec 15, 2023
8ca7068
Bump karma from 5.1.1 to 6.3.16 (#357)
dependabot[bot] Dec 15, 2023
0e7a0cb
yarn eslint . --fix (#421)
ExplodingCabbage Dec 15, 2023
bbb2359
Update package.json version to 5.1.0 (#422)
ExplodingCabbage Dec 15, 2023
902e7da
Bump more dependencies to please Dependabot (#425)
ExplodingCabbage Dec 15, 2023
e67c2a0
Bump qs from 6.7.0 to 6.11.0 (#426)
dependabot[bot] Dec 15, 2023
a3759a1
Move demo link to the top of the README (#370)
waldyrious Dec 15, 2023
021c973
chore: update license file (#331)
eddiemonge Dec 16, 2023
87dedb6
readme: add links to section: change objects (#316)
milahu Dec 18, 2023
e0e960a
Document diffJson() options (#332)
cincodenada Dec 18, 2023
976d60d
Fix `exports` field in `package.json` (#351)
fisker Dec 18, 2023
1387be9
Remove index.html from master (#429)
ExplodingCabbage Dec 19, 2023
96f5998
Update comment in index.js to reflect JsDiff->Diff rename in 5.0.0 (#…
ExplodingCabbage Dec 19, 2023
b4d7491
Document in a comment in web_example.html that you need to run a buil…
ExplodingCabbage Dec 19, 2023
f2596ea
Fix a typo (#433)
ExplodingCabbage Dec 20, 2023
6183041
Default value of line delimiters when a patch is applied (#228) (#393)
Cinedin Dec 20, 2023
2e08d01
Fix bug that leads to worse time complexity and cripplingly slow perf…
ExplodingCabbage Dec 23, 2023
5897c8f
Update CONTRIBUTING.md to use yarn (#441)
ExplodingCabbage Dec 23, 2023
a19bded
Fix typo / grammar error in CONTRIBUTING.md (#442)
ExplodingCabbage Dec 23, 2023
14bfcb6
Update release-notes.md with content on npm that never got pushed to …
ExplodingCabbage Dec 23, 2023
8e51326
Write release notes for PRs already merged to master (#444)
ExplodingCabbage Dec 23, 2023
573b7af
Option to strip trailing CR (#344)
oBusk Dec 27, 2023
e35c347
Add release notes for @oBusk's PR #344 (#445)
ExplodingCabbage Dec 27, 2023
a4eac49
Stop treating stuff like vertical tabs as line breaks when dealing wi…
ExplodingCabbage Dec 27, 2023
a2dc5ec
Add test showing patch from bug #177 is handled correctly now (#447)
ExplodingCabbage Dec 27, 2023
bf5ec4a
Flip core algorithm so everything is no longer the mirror image of My…
ExplodingCabbage Dec 27, 2023
97c676d
Merge remote-tracking branch 'origin/master' into 6.0.0-staging
ExplodingCabbage Dec 27, 2023
fe261ae
Prefer to order deletions before insertions when the edit cost is the…
ExplodingCabbage Dec 27, 2023
b1b2035
Speed up algorithm by not considering diagonals that take us off the …
ExplodingCabbage Dec 29, 2023
8bd13d6
Consistently capitalize "jsdiff" in all-lowercase in docs (#449)
ExplodingCabbage Dec 29, 2023
56c6a8a
Expose `formatPatch` on `diff` object and document (#451)
ExplodingCabbage Dec 29, 2023
6a574cc
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Dec 29, 2023
8365367
Add function to reverse a patch (#450)
ExplodingCabbage Jan 2, 2024
3351c82
Merge remote-tracking branch 'origin/master' into 6.0.0-staging
ExplodingCabbage Jan 2, 2024
3a99253
Always set `added` and `removed` to `true` or `false`, rather than le…
ExplodingCabbage Jan 2, 2024
a98b974
Flesh out the README a bit and fix some errors and omissions (#458)
ExplodingCabbage Jan 4, 2024
c8c5132
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Jan 4, 2024
c6498e3
Document that applyPatch can return false (#459)
ExplodingCabbage Jan 4, 2024
7eacf2a
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Jan 5, 2024
b3aab68
Handle case where the user explicitly passes `maxEditLength: 0` the w…
ExplodingCabbage Jan 8, 2024
ea983ba
Fix more gaps in the docs (#466)
ExplodingCabbage Jan 8, 2024
12e092d
Merge remote-tracking branch 'origin/master' into 6.0.0-staging
ExplodingCabbage Jan 8, 2024
e6c45b0
Add a oneChangePerToken option (#460)
ExplodingCabbage Jan 8, 2024
1e79116
Fix order of arguments to .equals and comparator (#467)
ExplodingCabbage Jan 8, 2024
25a14af
Migrate to DABH's fork of colors (#469)
ExplodingCabbage Jan 8, 2024
ca8718c
Bump follow-redirects from 1.14.8 to 1.15.4 (#470)
dependabot[bot] Jan 9, 2024
1c7514c
Fix mistake in README (#471)
ExplodingCabbage Jan 10, 2024
707fccc
Add note to README about setting `context` to Infinity or MAX_SAFE_IN…
ExplodingCabbage Jan 10, 2024
1f1ec96
Replace broken link to Myers's paper in the README with a working one…
ExplodingCabbage Jan 11, 2024
533893d
Add `timeout` option (#478)
ExplodingCabbage Jan 26, 2024
b5d1cfa
Modify node_example.js to support showing added/deleted spaces (#479)
ExplodingCabbage Jan 26, 2024
06a669b
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Jan 29, 2024
4abb5f3
Support max edit length in patch creation functions (#480)
ExplodingCabbage Jan 29, 2024
dfc6fe4
Add examples to docs of creating and applying patches (importantly in…
ExplodingCabbage Jan 29, 2024
a2f726a
Add myself to the list of maintainers (#482)
ExplodingCabbage Feb 12, 2024
370a9df
5.2.0 release (#483)
ExplodingCabbage Feb 12, 2024
ad635b1
Add a reminder to the releasing docs to update the gh-pages site afte…
ExplodingCabbage Feb 13, 2024
b9f56d3
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Feb 13, 2024
fc2e36d
Merge pull request #446 from kpdecker/6.0.0-staging
ExplodingCabbage Feb 13, 2024
5f9cd41
Sort out behaviour of newlineIsToken and ignoreWhitespace (#486)
ExplodingCabbage Feb 15, 2024
e83674b
Remove failing test (#487)
ExplodingCabbage Feb 15, 2024
a3e4812
Add some more exhaustive tests based on @Mingun's work (#488)
ExplodingCabbage Feb 15, 2024
13d9749
Fix the weird function signature of async callbacks (#490)
ExplodingCabbage Feb 15, 2024
bf45b03
Fix race conditions involving this.options being overwritten during e…
ExplodingCabbage Feb 15, 2024
a73b771
Add further assertion to test, as suggested by Mingun (#491)
ExplodingCabbage Feb 16, 2024
f38e47d
Support `Object.create(null)` in JSON diffing (#493)
danbeam Feb 19, 2024
3da78c2
Simplify tokenization logic in diffWords (#494)
ExplodingCabbage Feb 19, 2024
7a73dc1
Bump ip from 1.1.5 to 1.1.9 (#495)
dependabot[bot] Feb 21, 2024
f925d4c
Fix trivial typo ("threat"->"treat" in test name) (#498)
ExplodingCabbage Mar 5, 2024
045c346
Add test of how diffWordsWithSpace handles Windows-style newlines (#499)
ExplodingCabbage Mar 6, 2024
490f5ab
Make diffChars diff Unicode code points instead of UTF-16 code units …
ExplodingCabbage Mar 8, 2024
59161e0
Fix diffWords handling of whitespace (#497)
ExplodingCabbage Mar 11, 2024
f4f11df
Bump follow-redirects from 1.15.4 to 1.15.6 (#502)
dependabot[bot] Mar 18, 2024
c9bc8e3
Run: (#503)
ExplodingCabbage Mar 19, 2024
16a060e
Purge inactive/broken Travis and Sauce Labs integrations (#504)
ExplodingCabbage Mar 19, 2024
84b5c9e
Upgrade some more dev dependencies (#505)
ExplodingCabbage Mar 20, 2024
b90a3eb
Bump Mocha one major version (#507)
ExplodingCabbage Mar 20, 2024
64f587c
Always enable "strict mode" in parsePatch (#508)
ExplodingCabbage Mar 20, 2024
53339e2
Remove unused Grunt `version` task; flesh out docs on how to do a rel…
ExplodingCabbage Mar 20, 2024
eb73eb8
Remove style.css from master branch (it's part of the gh-pages site, …
ExplodingCabbage Mar 20, 2024
046b5d3
Bump webpack-dev-middleware from 7.0.0 to 7.1.1 (#511)
dependabot[bot] Mar 22, 2024
4ebc4bf
Bump express from 4.18.3 to 4.19.2 (#512)
dependabot[bot] Apr 28, 2024
c8a9cc5
Remove linedelimiters, improve handling of Windows vs Unix line endin…
ExplodingCabbage Jun 7, 2024
9bb34dc
Support `callback` in patch functions, not just diffFoo functions (#521)
ExplodingCabbage Jun 7, 2024
0126325
Add tests of existing broken parsePatch behaviour, to fix before next…
ExplodingCabbage Jun 14, 2024
5ecab06
Bump braces from 3.0.2 to 3.0.3 (#526)
dependabot[bot] Jun 18, 2024
323e8bb
Fix parse patch bug (#529)
ExplodingCabbage Jun 24, 2024
896c982
Fix release notes typo
ExplodingCabbage Jun 24, 2024
0e7c20c
Add ignoreNewlineAtEof (#530)
ExplodingCabbage Jun 24, 2024
353d117
Rewrite applyPatch (#533)
ExplodingCabbage Jul 26, 2024
939bb45
Fix handling of EOF in createPatch (#535)
ExplodingCabbage Jul 29, 2024
244df82
Fix more logic around newlines at EOF - this time stuff I recently br…
ExplodingCabbage Jul 29, 2024
4f0430a
Add Intl.Segmenter support (#539)
ExplodingCabbage Aug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a oneChangePerToken option (kpdecker#460)
* Add a oneChangePerToken option to emit one change object per token instead of combining consecutive tokens into one change object

* Document oneChangePerToken

* Add release notes

* Add test (which fails; needs changes from kpdecker#439)

* Add test of case with identical texts

* Add another test

* Move release notes to correct place now that I'm planning this for 6.0.0
  • Loading branch information
ExplodingCabbage authored Jan 8, 2024
commit e6c45b0f6511c93081892eaf18ae841573768aa1
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Certain options can be provided in the `options` object of *any* method that cal

(Note that if the ONLY option you want to provide is a callback, you can pass the callback function directly as the `options` parameter instead of passing an object with a `callback` property.)
* `maxEditLength`: a number specifying the maximum edit distance to consider between the old and new texts. If the edit distance is higher than this, jsdiff will return `undefined` instead of a diff. You can use this to limit the computational cost of diffing large, very different texts by giving up early if the cost will be huge. Works for functions that return change objects and also for `structuredPatch`, but not other patch-generation functions.
* `oneChangePerToken`: if `true`, the array of change objects returned will contain one change object per token (e.g. one per line if calling `diffLines`), instead of runs of consecutive tokens that are all added / all removed / all conserved being combined into a single change object.

### Defining custom diffing behaviors

Expand Down
1 change: 1 addition & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [#439](https://github.com/kpdecker/jsdiff/pull/439) Prefer diffs that order deletions before insertions. When faced with a choice between two diffs with an equal total edit distance, the Myers diff algorithm generally prefers one that does deletions before insertions rather than insertions before deletions. For instance, when diffing `abcd` against `acbd`, it will prefer a diff that says to delete the `b` and then insert a new `b` after the `c`, over a diff that says to insert a `c` before the `b` and then delete the existing `c`. JsDiff deviated from the published Myers algorithm in a way that led to it having the opposite preference in many cases, including that example. This is now fixed, meaning diffs output by JsDiff will more accurately reflect what the published Myers diff algorithm would output.
- [#455](https://github.com/kpdecker/jsdiff/pull/455) The `added` and `removed` properties of change objects are now guaranteed to be set to a boolean value. (Previously, they would be set to `undefined` or omitted entirely instead of setting them to false.)
- [#464](https://github.com/kpdecker/jsdiff/pull/464) Specifying `{maxEditLength: 0}` now sets a max edit length of 0 instead of no maximum.
- [#460][https://github.com/kpdecker/jsdiff/pull/460] Added `oneChangePerToken` option.

## Development

Expand Down
9 changes: 6 additions & 3 deletions src/diff/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Diff.prototype = {
let newPos = this.extractCommon(bestPath[0], newString, oldString, 0);
if (bestPath[0].oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
// Identity per the equality and tokenizer
return done([{value: this.join(newString), count: newString.length, added: false, removed: false}]);
return done(buildValues(self, bestPath[0].lastComponent, newString, oldString, self.useLongestToken));
}

// Once we hit the right edge of the edit graph on some diagonal k, we can
Expand Down Expand Up @@ -147,7 +147,7 @@ Diff.prototype = {

addToPath(path, added, removed, oldPosInc) {
let last = path.lastComponent;
if (last && last.added === added && last.removed === removed) {
if (last && !this.options.oneChangePerToken && last.added === added && last.removed === removed) {
return {
oldPos: path.oldPos + oldPosInc,
lastComponent: {count: last.count + 1, added: added, removed: removed, previousComponent: last.previousComponent }
Expand All @@ -170,9 +170,12 @@ Diff.prototype = {
newPos++;
oldPos++;
commonCount++;
if (this.options.oneChangePerToken) {
basePath.lastComponent = {count: 1, previousComponent: basePath.lastComponent, added: false, removed: false};
}
}

if (commonCount) {
if (commonCount && !this.options.oneChangePerToken) {
basePath.lastComponent = {count: commonCount, previousComponent: basePath.lastComponent, added: false, removed: false};
}

Expand Down
21 changes: 19 additions & 2 deletions test/diff/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@ import {expect} from 'chai';
describe('diff/character', function() {
describe('#diffChars', function() {
it('Should diff chars', function() {
const diffResult = diffChars('New Value.', 'New ValueMoreData.');
expect(convertChangesToXML(diffResult)).to.equal('New Value<ins>MoreData</ins>.');
const diffResult = diffChars('Old Value.', 'New ValueMoreData.');
expect(convertChangesToXML(diffResult)).to.equal('<del>Old</del><ins>New</ins> Value<ins>MoreData</ins>.');
});

describe('oneChangePerToken option', function() {
it('emits one change per character', function() {
const diffResult = diffChars('Old Value.', 'New ValueMoreData.', {oneChangePerToken: true});
expect(diffResult.length).to.equal(21);
expect(convertChangesToXML(diffResult)).to.equal('<del>O</del><del>l</del><del>d</del><ins>N</ins><ins>e</ins><ins>w</ins> Value<ins>M</ins><ins>o</ins><ins>r</ins><ins>e</ins><ins>D</ins><ins>a</ins><ins>t</ins><ins>a</ins>.');
});

it('correctly handles the case where the texts are identical', function() {
const diffResult = diffChars('foo bar baz qux', 'foo bar baz qux', {oneChangePerToken: true});
expect(diffResult).to.deep.equal(
['f', 'o', 'o', ' ', 'b', 'a', 'r', ' ', 'b', 'a', 'z', ' ', 'q', 'u', 'x'].map(
char => ({value: char, count: 1, added: false, removed: false})
)
);
});
});

describe('case insensitivity', function() {
Expand Down
19 changes: 19 additions & 0 deletions test/diff/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ describe('diff/line', function() {
});
});

describe('oneChangePerToken option', function() {
it('emits one change per line', function() {
const diffResult = diffLines(
'foo\nbar\nbaz\nqux\n',
'fox\nbar\nbaz\nqux\n',
{ oneChangePerToken: true }
);
expect(diffResult).to.deep.equal(
[
{value: 'foo\n', count: 1, added: false, removed: true},
{value: 'fox\n', count: 1, added: true, removed: false},
{value: 'bar\n', count: 1, added: false, removed: false},
{value: 'baz\n', count: 1, added: false, removed: false},
{value: 'qux\n', count: 1, added: false, removed: false}
]
);
});
});

// Trimmed Line Diff
describe('#TrimmedLineDiff', function() {
it('should diff lines', function() {
Expand Down