can-reflect
Advanced tools
{ | ||
"name": "can-reflect", | ||
"version": "1.16.4", | ||
"version": "1.16.5", | ||
"description": "reflection on unknown data types", | ||
@@ -5,0 +5,0 @@ "homepage": "http://canjs.com", |
@@ -380,2 +380,63 @@ var QUnit = require('steal-qunit'); | ||
QUnit.test("objects that serialize to strings should cache properly", function(){ | ||
function SimpleType(){} | ||
getSetReflections.setKeyValue(SimpleType.prototype, canSymbol.for("can.serialize"), function(){ | ||
return "baz"; | ||
}); | ||
var obj = new SimpleType(); | ||
var p = { | ||
foo: obj, bar: obj | ||
}; | ||
deepEqual(shapeReflections.serialize(p, window.Map), {foo:"baz", bar:"baz"}); | ||
}); | ||
QUnit.test("throw error when serializing circular reference", function(){ | ||
function SimpleType(){} | ||
var a = new SimpleType(); | ||
var b = new SimpleType(); | ||
a.b = b; | ||
b.a = a; | ||
getSetReflections.setKeyValue(a, canSymbol.for("can.serialize"), function(){ | ||
return { | ||
b: shapeReflections.serialize(this.b) | ||
}; | ||
}); | ||
getSetReflections.setKeyValue(b, canSymbol.for("can.serialize"), function(){ | ||
return { | ||
a: shapeReflections.serialize(this.a) | ||
}; | ||
}); | ||
try{ | ||
shapeReflections.serialize(a, window.Map); | ||
QUnit.ok(false); | ||
}catch(e){ | ||
QUnit.ok(true); | ||
} | ||
}); | ||
QUnit.test("throw should not when serializing circular reference properly", function(){ | ||
function SimpleType(){} | ||
var a = new SimpleType(); | ||
var b = new SimpleType(); | ||
a.b = b; | ||
b.a = a; | ||
getSetReflections.setKeyValue(a, canSymbol.for("can.serialize"), function(proto){ | ||
return proto.b = shapeReflections.serialize(this.b); | ||
}); | ||
getSetReflections.setKeyValue(b, canSymbol.for("can.serialize"), function(proto){ | ||
return proto.a = shapeReflections.serialize(this.a); | ||
}); | ||
try{ | ||
shapeReflections.serialize(a, window.Map); | ||
QUnit.ok(true); | ||
}catch(e){ | ||
QUnit.ok(false); | ||
} | ||
}); | ||
QUnit.test("updateDeep basics", function(){ | ||
@@ -522,3 +583,3 @@ | ||
QUnit.test("hasKey", function() { | ||
/*var objHasKey = {}; | ||
var objHasKey = {}; | ||
Object.defineProperty(objHasKey, "_keys", { | ||
@@ -544,4 +605,3 @@ value: { foo: true } | ||
objHasOwnKey.bar = "baz"; | ||
QUnit.ok(shapeReflections.hasKey(objHasOwnKey, "bar") , "returns true when hasOwnKey Symbol returns false but `in` returns true");*/ | ||
debugger; | ||
QUnit.ok(shapeReflections.hasKey(objHasOwnKey, "bar") , "returns true when hasOwnKey Symbol returns false but `in` returns true"); | ||
@@ -548,0 +608,0 @@ QUnit.ok(shapeReflections.hasKey(55, "toFixed") , "works on primitives"); |
@@ -7,3 +7,3 @@ var canSymbol = require("can-symbol"); | ||
// IE-remove-start | ||
var getPrototypeOfWorksWithPrimitives = true; | ||
@@ -15,4 +15,4 @@ try { | ||
} | ||
// IE-remove-end | ||
var ArrayMap; | ||
@@ -22,2 +22,3 @@ if(typeof Map === "function") { | ||
} else { | ||
// IE-remove-start | ||
function isEven(num) { | ||
@@ -65,4 +66,12 @@ return !(num % 2); | ||
} | ||
}, | ||
"delete": function(key){ | ||
var idx = this._getIndex(key); | ||
if(idx !== -1) { | ||
// Key already exists, replace the value. | ||
this.contents.splice(idx, 2); | ||
} | ||
} | ||
}; | ||
// IE-remove-end | ||
} | ||
@@ -123,2 +132,3 @@ | ||
return function serializer(value, MapType){ | ||
if (isSerializedHelper(value)) { | ||
@@ -130,5 +140,14 @@ return value; | ||
if(!serializeMap) { | ||
MapType = MapType || ArrayMap; | ||
serializeMap = { | ||
unwrap: MapType ? new MapType() : new ArrayMap(), | ||
serialize: MapType ? new MapType() : new ArrayMap() | ||
unwrap: new MapType(), | ||
serialize: new MapType() , | ||
isSerializing: { | ||
unwrap: new MapType(), | ||
serialize: new MapType() | ||
}, | ||
circularReferenceIsSerializing: { | ||
unwrap: new MapType(), | ||
serialize: new MapType() | ||
} | ||
}; | ||
@@ -153,2 +172,6 @@ firstSerialize = true; | ||
if( serializeMap[methodName].has(value) ) { | ||
// if we are in the process of serializing the first time, setup circular reference detection. | ||
if(serializeMap.isSerializing[methodName].has(value)) { | ||
serializeMap.circularReferenceIsSerializing[methodName].set(value, true); | ||
} | ||
return serializeMap[methodName].get(value); | ||
@@ -163,3 +186,21 @@ } else { | ||
if(serializer) { | ||
var result = serializer.call(value, serialized); | ||
// mark that we are serializing | ||
serializeMap.isSerializing[methodName].set(value, true); | ||
var result = serializer.call(value, serialized); | ||
serializeMap.isSerializing[methodName].delete(value); | ||
// if the result differs, but this was circular, blow up. | ||
if(result !== serialized) { | ||
// jshint -W073 | ||
if(serializeMap.circularReferenceIsSerializing[methodName].has(value)) { | ||
// Circular references should use a custom serializer | ||
// that sets the serialized value on the object | ||
// passed to it as the first argument e.g. | ||
// function(proto){ | ||
// return proto.a = canReflect.serialize(this.a); | ||
// } | ||
throw new Error("Cannot serialize cirular reference!"); | ||
} | ||
serializeMap[methodName].set(value, result); | ||
} | ||
if(firstSerialize) { | ||
@@ -1011,7 +1052,16 @@ serializeMap = null; | ||
} else { | ||
var proto = (getPrototypeOfWorksWithPrimitives ? Object.getPrototypeOf(obj) : obj.__proto__); | ||
var proto; | ||
if(getPrototypeOfWorksWithPrimitives) { | ||
proto = Object.getPrototypeOf(obj); | ||
} else { | ||
// IE-remove-start | ||
proto = obj.__proto__; | ||
// IE-remove-end | ||
}; | ||
if(proto !== undefined) { | ||
return key in proto; | ||
} else { | ||
// IE-remove-start | ||
return obj[key] !== undefined; | ||
// IE-remove-end | ||
} | ||
@@ -1018,0 +1068,0 @@ } |
176783
1.76%4799
2.17%