Skip to content

Commit

Permalink
Fixes issue with hasKey not working on primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt committed Jun 14, 2018
1 parent 56ae882 commit 4754791
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions reflections/shape/shape-test.js
Expand Up @@ -540,6 +540,10 @@ QUnit.test("hasKey", function() {

objHasOwnKey.bar = "baz";
QUnit.ok(shapeReflections.hasKey(objHasOwnKey, "bar") , "returns true when hasOwnKey Symbol returns false but `in` returns true");

QUnit.ok(shapeReflections.hasKey(55, "toFixed") , "works on primitives");
QUnit.ok(shapeReflections.hasKey(true, "valueOf") , "works on primitives");
QUnit.ok(shapeReflections.hasKey('foo', "length") , "works on primitives");
});

QUnit.test("serialize clones", function(){
Expand Down
8 changes: 8 additions & 0 deletions reflections/shape/shape.js
Expand Up @@ -983,6 +983,14 @@ shapeReflections = {
* @return {Boolean} `true` if `obj`'s key set contains `key` or an object on its prototype chain's key set contains `key`, `false` otherwise
*/
"hasKey": function(obj, key) {
if (typeReflections.isPrimitive(obj)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return true;
} else {
return key in Object.getPrototypeOf(obj);
}
}

var hasKey = obj[canSymbol.for("can.hasKey")];
if(hasKey) {
return hasKey.call(obj, key);
Expand Down

0 comments on commit 4754791

Please sign in to comment.