From 1023590bd4b29edb8c4d280f9bd50966fc22e469 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 30 Oct 2017 15:20:16 -0400 Subject: [PATCH] Omit redundant slice in join method of diffArrays --- src/diff/array.js | 4 ++-- test/diff/array.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/diff/array.js b/src/diff/array.js index 28de3a0c..99de08fc 100644 --- a/src/diff/array.js +++ b/src/diff/array.js @@ -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; }; diff --git a/test/diff/array.js b/test/diff/array.js index df15e82c..33dcf61a 100644 --- a/test/diff/array.js +++ b/test/diff/array.js @@ -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) {