clone
Advanced tools
Comparing version 0.1.11 to 0.1.12
12
clone.js
@@ -42,6 +42,12 @@ "use strict"; | ||
* circular references. (optional - true by default) | ||
* @param `depth` - set to a number if the object is only to be cloned to | ||
* a particular depth. (optional - defaults to Infinity) | ||
*/ | ||
function clone(parent, circular) { | ||
function clone(parent, circular, depth) { | ||
if (typeof circular == 'undefined') | ||
circular = true; | ||
if (typeof depth == 'undefined') | ||
depth = Infinity; | ||
if (depth === 0) | ||
return parent; | ||
@@ -126,3 +132,3 @@ var useBuffer = typeof Buffer != 'undefined'; | ||
for(i in parent) | ||
child[i] = clone(parent[i], circular); | ||
child[i] = clone(parent[i], circular, depth - 1); | ||
} | ||
@@ -138,3 +144,3 @@ else if (util.isDate(parent)) | ||
for(i in parent) | ||
child[i] = clone(parent[i], circular); | ||
child[i] = clone(parent[i], circular, depth - 1); | ||
} | ||
@@ -141,0 +147,0 @@ } |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"repository": { | ||
@@ -21,3 +21,3 @@ "type": "git", | ||
"main": "clone.js", | ||
"author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)", | ||
"author": "Paul Vorbach <paul@vorba.ch> (http://vorba.ch/)", | ||
"contributors": [ | ||
@@ -33,3 +33,4 @@ "Blake Miner <miner.blake@gmail.com> (http://www.blakeminer.com/)", | ||
"Dustin Diaz (http://dustindiaz.com)", | ||
"Ilya Shaisultanov (https://github.com/diversario)" | ||
"Ilya Shaisultanov (https://github.com/diversario)", | ||
"Nathan MacInnes <nathan@macinn.es> (http://macinn.es/)" | ||
], | ||
@@ -36,0 +37,0 @@ "engines": { |
@@ -47,3 +47,3 @@ # clone | ||
`clone(val, circular)` | ||
`clone(val, circular, depth)` | ||
@@ -56,2 +56,4 @@ * `val` -- the value that you want to clone, any type allowed | ||
There is no error if `undefined` or `null` is passed as `obj`. | ||
* `depth` -- depth to wich the object is to be cloned (optional, | ||
defaults to infinity) | ||
@@ -58,0 +60,0 @@ `clone.clonePrototype(obj)` |
21
test.js
@@ -218,1 +218,22 @@ if(module.parent === null) { | ||
} | ||
exports['clone object with depth argument'] = function (test) { | ||
test.expect(6); | ||
var a = { | ||
foo: { | ||
bar : { | ||
baz : 'qux' | ||
} | ||
} | ||
}; | ||
var b = clone(a, false, 1); | ||
test.deepEqual(b, a); | ||
test.notEqual(b, a); | ||
test.strictEqual(b.foo, a.foo); | ||
b = clone(a, false, 2); | ||
test.deepEqual(b, a); | ||
test.notEqual(b.foo, a.foo); | ||
test.strictEqual(b.foo.bar, a.foo.bar); | ||
test.done(); | ||
} |
15533
325
127