From 2c301fac0a9ad6ffc9cb8b32872e000cfab06b36 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Sun, 27 Mar 2016 16:05:55 +0200 Subject: [PATCH] ignore non-enumerable properties - fixes #23 --- index.js | 5 +++++ test.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/index.js b/index.js index 1bc214b..fcd5816 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,11 @@ module.exports.get = function (obj, path) { var pathArr = getPathSegments(path); for (var i = 0; i < pathArr.length; i++) { + var descriptor = Object.getOwnPropertyDescriptor(obj, pathArr[i]) || Object.getOwnPropertyDescriptor(Object.prototype, pathArr[i]); + if (descriptor && !descriptor.enumerable) { + return; + } + obj = obj[pathArr[i]]; if (obj === undefined) { diff --git a/test.js b/test.js index 8fa49d3..6d4e061 100644 --- a/test.js +++ b/test.js @@ -13,6 +13,11 @@ test('get', t => { t.is(m.get({foo: {bar: {baz: null}}}, 'foo.bar.baz'), null); t.is(m.get({foo: {bar: 'a'}}, 'foo.fake.fake2'), undefined); + const f2 = {}; + Object.defineProperty(f2, 'foo', {value: 'bar', enumerable: false}); + t.is(m.get(f2, 'foo'), undefined); + t.is(m.get({}, 'hasOwnProperty'), undefined); + function fn() {} fn.foo = {bar: 1}; t.is(m.get(fn), fn);