Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: thenables/thenify
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3.3.0
Choose a base ref
...
head repository: thenables/thenify
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3.3.1
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Jun 17, 2020

  1. fix: remove eval (#30)

    dead-horse authored Jun 17, 2020
    Copy the full SHA
    0d94a24 View commit details
  2. Release 3.3.1

    dead-horse committed Jun 17, 2020
    Copy the full SHA
    1d054b4 View commit details
Showing with 61 additions and 34 deletions.
  1. +6 −0 History.md
  2. +30 −33 index.js
  3. +1 −1 package.json
  4. +24 −0 test/test.js
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

3.3.1 / 2020-06-18
==================

**fixes**
* [[`0d94a24`](http://github.com/thenables/thenify/commit/0d94a24eb933bc835d568f3009f4d269c4c4c17a)] - fix: remove eval (#30) (Yiyu He <<dead_horse@qq.com>>)

3.3.0 / 2017-05-19
==================

63 changes: 30 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -7,33 +7,34 @@ module.exports = thenify
/**
* Turn async functions into promises
*
* @param {Function} $$__fn__$$
* @param {Function} fn
* @return {Function}
* @api public
*/

function thenify($$__fn__$$, options) {
assert(typeof $$__fn__$$ === 'function')
return eval(createWrapper($$__fn__$$.name, options))
function thenify(fn, options) {
assert(typeof fn === 'function')
return createWrapper(fn, options)
}

/**
* Turn async functions into promises and backward compatible with callback
*
* @param {Function} $$__fn__$$
* @param {Function} fn
* @return {Function}
* @api public
*/

thenify.withCallback = function ($$__fn__$$, options) {
assert(typeof $$__fn__$$ === 'function')
thenify.withCallback = function (fn, options) {
assert(typeof fn === 'function')
options = options || {}
options.withCallback = true
if (options.multiArgs === undefined) options.multiArgs = true
return eval(createWrapper($$__fn__$$.name, options))
return createWrapper(fn, options)
}

function createCallback(resolve, reject, multiArgs) {
// default to true
if (multiArgs === undefined) multiArgs = true
return function(err, value) {
if (err) return reject(err)
var length = arguments.length
@@ -52,29 +53,25 @@ function createCallback(resolve, reject, multiArgs) {
}
}

function createWrapper(name, options) {
name = (name || '').replace(/\s|bound(?!$)/g, '')
function createWrapper(fn, options) {
options = options || {}
// default to true
var multiArgs = options.multiArgs !== undefined ? options.multiArgs : true
multiArgs = 'var multiArgs = ' + JSON.stringify(multiArgs) + '\n'

var withCallback = options.withCallback ?
'var lastType = typeof arguments[len - 1]\n'
+ 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n'
: ''

return '(function ' + name + '() {\n'
+ 'var self = this\n'
+ 'var len = arguments.length\n'
+ multiArgs
+ withCallback
+ 'var args = new Array(len + 1)\n'
+ 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n'
+ 'var lastIndex = i\n'
+ 'return new Promise(function (resolve, reject) {\n'
+ 'args[lastIndex] = createCallback(resolve, reject, multiArgs)\n'
+ '$$__fn__$$.apply(self, args)\n'
+ '})\n'
+ '})'
var name = fn.name;
name = (name || '').replace(/\s|bound(?!$)/g, '')
var newFn = function () {
var self = this
var len = arguments.length
if (options.withCallback) {
var lastType = typeof arguments[len - 1]
if (lastType === 'function') return fn.apply(self, arguments)
}
var args = new Array(len + 1)
for (var i = 0; i < len; ++i) args[i] = arguments[i]
var lastIndex = i
return new Promise(function (resolve, reject) {
args[lastIndex] = createCallback(resolve, reject, options.multiArgs)
fn.apply(self, args)
})
}
Object.defineProperty(newFn, 'name', { value: name })
return newFn
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "thenify",
"description": "Promisify a callback-based function",
"version": "3.3.0",
"version": "3.3.1",
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
"license": "MIT",
"repository": "thenables/thenify",
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -69,3 +69,27 @@ it('fn(..args, callback())', function () {
assert.deepEqual(values, [1, 2, 3])
})
})

it('unicode function name', function () {
function 你好$hello_123(a, b, c, cb) {
cb(null, a, b, c)
}
var wrapper = thenify(你好$hello_123)
assert.equal(wrapper.name, '你好$hello_123')
wrapper(1, 2, 3).then(function (values) {
assert.deepEqual(values, [1, 2, 3])
})
})

it('invalid function name', function () {
function fn(a, b, c, cb) {
cb(null, a, b, c)
}

Object.defineProperty(fn, 'name', { value: 'fake(){a.b;})();(function(){//' })
var wrapper = thenify(fn)
assert.equal(wrapper.name, fn.name)
wrapper(1, 2, 3).then(function (values) {
assert.deepEqual(values, [1, 2, 3])
})
})