Comparing version 0.1.6 to 0.1.7
100
clone.js
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
var util = require('util'); | ||
@@ -25,56 +27,57 @@ | ||
var i; | ||
if (circular) { | ||
var circularParent = {}; | ||
var circularResolved = {}; | ||
var circularReplace = []; | ||
function _clone(parent, context, child, cIndex) { | ||
var i; // Use local context within this function | ||
// Deep clone all properties of parent into child | ||
if (typeof parent == 'object') { | ||
if (parent == null) | ||
return parent; | ||
// Check for circular references | ||
for(i in circularParent) | ||
if (circularParent[i] === parent) { | ||
// We found a circular reference | ||
circularReplace.push({'resolveTo': i, 'child': child, 'i': cIndex}); | ||
return null; //Just return null for now... | ||
// we will resolve circular references later | ||
} | ||
var circularParent = {}; | ||
var circularResolved = {}; | ||
var circularReplace = []; | ||
// Add to list of all parent objects | ||
circularParent[context] = parent; | ||
// Now continue cloning... | ||
if (util.isArray(parent)) { | ||
child = []; | ||
for(i in parent) | ||
child[i] = _clone(parent[i], context + '[' + i + ']', child, i); | ||
function _clone(parent, context, child, cIndex) { | ||
var i; // Use local context within this function | ||
// Deep clone all properties of parent into child | ||
if (typeof parent == 'object') { | ||
if (parent == null) | ||
return parent; | ||
// Check for circular references | ||
for(i in circularParent) | ||
if (circularParent[i] === parent) { | ||
// We found a circular reference | ||
circularReplace.push({'resolveTo': i, 'child': child, 'i': cIndex}); | ||
return null; //Just return null for now... | ||
// we will resolve circular references later | ||
} | ||
else if (util.isDate(parent)) | ||
child = new Date(parent.getTime()); | ||
else if (util.isRegExp(parent)) | ||
child = new RegExp(parent.source); | ||
else if (useBuffer && Buffer.isBuffer(parent)) | ||
{ | ||
child = new Buffer(parent.length); | ||
parent.copy(child); | ||
} | ||
else { | ||
child = {}; | ||
// Also copy prototype over to new cloned object | ||
child.__proto__ = parent.__proto__; | ||
for(i in parent) | ||
child[i] = _clone(parent[i], context + '[' + i + ']', child, i); | ||
} | ||
// Add to list of all parent objects | ||
circularParent[context] = parent; | ||
// Now continue cloning... | ||
if (util.isArray(parent)) { | ||
child = []; | ||
for(i in parent) | ||
child[i] = _clone(parent[i], context + '[' + i + ']', child, i); | ||
} | ||
else if (util.isDate(parent)) | ||
child = new Date(parent.getTime()); | ||
else if (util.isRegExp(parent)) | ||
child = new RegExp(parent.source); | ||
else if (useBuffer && Buffer.isBuffer(parent)) | ||
{ | ||
child = new Buffer(parent.length); | ||
parent.copy(child); | ||
} | ||
else { | ||
child = {}; | ||
// Add to list of all cloned objects | ||
circularResolved[context] = child; | ||
// Also copy prototype over to new cloned object | ||
child.__proto__ = parent.__proto__; | ||
for(i in parent) | ||
child[i] = _clone(parent[i], context + '[' + i + ']', child, i); | ||
} | ||
else | ||
child = parent; //Just a simple shallow copy will do | ||
return child; | ||
// Add to list of all cloned objects | ||
circularResolved[context] = child; | ||
} | ||
else | ||
child = parent; //Just a simple shallow copy will do | ||
return child; | ||
} | ||
var i; | ||
if (circular) { | ||
var cloned = _clone(parent, '*'); | ||
@@ -91,4 +94,3 @@ | ||
return cloned; | ||
} | ||
else { | ||
} else { | ||
// Deep clone all properties of parent into child | ||
@@ -95,0 +97,0 @@ var child; |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
/** | ||
@@ -2,0 +4,0 @@ * Simple flat clone using prototype, accepts only objects, usefull for property |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"repository": { | ||
@@ -29,3 +29,4 @@ "type": "git", | ||
"Dan MacTough (http://yabfog.com/)", | ||
"w1nk (https://github.com/w1nk)" | ||
"w1nk (https://github.com/w1nk)", | ||
"Hugh Kennedy (http://twitter.com/hughskennedy)" | ||
], | ||
@@ -32,0 +33,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
268
13525