Skip to content

Commit

Permalink
fix: allow excluding accessor methods (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 4, 2020
1 parent 90cc4c4 commit 114d8bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Expand Up @@ -137,10 +137,10 @@ export function promisifyAll(Class: Function, options?: PromisifyAllOptions) {
const methods = ownPropertyNames.filter(methodName => {
// clang-format off
return (
!exclude.includes(methodName) &&
typeof Class.prototype[methodName] === 'function' && // is it a function?
!/(^_|(Stream|_)|promise$)|^constructor$/.test(methodName) && // is it promisable?
exclude.indexOf(methodName) === -1
); // is it blacklisted?
!/(^_|(Stream|_)|promise$)|^constructor$/.test(methodName) // is it promisable?
);
// clang-format on
});

Expand Down Expand Up @@ -203,10 +203,10 @@ export function callbackifyAll(
const methods = ownPropertyNames.filter(methodName => {
// clang-format off
return (
!exclude.includes(methodName) &&
typeof Class.prototype[methodName] === 'function' && // is it a function?
!/^_|(Stream|_)|^constructor$/.test(methodName) && // is it callbackifyable?
exclude.indexOf(methodName) === -1
); // is it blacklisted?
!/^_|(Stream|_)|^constructor$/.test(methodName) // is it callbackifyable?
);
// clang-format on
});

Expand Down
32 changes: 32 additions & 0 deletions test/index.ts
Expand Up @@ -98,6 +98,22 @@ describe('promisifyAll', () => {
assert(FakeClass2.prototype.method.promisified_);
});

it('should honor excluded properties first', done => {
function FakeClass2() {}
Object.defineProperty(FakeClass2.prototype, 'method', {
get: () => {
done(new Error('Accessor method should not be called.'));
return {};
},
});
assert.doesNotThrow(() => {
util.promisifyAll(FakeClass2, {
exclude: ['method'],
});
done();
});
});

it('should pass the options object to promisify', done => {
const fakeOptions = {
a: 'a',
Expand Down Expand Up @@ -295,6 +311,22 @@ describe('callbackifyAll', () => {
assert.strictEqual(FakeClass2.prototype.methodSync, noop);
});

it('should honor excluded properties first', done => {
function FakeClass2() {}
Object.defineProperty(FakeClass2.prototype, 'method', {
get: () => {
done(new Error('Accessor method should not be called.'));
return {};
},
});
assert.doesNotThrow(() => {
util.callbackifyAll(FakeClass2, {
exclude: ['method'],
});
done();
});
});

it('should not re-callbackify method', () => {
const method = FakeClass.prototype.methodName;
util.callbackifyAll(FakeClass);
Expand Down

0 comments on commit 114d8bc

Please sign in to comment.