From f9df2823722f55d40713f3fc3b5b0718dba0f3f4 Mon Sep 17 00:00:00 2001 From: angus croll Date: Mon, 15 Sep 2014 13:57:04 -0700 Subject: [PATCH 1/2] create obj.propertyIsEnumerable, remove isEnumebrable, add docs --- doc/utils_api.md | 7 ++++--- lib/utils.js | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/utils_api.md b/doc/utils_api.md index b99df776..b053a842 100644 --- a/doc/utils_api.md +++ b/doc/utils_api.md @@ -40,10 +40,11 @@ The handlers are lazily resolved when the event is fired. Detect if an object is a DOM node. - -## utils.isEnumerable(obj, property) + +## utils.getEnumerableProperty(obj, key) -Detect is a property of an object is enumerable. +If obj.key points to an enumerable property, return its value +If obj.key points to a non-enumerable property, return undefined ## utils.merge(obj1, obj2, ... [, deepClone]) diff --git a/lib/utils.js b/lib/utils.js index 476a0e5b..3b10b574 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -10,7 +10,7 @@ define( var DEFAULT_INTERVAL = 100; function canWriteProtect() { - var writeProtectSupported = debug.enabled && !utils.isEnumerable(Object, 'getOwnPropertyDescriptor'); + var writeProtectSupported = debug.enabled && !Object.propertyIsEnumerable('getOwnPropertyDescriptor'); if (writeProtectSupported) { //IE8 getOwnPropertyDescriptor is built-in but throws exeption on non DOM objects try { @@ -123,8 +123,10 @@ define( return base; }, - isEnumerable: function(obj, property) { - return Object.keys(obj).indexOf(property) > -1; + // If obj.key points to an enumerable property, return its value + // If obj.key points to a non-enumerable property, return undefined + getEnumerableProperty: function(obj, key) { + return obj.propertyIsEnumerable(key) ? obj[key] : undefined; }, // build a function from other function(s) From e954ef3ba8670896b62505daebe75c353dc620c5 Mon Sep 17 00:00:00 2001 From: angus croll Date: Mon, 15 Sep 2014 14:19:02 -0700 Subject: [PATCH 2/2] add test for getEnumerableProperty --- test/spec/utils_spec.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/test/spec/utils_spec.js b/test/spec/utils_spec.js index ad6a95bc..6e15e383 100644 --- a/test/spec/utils_spec.js +++ b/test/spec/utils_spec.js @@ -317,8 +317,6 @@ define(['lib/component', 'lib/utils', 'lib/debug'], function (defineComponent, u expect(spy1).toHaveBeenCalled(); expect(spy2).not.toHaveBeenCalled(); }); - - }); describe('countThen()', function () { @@ -359,7 +357,7 @@ define(['lib/component', 'lib/utils', 'lib/debug'], function (defineComponent, u }); }); - describe('property locking', function() { + describe('property locking', function() { beforeEach(function () { debug.enable(true); }); @@ -402,4 +400,25 @@ define(['lib/component', 'lib/utils', 'lib/debug'], function (defineComponent, u }); }); }); + + describe('getEnumerableProperty()', function() { + var obj; + + beforeEach(function () { + obj = { + custom: 123 + }; + }); + + it('should return custom values', function() { + expect(utils.getEnumerableProperty(obj, 'custom')).toBe(123); + }); + it('should not return native values', function() { + expect(utils.getEnumerableProperty(obj, 'toString')).toBe(undefined); + }); + it('should return shadowed native values', function() { + obj.toString = function() {}; + expect(typeof utils.getEnumerableProperty(obj, 'toString')).toBe('function'); + }); + }); });