clone
Advanced tools
Comparing version 1.0.2 to 2.0.0
85
clone.js
var clone = (function() { | ||
'use strict'; | ||
var nativeMap; | ||
try { | ||
nativeMap = Map; | ||
} catch(_) { | ||
// maybe a reference error because no `Map`. Give it a dummy value that no | ||
// value will ever be an instanceof. | ||
nativeMap = function() {}; | ||
} | ||
var nativeSet; | ||
try { | ||
nativeSet = Set; | ||
} catch(_) { | ||
nativeSet = function() {}; | ||
} | ||
var nativePromise; | ||
try { | ||
nativePromise = Promise; | ||
} catch(_) { | ||
nativePromise = function() {}; | ||
} | ||
/** | ||
@@ -28,3 +51,3 @@ * Clones (copies) an Object using deep copying. | ||
filter = circular.filter; | ||
circular = circular.circular | ||
circular = circular.circular; | ||
} | ||
@@ -50,3 +73,3 @@ // maintain two arrays for circular references, where corresponding parents | ||
if (depth == 0) | ||
if (depth === 0) | ||
return parent; | ||
@@ -60,3 +83,15 @@ | ||
if (clone.__isArray(parent)) { | ||
if (parent instanceof nativeMap) { | ||
child = new nativeMap(); | ||
} else if (parent instanceof nativeSet) { | ||
child = new nativeSet(); | ||
} else if (parent instanceof nativePromise) { | ||
child = new nativePromise(function (resolve, reject) { | ||
parent.then(function(value) { | ||
resolve(_clone(value, depth - 1)); | ||
}, function(err) { | ||
reject(_clone(err, depth - 1)); | ||
}); | ||
}); | ||
} else if (clone.__isArray(parent)) { | ||
child = []; | ||
@@ -93,2 +128,26 @@ } else if (clone.__isRegExp(parent)) { | ||
if (parent instanceof nativeMap) { | ||
var keyIterator = parent.keys(); | ||
while(true) { | ||
var next = keyIterator.next(); | ||
if (next.done) { | ||
break; | ||
} | ||
var keyChild = _clone(next.value, depth - 1); | ||
var valueChild = _clone(parent.get(next.value), depth - 1); | ||
child.set(keyChild, valueChild); | ||
} | ||
} | ||
if (parent instanceof nativeSet) { | ||
var iterator = parent.keys(); | ||
while(true) { | ||
var next = iterator.next(); | ||
if (next.done) { | ||
break; | ||
} | ||
var entryChild = _clone(next.value, depth - 1); | ||
child.add(entryChild); | ||
} | ||
} | ||
for (var i in parent) { | ||
@@ -106,2 +165,12 @@ var attrs; | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(parent); | ||
for (var i = 0; i < symbols.length; i++) { | ||
// Don't need to worry about cloning a symbol because it is a primitive, | ||
// like a number or string. | ||
var symbol = symbols[i]; | ||
child[symbol] = _clone(parent[symbol], depth - 1); | ||
} | ||
} | ||
return child; | ||
@@ -133,3 +202,3 @@ } | ||
return Object.prototype.toString.call(o); | ||
}; | ||
} | ||
clone.__objToStr = __objToStr; | ||
@@ -139,3 +208,3 @@ | ||
return typeof o === 'object' && __objToStr(o) === '[object Date]'; | ||
}; | ||
} | ||
clone.__isDate = __isDate; | ||
@@ -145,3 +214,3 @@ | ||
return typeof o === 'object' && __objToStr(o) === '[object Array]'; | ||
}; | ||
} | ||
clone.__isArray = __isArray; | ||
@@ -151,3 +220,3 @@ | ||
return typeof o === 'object' && __objToStr(o) === '[object RegExp]'; | ||
}; | ||
} | ||
clone.__isRegExp = __isRegExp; | ||
@@ -161,3 +230,3 @@ | ||
return flags; | ||
}; | ||
} | ||
clone.__getRegExpFlags = __getRegExpFlags; | ||
@@ -164,0 +233,0 @@ |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"repository": { | ||
@@ -38,3 +38,5 @@ "type": "git", | ||
"Aurélio A. Heckert (http://softwarelivre.org/aurium)", | ||
"Guy Ellis (http://www.guyellisrocks.com/)" | ||
"Guy Ellis (http://www.guyellisrocks.com/)", | ||
"fscherwi (https://fscherwi.github.io)", | ||
"rictic (https://github.com/rictic)" | ||
], | ||
@@ -41,0 +43,0 @@ "license": "MIT", |
# clone | ||
[![build status](https://secure.travis-ci.org/pvorb/node-clone.png)](http://travis-ci.org/pvorb/node-clone) | ||
[![build status](https://secure.travis-ci.org/pvorb/node-clone.svg)](http://travis-ci.org/pvorb/node-clone) | ||
[![info badge](https://nodei.co/npm/clone.png?downloads=true&downloadRank=true&stars=true)](http://npm-stat.com/charts.html?package=clone) | ||
[![info badge](https://nodei.co/npm/clone.svg?downloads=true&downloadRank=true&stars=true)](http://npm-stat.com/charts.html?package=clone) | ||
@@ -14,3 +14,5 @@ offers foolproof _deep cloning_ of objects, arrays, numbers, strings etc. in JavaScript. | ||
(It also works with browserify, ender or standalone.) | ||
(It also works with browserify, ender or standalone. You may want to use the | ||
option `noParse` in browserify to reduce the resulting file size, since usually | ||
`Buffer`s are not needed in browsers.) | ||
@@ -17,0 +19,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
0
129
0
13035
6
200
1