Comparing version
76
clone.js
| module.exports = clone; | ||
| /** Clones an Object using deep cloning. | ||
| This function supports circular references by default, but if you are certain | ||
| there are no circular references in your object, you can save some CPU time | ||
| by calling clone(obj, false). | ||
| Caution: if `circular` is false and `parent` contains circular references, | ||
| your program may enter an infinite loop and crash. | ||
| @param `parent` - the object to be cloned | ||
| @param `circular` - set to true if the object to be cloned may contain | ||
| circular references. (optional - true by default) | ||
| /** | ||
| * Clones an Object using deep cloning. | ||
| * | ||
| * This function supports circular references by default, but if you are certain | ||
| * there are no circular references in your object, you can save some CPU time | ||
| * by calling clone(obj, false). | ||
| * | ||
| * Caution: if `circular` is false and `parent` contains circular references, | ||
| * your program may enter an infinite loop and crash. | ||
| * | ||
| * @param `parent` - the object to be cloned | ||
| * @param `circular` - set to true if the object to be cloned may contain | ||
| * circular references. (optional - true by default) | ||
| */ | ||
| function clone(parent, circular) { | ||
| if (circular == undefined) circular = true; | ||
| if (typeof circular == 'undefined') | ||
| circular = true; | ||
| if (circular) { | ||
@@ -22,24 +25,25 @@ var circularParent = {}; | ||
| function _clone(parent, context, child, cIndex) { | ||
| //Deep clone all properties of parent into child | ||
| if (typeof parent == "object") { | ||
| if (parent == null) //works for undefined, too | ||
| // Deep clone all properties of parent into child | ||
| if (typeof parent == 'object') { | ||
| if (parent == null) | ||
| return parent; | ||
| //Check for circular references | ||
| // Check for circular references | ||
| for(i in circularParent) | ||
| if (circularParent[i] === parent) { | ||
| //We found a circular reference | ||
| // 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... | ||
| // we will resolve circular references later | ||
| } | ||
| //Add to list of all parent objects | ||
| // Add to list of all parent objects | ||
| circularParent[context] = parent; | ||
| //Now continue cloning... | ||
| // Now continue cloning... | ||
| if (parent instanceof Array) { | ||
| child = []; | ||
| for(i in parent) | ||
| child[i] = _clone(parent[i], context + "[" + i + "]", child, i); | ||
| child[i] = _clone(parent[i], context + '[' + i + ']', child, i); | ||
| } | ||
| else if (parent instanceof Date) | ||
| child = new Date(parent.getTime() ); | ||
| child = new Date(parent.getTime()); | ||
| else if (parent instanceof RegExp) | ||
@@ -49,8 +53,10 @@ child = new RegExp(parent.source); | ||
| child = {}; | ||
| //Also copy prototype over to new cloned object | ||
| // 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); | ||
| child[i] = _clone(parent[i], context + '[' + i + ']', child, i); | ||
| } | ||
| //Add to list of all cloned objects | ||
| // Add to list of all cloned objects | ||
| circularResolved[context] = child; | ||
@@ -62,8 +68,12 @@ } | ||
| } | ||
| var cloned = _clone(parent, "*"); | ||
| /* Now this object has been cloned. Let's check to see if there are any | ||
| circular references for it */ | ||
| var cloned = _clone(parent, '*'); | ||
| // Now this object has been cloned. Let's check to see if there are any | ||
| // circular references for it | ||
| for(i in circularReplace) { | ||
| var c = circularReplace[i]; | ||
| c.child[c.i] = circularResolved[c.resolveTo]; | ||
| if (c && c.child && c.i in c.child) { | ||
| c.child[c.i] = circularResolved[c.resolveTo]; | ||
| } | ||
| } | ||
@@ -73,5 +83,5 @@ return cloned; | ||
| else { | ||
| //Deep clone all properties of parent into child | ||
| // Deep clone all properties of parent into child | ||
| var child; | ||
| if (typeof parent == "object") { | ||
| if (typeof parent == 'object') { | ||
| if (parent == null) | ||
@@ -96,5 +106,5 @@ return parent; | ||
| else | ||
| child = parent; //Just a simple shallow clone will do | ||
| child = parent; // Just a simple shallow clone will do | ||
| return child; | ||
| } | ||
| } | ||
| } |
| { | ||
| "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)", | ||
| "name": "clone", | ||
| "description": "deep cloning of objects and arrays", | ||
| "tags": [ "clone", "object", "array", "function", "date" ], | ||
| "version": "0.0.4", | ||
| "tags": [ | ||
| "clone", | ||
| "object", | ||
| "array", | ||
| "function", | ||
| "date" | ||
| ], | ||
| "version": "0.0.5", | ||
| "repository": { | ||
@@ -15,9 +20,14 @@ "type": "git", | ||
| "main": "clone.js", | ||
| "author": "Paul Vorbach <paul@vorb.de> (http://vorb.de)", | ||
| "contributors": [ | ||
| "Blake Miner <miner.blake@gmail.com> (http://www.blakeminer.com)", | ||
| "Tian You <axqd001@gmail.com> (http://blog.axqd.net/)" | ||
| "Blake Miner <miner.blake@gmail.com> (http://www.blakeminer.com/)", | ||
| "Tian You <axqd001@gmail.com> (http://blog.axqd.net/)", | ||
| "George Stagas <gstagas@gmail.com> (http://stagas.com/)" | ||
| ], | ||
| "engines": { | ||
| "node": "*" | ||
| } | ||
| } | ||
| }, | ||
| "dependencies": {}, | ||
| "devDependencies": {}, | ||
| "optionalDependencies": {} | ||
| } |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7387
25.1%99
8.79%86
59.26%0
-100%