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

Add missing hash call in diff calculation #49

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function lcsToJsonPatch(a1, a2, path, state, lcsMatrix) {
* @returns {object} updated diff state
*/
function appendValueChanges(a, b, path, state) {
if(a !== b) {
if(state.hash(a) !== state.hash(b)) {
if(state.invertible) {
state.patch.push({ op: 'test', path: path, value: a });
}
Expand Down
54 changes: 54 additions & 0 deletions test/jiff-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,60 @@ buster.testCase('jiff', {
}
}
}
},
'with custom hash function': {
'primitives as elements': {
'should generate an empty patch when elements are equal': function() {
var patch = jiff.diff([1, 'a', true], [1, 'a', true], {
hash: function (a) {
return JSON.stringify(a) + '_SUFFIX';
}
});
assert.equals(patch.length, 0);
}
},
'arrays as elements': {
'should generate an empty patch when elements are equal': function() {
var patch = jiff.diff([['a'], ['b'], ['c']], [['a'], ['b'], ['c']], {
hash: function (a) {
return JSON.stringify(a) + '_SUFFIX';
}
});
assert.equals(patch.length, 0);
}
},
'objects as elements': {
'object keys with consistent order': {
'should generate an empty patch when elements are equal': function() {
var patch = jiff.diff([{a: 'a', b: 'b', c: 'c'}], [{a: 'a', b: 'b', c: 'c'}], {
hash: function (a) {
return JSON.stringify(a) + '_SUFFIX';
}
});
assert.equals(patch.length, 0);
}
},
'object keys with inconsistent order': {
'should generate a non empty patch even though elements are equal': function() {
var patch = jiff.diff([{b: 'b', c: 'c', a: 'a'}], [{a: 'a', b: 'b', c: 'c'}], {
hash: function (a) {
return JSON.stringify(a) + '_SUFFIX';
}
});
refute.equals(patch.length, 0);
}
}
},
'when custom hash ignores value': function() {
var patch = jiff.diff([1], [2], {
invertible: false,
hash: function (a) {
return typeof a;
}
});

assert.equals(patch.length, 0);
}
}
},

Expand Down