From 1d75e397b467b7000c540458b4d00a6ccfd2cd83 Mon Sep 17 00:00:00 2001 From: Nickolay Ribal Date: Thu, 30 Jun 2016 19:07:46 +0300 Subject: [PATCH] Write failing test and fix accessing a property of an object with null prototype. --- index.js | 2 +- test.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ea684d3..11bbacd 100644 --- a/index.js +++ b/index.js @@ -79,7 +79,7 @@ }; function getShallowProperty(obj, prop) { - if(options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || obj.hasOwnProperty(prop)) { + if (options.includeInheritedProps || (typeof prop === 'number' && Array.isArray(obj)) || _hasOwnProperty.call(obj, prop)) { return obj[prop]; } } diff --git a/test.js b/test.js index a0edb06..fe20a10 100644 --- a/test.js +++ b/test.js @@ -102,6 +102,17 @@ describe('get', function() { expect(objectPath.get(undefined, 'test', 'a')).to.be.deep.equal('a'); }); + it( + 'should not fail on an object with a null prototype', + function assertSuccessForObjWithNullProto(){ + // TODO: verify this works on node 0.10 + var foo = 'FOO'; + var objWithNullProto = Object.create(null); + objWithNullProto.foo = foo; + expect(objectPath.get(objWithNullProto, 'foo')).to.equal(foo); + } + ); + it('should skip non own properties', function() { var Base = function(enabled){ }; Base.prototype = { @@ -772,7 +783,7 @@ describe('bind object', function () { }); }); -describe('Don\' access not own properties [default]', function () { +describe('Don\'t access not own properties [default]', function () { it('should not get a not own property', function() { var Obj = function() {}; Obj.prototype.notOwn = {a: 'a'};