Skip to content

Commit e4ee3bd

Browse files
authored
Merge pull request #146 from wvanderdeijl/array
JsDiff.diffArrays to compare arrays
2 parents 5eb1d28 + d5003de commit e4ee3bd

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ or
5555

5656
Returns a list of change objects (See below).
5757

58+
* `JsDiff.diffArrays(oldArr, newArr[, options])` - diffs two arrays, comparing each item for strict equality (===).
59+
60+
Returns a list of change objects (See below).
61+
5862
* `JsDiff.createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.
5963

6064
Parameters:

src/diff/array.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Diff from './base';
2+
3+
export const arrayDiff = new Diff();
4+
arrayDiff.tokenize = arrayDiff.join = function(value) {
5+
return value.slice();
6+
};
7+
8+
export function diffArrays(oldArr, newArr, callback) { return arrayDiff.diff(oldArr, newArr, callback); }

src/diff/base.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Diff.prototype = {
3636
let oldPos = this.extractCommon(bestPath[0], newString, oldString, 0);
3737
if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
3838
// Identity per the equality and tokenizer
39-
return done([{value: newString.join(''), count: newString.length}]);
39+
return done([{value: this.join(newString), count: newString.length}]);
4040
}
4141

4242
// Main worker method. checks all permutations of a given edit length for acceptance.
@@ -160,6 +160,9 @@ Diff.prototype = {
160160
},
161161
tokenize(value) {
162162
return value.split('');
163+
},
164+
join(chars) {
165+
return chars.join('');
163166
}
164167
};
165168

@@ -179,9 +182,9 @@ function buildValues(diff, components, newString, oldString, useLongestToken) {
179182
return oldValue.length > value.length ? oldValue : value;
180183
});
181184

182-
component.value = value.join('');
185+
component.value = diff.join(value);
183186
} else {
184-
component.value = newString.slice(newPos, newPos + component.count).join('');
187+
component.value = diff.join(newString.slice(newPos, newPos + component.count));
185188
}
186189
newPos += component.count;
187190

@@ -190,7 +193,7 @@ function buildValues(diff, components, newString, oldString, useLongestToken) {
190193
oldPos += component.count;
191194
}
192195
} else {
193-
component.value = oldString.slice(oldPos, oldPos + component.count).join('');
196+
component.value = diff.join(oldString.slice(oldPos, oldPos + component.count));
194197
oldPos += component.count;
195198

196199
// Reverse add and remove so removes are output first to match common convention

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {diffSentences} from './diff/sentence';
2323
import {diffCss} from './diff/css';
2424
import {diffJson, canonicalize} from './diff/json';
2525

26+
import {diffArrays} from './diff/array';
27+
2628
import {applyPatch, applyPatches} from './patch/apply';
2729
import {parsePatch} from './patch/parse';
2830
import {structuredPatch, createTwoFilesPatch, createPatch} from './patch/create';
@@ -43,6 +45,8 @@ export {
4345
diffCss,
4446
diffJson,
4547

48+
diffArrays,
49+
4650
structuredPatch,
4751
createTwoFilesPatch,
4852
createPatch,

test/diff/array.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {diffArrays} from '../../lib/diff/array';
2+
3+
import {expect} from 'chai';
4+
5+
describe('diff/array', function() {
6+
describe('#diffArrays', function() {
7+
it('Should diff arrays', function() {
8+
const a = {a: 0}, b = {b: 1}, c = {c: 2};
9+
const diffResult = diffArrays([a, b, c], [a, c, b]);
10+
console.log(diffResult);
11+
expect(diffResult).to.deep.equals([
12+
{count: 1, value: [a]},
13+
{count: 1, value: [c], removed: undefined, added: true},
14+
{count: 1, value: [b]},
15+
{count: 1, value: [c], removed: true, added: undefined}
16+
]);
17+
});
18+
});
19+
});

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('root exports', function() {
1616
expect(Diff.diffCss).to.exist;
1717
expect(Diff.diffJson).to.exist;
1818

19+
expect(Diff.diffArrays).to.exist;
20+
1921
expect(Diff.structuredPatch).to.exist;
2022
expect(Diff.createTwoFilesPatch).to.exist;
2123
expect(Diff.createPatch).to.exist;

0 commit comments

Comments
 (0)