can-reflect
Advanced tools
Comparing version 1.1.0 to 1.1.3
{ | ||
"name": "can-reflect", | ||
"version": "1.1.0", | ||
"version": "1.1.3", | ||
"description": "reflection on unknown data types", | ||
@@ -5,0 +5,0 @@ "homepage": "http://canjs.com", |
@@ -406,3 +406,40 @@ var QUnit = require('steal-qunit'); | ||
QUnit.test("assignMap", function(){ | ||
var target = shapeReflections.assignSymbols({},{ | ||
"can.setKeyValue": function(key, value){ | ||
this[key] = value * 2; | ||
}, | ||
"can.getKeyValue": function(key) { | ||
return this[key] !== undefined ? this[key] / 2 : undefined; | ||
} | ||
}); | ||
target.a = 22; | ||
var source = shapeReflections.assignSymbols({},{ | ||
"can.setKeyValue": function(key, value){ | ||
this[key] = value * 3; | ||
}, | ||
"can.getKeyValue": function(key) { | ||
return this[key] !== undefined ? this[key] / 3 : undefined; | ||
} | ||
}); | ||
shapeReflections.assignMap(source,{ | ||
a: 1, | ||
b: 2 | ||
}); | ||
QUnit.deepEqual(source,{ | ||
a: 3, | ||
b: 6 | ||
}, "set values on source"); | ||
shapeReflections.assignMap(target, source); | ||
QUnit.deepEqual(target,{ | ||
a: 2, | ||
b: 4 | ||
}, "set values on target"); | ||
}); | ||
/*QUnit.module('can-reflect: shape reflections: proto chain'); | ||
@@ -409,0 +446,0 @@ |
@@ -6,2 +6,16 @@ var canSymbol = require("can-symbol"); | ||
var shiftFirstArgumentToThis = function(func){ | ||
return function(){ | ||
var args = [this]; | ||
args.push.apply(args, arguments); | ||
return func.apply(null,args); | ||
}; | ||
}; | ||
var getKeyValueSymbol = canSymbol.for("can.getKeyValue"); | ||
var shiftedGetKeyValue = shiftFirstArgumentToThis(getSetReflections.getKeyValue); | ||
var setKeyValueSymbol = canSymbol.for("can.setKeyValue"); | ||
var shiftedSetKeyValue = shiftFirstArgumentToThis(getSetReflections.setKeyValue); | ||
var serializeMap = null; | ||
@@ -226,28 +240,38 @@ | ||
// each index in something list-like. Uses iterator if it has it. | ||
var iter, iterator = list[canSymbol.iterator]; | ||
if(Array.isArray(list)) { | ||
// do nothing | ||
} else if(typeReflections.isIteratorLike(list)) { | ||
// we are looping through an iterator | ||
iter = list; | ||
} else if(iterator) { | ||
iter = iterator.call(list); | ||
} | ||
// fast-path arrays | ||
if(iter) { | ||
var res, index = 0; | ||
return this.eachListLike(list, callback, context); | ||
} else { | ||
var iter, iterator = list[canSymbol.iterator]; | ||
if(typeReflections.isIteratorLike(list)) { | ||
// we are looping through an iterator | ||
iter = list; | ||
} else if(iterator) { | ||
iter = iterator.call(list); | ||
} | ||
// fast-path arrays | ||
if(iter) { | ||
var res, index = 0; | ||
while(!(res = iter.next()).done) { | ||
if( callback.call(context || list, res.value, index++, list) === false ){ | ||
break; | ||
while(!(res = iter.next()).done) { | ||
if( callback.call(context || list, res.value, index++, list) === false ){ | ||
break; | ||
} | ||
} | ||
} else { | ||
this.eachListLike(list, callback, context); | ||
} | ||
} else { | ||
for (var i = 0, len = list.length; i < len; i++) { | ||
var item = list[i]; | ||
if (callback.call(context || item, item, i, list) === false) { | ||
break; | ||
} | ||
} | ||
return list; | ||
}, | ||
eachListLike: function(list, callback, context){ | ||
var index = -1; | ||
var length = list.length; | ||
while (++index < length) { | ||
var item = list[index]; | ||
if (callback.call(context || item, item, index, list) === false) { | ||
break; | ||
} | ||
} | ||
return list; | ||
@@ -309,7 +333,14 @@ }, | ||
// eachOwnEnumerableKey | ||
var enumerableKeys = this.getOwnEnumerableKeys(obj); | ||
return this.eachIndex(enumerableKeys, function(key){ | ||
var value = getSetReflections.getKeyValue(obj, key); | ||
return callback.call(context || obj, value, key, obj); | ||
}); | ||
if(obj) { | ||
var enumerableKeys = this.getOwnEnumerableKeys(obj); | ||
// cache getKeyValue method if we can | ||
var getKeyValue = obj[getKeyValueSymbol] || shiftedGetKeyValue; | ||
return this.eachIndex(enumerableKeys, function(key){ | ||
var value = getKeyValue.call(obj, key); | ||
return callback.call(context || obj, value, key, obj); | ||
}); | ||
} | ||
return obj; | ||
}, | ||
@@ -501,6 +532,8 @@ /** | ||
var targetKeyMap = makeMap(this.getOwnEnumerableKeys(target)); | ||
var getKeyValue = target[getKeyValueSymbol] || shiftedGetKeyValue; | ||
var setKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; | ||
this.eachKey(source,function(value, key){ | ||
// if the target doesn't have this key or the keys are not the same | ||
if(!targetKeyMap[target] || getSetReflections.getKeyValue(target, key) !== value) { | ||
getSetReflections.setKeyValue(target, key, value); | ||
if(!targetKeyMap[key] || getKeyValue.call(target, key) !== value) { | ||
setKeyValue.call(target, key, value); | ||
} | ||
@@ -527,2 +560,4 @@ }); | ||
var targetKeyMap = makeMap(this.getOwnEnumerableKeys(target)); | ||
var getKeyValue = target[getKeyValueSymbol] || shiftedGetKeyValue; | ||
var setKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; | ||
@@ -534,3 +569,3 @@ this.eachKey(source, function(newVal, key){ | ||
} else { | ||
var curVal = getSetReflections.getKeyValue(target, key); | ||
var curVal = getKeyValue.call(target, key); | ||
@@ -541,3 +576,3 @@ // if either was primitive, no recursive update possible | ||
} else if(typeReflections.isPrimitive(curVal) || typeReflections.isPrimitive(newVal)) { | ||
getSetReflections.setKeyValue(target, key, newVal); | ||
setKeyValue.call(target, key, newVal); | ||
} else{ | ||
@@ -569,2 +604,5 @@ this.assignDeep(curVal, newVal); | ||
var sourceGetKeyValue = source[getKeyValueSymbol] || shiftedGetKeyValue; | ||
var targetSetKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; | ||
this.eachKey(target, function(curVal, key){ | ||
@@ -576,7 +614,7 @@ if(!sourceKeyMap[key]) { | ||
sourceKeyMap[key] = false; | ||
var newVal = getSetReflections.getKeyValue(source, key); | ||
var newVal = sourceGetKeyValue.call(source, key); | ||
// if either was primitive, no recursive update possible | ||
if(newVal !== curVal) { | ||
getSetReflections.setKeyValue(target, key, newVal); | ||
targetSetKeyValue.call(target, key, newVal); | ||
} | ||
@@ -587,3 +625,3 @@ }, this); | ||
if(sourceKeyMap[key]) { | ||
getSetReflections.setKeyValue(target, key, getSetReflections.getKeyValue(source, key) ); | ||
targetSetKeyValue.call(target, key, sourceGetKeyValue.call(source, key) ); | ||
} | ||
@@ -610,5 +648,7 @@ } | ||
var sourceGetKeyValue = source[getKeyValueSymbol] || shiftedGetKeyValue; | ||
var targetSetKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue; | ||
this.eachKey(target, function(curVal, key){ | ||
if(!sourceKeyMap[key]) { | ||
@@ -619,7 +659,7 @@ getSetReflections.deleteKeyValue(target, key); | ||
sourceKeyMap[key] = false; | ||
var newVal = getSetReflections.getKeyValue(source, key); | ||
var newVal = sourceGetKeyValue.call(source, key); | ||
// if either was primitive, no recursive update possible | ||
if(typeReflections.isPrimitive(curVal) || typeReflections.isPrimitive(newVal)) { | ||
getSetReflections.setKeyValue(target, key, newVal); | ||
targetSetKeyValue.call(target, key, newVal); | ||
} else{ | ||
@@ -633,3 +673,3 @@ this.updateDeep(curVal, newVal); | ||
if(sourceKeyMap[key]) { | ||
getSetReflections.setKeyValue(target, key, getSetReflections.getKeyValue(source, key) ); | ||
targetSetKeyValue.call(target, key, sourceGetKeyValue.call(source, key) ); | ||
} | ||
@@ -661,4 +701,4 @@ } | ||
this.eachKey(source, function(value, key){ | ||
this.setKeyValue(target, canSymbol.for(key), value); | ||
}, this); | ||
getSetReflections.setKeyValue(target, canSymbol.for(key), value); | ||
}); | ||
return target; | ||
@@ -665,0 +705,0 @@ } |
99931
2570