Skip to content
This repository was archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Add .shallowClone and .deepClone api convenience aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
johan committed Oct 11, 2014
1 parent fc0eae2 commit 73f48cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ npm install node-v8-clone
### Usage:

```javascript
var clone = require('node-v8-clone').clone;
var clone = require('node-v8-clone');
var shallowClone = clone.shallowClone;
var deepClone = clone.deepClone;
var a = { x: { y: {} } };

// deep clone
var b = clone(a, true);
var b = deepClone(a);
a === b // false
a.x === b.x // false
a.x.y === b.x.y // false

// shallow clone
var c = clone(a, false);
var c = shallowClone(a);
a === c // false
a.x === c.x // true
a.x.y === c.x.y // true
Expand Down
16 changes: 12 additions & 4 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ Cloner.prototype._noop = function(cloner, value) {};
var cloner_shallow = new Cloner();
var cloner_deep = new Cloner(true);

module.exports.clone = function clone(value, deep) {
if (deep) {
module.exports =
{ clone: function clone(value, deep) {
if (deep) {
return cloner_deep.clone(value);
}
return cloner_shallow.clone(value);
}
, deepClone: function deepClone(value) {
return cloner_deep.clone(value);
}
return cloner_shallow.clone(value);
};
, shallowClone: function shallowClone(value) {
return cloner_shallow.clone(value);
}
};
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var assert = require('assert');
var Cloner = require('..').Cloner;
var clone = require('..').clone;
var deepClone = require('..').deepClone;
var shallowClone = require('..').shallowClone;
var shared = require('./shared.js');

describe('node-v8-clone.clone(obj, false)', function(){
Expand All @@ -12,6 +14,15 @@ describe('node-v8-clone.clone(obj, false)', function(){
shared.behavesAsShallow();
});

describe('node-v8-clone.shallowClone(obj)', function(){
beforeEach(function(){
this.clone = function(value) {
return shallowClone(value);
};
});
shared.behavesAsShallow();
});

describe('node-v8-clone.clone(obj, true)', function(){
beforeEach(function(){
this.clone = function(value) {
Expand All @@ -23,6 +34,17 @@ describe('node-v8-clone.clone(obj, true)', function(){
shared.behavesAsDeepWCircular();
});

describe('node-v8-clone.deepClone(obj)', function(){
beforeEach(function(){
this.clone = function(value) {
return deepClone(value);
};
});
shared.behavesAsShallow();
shared.behavesAsDeep();
shared.behavesAsDeepWCircular();
});


describe('new Cloner(false)', function(){
beforeEach(function(){
Expand Down

0 comments on commit 73f48cb

Please sign in to comment.