Comparing version 0.0.4 to 1.0.0
{ | ||
"name": "udc", | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"description": "UltraDeepClone - A complete (as possible) deep-clone algorithm that can clone objects with cycles.", | ||
@@ -15,6 +15,7 @@ "main": "udc.js", | ||
"cycles", | ||
"cyclical", | ||
"deepclone", | ||
"deep-clone", | ||
"ultra", | ||
"deep" | ||
"deep", | ||
"typed arrays" | ||
], | ||
@@ -21,0 +22,0 @@ "devDependencies": { |
@@ -13,7 +13,11 @@ var vows = require('vows'), | ||
var testString = "12345"; | ||
var testArray = ["foo", "bar" ]; | ||
var testArray = ["foo", "bar"]; | ||
var testRegExp = /./g; | ||
var testBoolean = true; | ||
var testFunction = function _test() { return "test"; }; | ||
var testObject = { foo:"test_foo" }; | ||
var testObject = { foo: "test_foo" }; | ||
var testTypedArray = new Int32Array(3); | ||
testTypedArray[0] = 6; | ||
testTypedArray[1] = 3; | ||
testTypedArray[2] = -99; | ||
@@ -34,3 +38,4 @@ var testComplexObject = { | ||
cycles : {}, | ||
cycles2 :{} | ||
cycles2 :{}, | ||
typedArray: testTypedArray | ||
}; | ||
@@ -51,6 +56,2 @@ | ||
assert.equal (clone, testNumber); | ||
}, | ||
'Changes Independently' : function (clone) { | ||
clone = 0; | ||
assert.equal (testNumber, 12345); | ||
} | ||
@@ -80,2 +81,8 @@ }, | ||
assert.deepEqual (clone, testArray); | ||
}, | ||
'Changes Independently' : function (clone) { | ||
clone[0] = 'something'; | ||
clone[1] = 'different'; | ||
assert.equal (testArray[0], "foo"); | ||
assert.equal (testArray[1], "bar"); | ||
} | ||
@@ -89,2 +96,12 @@ }, | ||
}, | ||
'Typed Array': { | ||
topic: function(){ return deepCopy(testTypedArray); }, | ||
'Correct Value' : function (clone) { | ||
assert.deepEqual (clone, testTypedArray); | ||
}, | ||
'Changes Independently' : function (clone) { | ||
clone[0] = 0; | ||
assert.equal (testTypedArray[0], 6); | ||
} | ||
}, | ||
'Simple Object': { | ||
@@ -94,2 +111,6 @@ topic: function(){ return deepCopy(testObject); }, | ||
assert.deepEqual (clone, testObject); | ||
}, | ||
'Changes Independently' : function (clone) { | ||
clone.foo = 'different'; | ||
assert.equal (testObject.foo, "test_foo"); | ||
} | ||
@@ -168,4 +189,9 @@ }, | ||
assert.equal (clone, clone.cycles.parent); | ||
}, | ||
'typed array' : function (clone) { | ||
assert.deepEqual (clone.typedArray, testTypedArray); | ||
clone.typedArray[0] = 0; | ||
assert.equal (testTypedArray[0], 6); | ||
} | ||
} | ||
}).export(module); |
92
udc.js
@@ -15,14 +15,27 @@ (function (root, factory) { | ||
"caller", | ||
"arguments", | ||
"prototype" | ||
"arguments" | ||
]; | ||
// Node.js has a lot of silly properties on their "TypedArray" implementation | ||
var typedArrayPropertyFilter = [ | ||
'BYTES_PER_ELEMENT', | ||
'get', | ||
'set', | ||
'slice', | ||
'subarray', | ||
'buffer', | ||
'length', | ||
'byteOffset', | ||
'byteLength' | ||
]; | ||
var primitiveCloner = makeCloner(clonePrimitive); | ||
var typedArrayCloner = makeRecursiveCloner(makeCloner(cloneTypedArray), typedArrayPropertyFilter); | ||
var cloneFunctions = { | ||
"[object Null]" : primitiveCloner , | ||
"[object Undefined]" : primitiveCloner , | ||
"[object Number]" : primitiveCloner , | ||
"[object String]" : primitiveCloner , | ||
"[object Boolean]" : primitiveCloner , | ||
"[object Null]" : primitiveCloner, | ||
"[object Undefined]" : primitiveCloner, | ||
"[object Number]" : primitiveCloner, | ||
"[object String]" : primitiveCloner, | ||
"[object Boolean]" : primitiveCloner, | ||
"[object RegExp]" : makeCloner(cloneRegExp), | ||
@@ -32,6 +45,32 @@ "[object Date]" : makeCloner(cloneDate), | ||
"[object Object]" : makeRecursiveCloner(makeCloner(cloneObject)), | ||
"[object Array]" : makeRecursiveCloner(makeCloner(cloneArray)) | ||
"[object Array]" : makeRecursiveCloner(makeCloner(cloneArray)), | ||
"[object Int8Array]" : typedArrayCloner, | ||
"[object Uint8Array]" : typedArrayCloner, | ||
"[object Uint8ClampedArray]" : typedArrayCloner, | ||
"[object Int16Array]" : typedArrayCloner, | ||
"[object Uint16Array]" : typedArrayCloner, | ||
"[object Int32Array]" : typedArrayCloner, | ||
"[object Uint32Array]" : typedArrayCloner, | ||
"[object Float32Array]" : typedArrayCloner, | ||
"[object Float64Array]" :typedArrayCloner | ||
}; | ||
function makeCloner(cloneThing) { | ||
function makeArguments (numberOfArgs) { | ||
var letters = []; | ||
for ( var i = 1; i <= numberOfArgs; i++ ) letters.push("arg" + i); | ||
return letters; | ||
} | ||
function wrapFunctionWithArity (callback) { | ||
var argList = makeArguments(callback.length); | ||
var functionCode = 'return false || function '; | ||
functionCode += callback.name + '('; | ||
functionCode += argList.join(', ') + ') {\n'; | ||
functionCode += 'return fn.apply(this, arguments);\n'; | ||
functionCode += '};' | ||
return Function("fn", functionCode)(callback); | ||
} | ||
function makeCloner (cloneThing) { | ||
return function(thing, thingStack, copyStack) { | ||
@@ -45,18 +84,18 @@ thingStack.push(thing); | ||
function clonePrimitive(primitive) { | ||
function clonePrimitive (primitive) { | ||
return primitive; | ||
} | ||
function cloneRegExp(regexp) { | ||
function cloneRegExp (regexp) { | ||
return new RegExp(regexp); | ||
} | ||
function cloneDate(date) { | ||
function cloneDate (date) { | ||
return new Date(date.getTime()); | ||
} | ||
function cloneFunction(fn) { | ||
var copy = Function("return " + fn.toString() + ";")(); | ||
copy.prototype = Object.getPrototypeOf(fn); | ||
return copy; | ||
// We can't really clone functions but we can wrap them in a new function that will | ||
// recieve clones of any properties the original function may have had | ||
function cloneFunction (fn) { | ||
return wrapFunctionWithArity(fn); | ||
} | ||
@@ -67,11 +106,16 @@ | ||
// was originally invoked. | ||
function cloneObject(object) { | ||
function cloneObject (object) { | ||
return Object.create(Object.getPrototypeOf(object)); | ||
} | ||
function cloneArray(array) { | ||
function cloneArray (array) { | ||
return []; | ||
} | ||
function makeRecursiveCloner(cloneThing, propertyFilter) { | ||
function cloneTypedArray (typedArray) { | ||
var len = typedArray.length; | ||
return new typedArray.constructor(len); | ||
} | ||
function makeRecursiveCloner (cloneThing, propertyFilter) { | ||
return function(thing, thingStack, copyStack) { | ||
@@ -98,11 +142,11 @@ var clone = this; | ||
return function _ultraDeepClone(source) { | ||
return function _ultraDeepClone (source) { | ||
var thingStack = [], | ||
copyStack = []; | ||
var thingStack = []; | ||
var copyStack = []; | ||
function clone(thing){ | ||
function clone (thing) { | ||
var typeOfThing = Object.prototype.toString.call(thing); | ||
return cloneFunctions[typeOfThing].call(clone, thing, thingStack, copyStack); | ||
} | ||
}; | ||
@@ -109,0 +153,0 @@ return clone(source); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14785
307
1