-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
canonicalize: Keep track of the replacement objects so the correct ob…
…ject can be substituted when a circular reference is detected.
- Loading branch information
1 parent
d786a63
commit d12c46d
Showing
2 changed files
with
52 additions
and
6 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
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,37 @@ | ||
const VERBOSE = false; | ||
|
||
var diff = require('../diff'); | ||
|
||
function getKeys(obj) { | ||
var keys = []; | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
keys.push(key); | ||
} | ||
} | ||
return keys; | ||
} | ||
|
||
describe('#canonicalize', function() { | ||
it('should put the keys in canonical order', function() { | ||
getKeys(diff.canonicalize({b: 456, a: 123})).should.eql(['a', 'b']); | ||
}); | ||
|
||
it('should dive into nested objects', function() { | ||
var canonicalObj = diff.canonicalize({b: 456, a: {d: 123, c: 456}}); | ||
getKeys(canonicalObj.a).should.eql(['c', 'd']); | ||
}); | ||
|
||
it('should dive into nested arrays', function() { | ||
var canonicalObj = diff.canonicalize({b: 456, a: [789, {d: 123, c: 456}]}); | ||
getKeys(canonicalObj.a[1]).should.eql(['c', 'd']); | ||
}); | ||
|
||
it('should handle circular references correctly', function() { | ||
var obj = {b: 456}; | ||
obj.a = obj; | ||
var canonicalObj = diff.canonicalize(obj); | ||
getKeys(canonicalObj).should.eql(['a', 'b']); | ||
getKeys(canonicalObj.a).should.eql(['a', 'b']); | ||
}); | ||
}); |