-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): implement object diff detector (#55)
- Loading branch information
1 parent
de1a013
commit 6704d62
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var assert = require('assert') | ||
|
||
function difference(a, b) { | ||
var diff = {} | ||
|
||
Object.keys(a).forEach(compare(diff, a, b)) | ||
Object.keys(b).forEach(compare(diff, a, b)) | ||
|
||
return diff | ||
} | ||
|
||
function compare(diff, a, b) { | ||
return function curried(n) { | ||
if (!diff[n]) { | ||
try { | ||
assert.deepEqual(a[n], b[n]) | ||
} catch (e) { | ||
diff[n] = { | ||
config1: a[n], | ||
config2: b[n], | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = difference |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var assert = require('assert') | ||
var difference = require('../../src/lib/object-diff') | ||
|
||
describe('object difference', function() { | ||
it('should return difference', function() { | ||
assert.deepEqual( | ||
difference({'foo-rule': [2, 'foo']}, {'foo-rule': [2, 'bar']}), | ||
{'foo-rule': {config1: [2, 'foo'], config2: [2, 'bar']}} | ||
) | ||
assert.deepEqual( | ||
difference({'foo-rule': [2, 'foo', 'bar']}, {'foo-rule': 2}), | ||
{'foo-rule': {config1: [2, 'foo', 'bar'], config2: 2}} | ||
) | ||
assert.deepEqual( | ||
difference({'foo-rule': [0, 'foo']}, {'bar-rule': [1, 'bar']}), | ||
{ | ||
'foo-rule': {config1: [0, 'foo'], config2: undefined}, | ||
'bar-rule': {config1: undefined, config2: [1, 'bar']}, | ||
} | ||
) | ||
|
||
assert.deepEqual( | ||
difference({'foo-rule': [1, 'foo', 'bar']}, {'foo-rule': [1, 'foo', 'bar']}), | ||
{} | ||
) | ||
}) | ||
}) |