Skip to content

Commit

Permalink
Omit redundant slice in join method of diffArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and kpdecker committed Mar 5, 2018
1 parent c72ef4a commit 1023590
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/diff/array.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Diff from './base';

export const arrayDiff = new Diff();
arrayDiff.tokenize = arrayDiff.join = function(value) {
arrayDiff.tokenize = function(value) {
return value.slice();
};
arrayDiff.removeEmpty = function(value) {
arrayDiff.join = arrayDiff.removeEmpty = function(value) {
return value;
};

Expand Down
28 changes: 28 additions & 0 deletions test/diff/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,35 @@ describe('diff/array', function() {
{count: 1, value: [c], removed: true, added: undefined}
]);
});
describe('anti-aliasing', function() {
// Test apparent contract that no chunk value is ever an input argument.
const value = [0, 1, 2];
const expected = [
{count: value.length, value: value}
];

const input = value.slice();
const diffResult = diffArrays(input, input);
it('returns correct deep result for identical inputs', function() {
expect(diffResult).to.deep.equals(expected);
});
it('does not return the input array', function() {
expect(diffResult[0].value).to.not.equal(input);
});

const input1 = value.slice();
const input2 = value.slice();
const diffResult2 = diffArrays(input1, input2);
it('returns correct deep result for equivalent inputs', function() {
expect(diffResult2).to.deep.equals(expected);
});
it('does not return the first input array', function() {
expect(diffResult2[0].value).to.not.equal(input1);
});
it('does not return the second input array', function() {
expect(diffResult2[0].value).to.not.equal(input2);
});
});
it('Should diff arrays with comparator', function() {
const a = {a: 0}, b = {a: 1}, c = {a: 2}, d = {a: 3};
function comparator(left, right) {
Expand Down

0 comments on commit 1023590

Please sign in to comment.