Skip to content

Commit

Permalink
Merge pull request #311 from flightjs/get_enumerable_property
Browse files Browse the repository at this point in the history
Get enumerable property
  • Loading branch information
danwrong committed Sep 16, 2014
2 parents 6958b42 + e954ef3 commit 72d73d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
7 changes: 4 additions & 3 deletions doc/utils_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ The handlers are lazily resolved when the event is fired.

Detect if an object is a DOM node.

<a name="utils.isEnumerable"></a>
## utils.isEnumerable(obj, property)
<a name="utils.getEnumerableProperty"></a>
## 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

<a name="utils.merge"></a>
## utils.merge(obj1, obj2, ... [, deepClone])
Expand Down
8 changes: 5 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 22 additions & 3 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,6 @@ define(['lib/component', 'lib/utils', 'lib/debug'], function (defineComponent, u
expect(spy1).toHaveBeenCalled();
expect(spy2).not.toHaveBeenCalled();
});


});

describe('countThen()', function () {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 72d73d9

Please sign in to comment.