Skip to content

Commit

Permalink
fix: identifier of bound functions for thenify.withCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Oct 10, 2016
1 parent 58cbc49 commit acdcd59
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = thenify

function thenify($$__fn__$$) {
assert(typeof $$__fn__$$ === 'function')
return eval(createWrapper($$__fn__$$.name.replace(/\s|bound(?!$)/g,'')))
return eval(createWrapper($$__fn__$$.name))
}

/**
Expand Down Expand Up @@ -42,12 +42,13 @@ function createCallback(resolve, reject) {
}

function createWrapper(name, withCallback) {
name = (name || '').replace(/\s|bound(?!$)/g, '')
withCallback = withCallback ?
'var lastType = typeof arguments[len - 1]\n'
+ 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n'
: ''

return '(function ' + (name || '') + '() {\n'
return '(function ' + name + '() {\n'
+ 'var self = this\n'
+ 'var len = arguments.length\n'
+ withCallback
Expand Down
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ it('fn.name', function () {
it('fn.name (bound function)', function () {
function bound() {}
assert.equal('bound', thenify(bound).name)

assert.equal('bound', thenify.withCallback(bound).name)

var noname = (function () {}).bind(this)
assert.equal('', thenify(noname).name)
assert.equal('', thenify.withCallback(noname).name)
})

it('fn(callback(err))', function () {
Expand Down

0 comments on commit acdcd59

Please sign in to comment.