-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clone additionalMethods to prevent mutation (#4)
Closes #3
- Loading branch information
Showing
4 changed files
with
50 additions
and
3 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,26 @@ | ||
'use strict' | ||
|
||
var supports = require('..') | ||
|
||
// Every object in a manifest must have a unique identity, to avoid accidental | ||
// mutation. In supports() we only shallowly clone the manifest object itself | ||
// and additionalMethods. If in the future we add more objects to manifests, | ||
// this test will break and we'll know to start performing a deep clone. | ||
module.exports = function cloneable (t, manifest) { | ||
var copy = supports(manifest) | ||
verifyUnique(t, 'manifest', manifest, copy) | ||
} | ||
|
||
function verifyUnique (t, path, a, b) { | ||
if (isObject(a) && isObject(b)) { | ||
t.ok(a !== b, path + ' has unique identity') | ||
|
||
Object.keys(a).forEach(function (key) { | ||
verifyUnique(t, path + '.' + key, a[key], b[key]) | ||
}) | ||
} | ||
} | ||
|
||
function isObject (o) { | ||
return typeof o === 'object' && o !== null | ||
} |
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