Skip to content

Commit

Permalink
Merge pull request #2 from zaboco/master
Browse files Browse the repository at this point in the history
Does not try to wrap getters & setters
  • Loading branch information
danielstjules committed Dec 2, 2015
2 parents 1f07a38 + 8d53af2 commit f6853e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function validateMethodNames(methodNames) {
}

function wrapFunctions(target, methodNames) {
Object.getOwnPropertyNames(target).forEach(function(key) {
_actualMethodKeys(target).forEach(function(key) {
let constructor = target[key].constructor.name;

if (methodNames) {
Expand All @@ -78,8 +78,6 @@ function wrapFunctions(target, methodNames) {
return;
}

if (typeof target[key] !== 'function') return;

if (target[key].constructor.name === 'GeneratorFunction') {
target[key] = Promise.coroutine(target[key]);
} else {
Expand All @@ -88,6 +86,15 @@ function wrapFunctions(target, methodNames) {
});
}

function _actualMethodKeys(target) {
return Object.getOwnPropertyNames(target)
.filter(key => {
var propertyDescriptor = Object.getOwnPropertyDescriptor(target, key);
return !propertyDescriptor.get && !propertyDescriptor.set;
})
.filter(key => typeof target[key] === 'function');
}

module.exports = {
wrap,
wrapStaticMethods,
Expand Down
10 changes: 9 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class FakeDataStore {
return keys;
}

get size() {
return this.store.size;
}

getAsync(key) {
let val = this.store.get(key);
return Promise.resolve(val);
Expand All @@ -54,8 +58,12 @@ describe('async-class', function() {
});

describe('wrap', function() {
it('does not modify getters', function() {
expect(dataStore.size).to.eql(0);
});

it('only modifies properties that are functions', function() {
expect(dataStore.store).to.be.instanceOf(Map)
expect(dataStore.store).to.be.instanceOf(Map);
});

it('does not wrap functions that do not end with Async', function() {
Expand Down

0 comments on commit f6853e1

Please sign in to comment.