Comparing version 2.2.0 to 2.3.0
52
index.js
'use strict'; | ||
var pify = module.exports = function (fn, P, opts) { | ||
if (typeof P !== 'function') { | ||
opts = P; | ||
P = Promise; | ||
} | ||
opts = opts || {}; | ||
if (typeof fn !== 'function') { | ||
return P.reject(new TypeError('Expected a function')); | ||
} | ||
var processFn = function (fn, P, opts) { | ||
return function () { | ||
var that = this; | ||
var args = [].slice.call(arguments); | ||
var args = new Array(arguments.length); | ||
for (var i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
return new P(function (resolve, reject) { | ||
@@ -24,3 +17,9 @@ args.push(function (err, result) { | ||
} else if (opts.multiArgs) { | ||
resolve([].slice.call(arguments, 1)); | ||
var results = new Array(arguments.length - 1); | ||
for (var i = 1; i < arguments.length; i++) { | ||
results[i - 1] = arguments[i]; | ||
} | ||
resolve(results); | ||
} else { | ||
@@ -36,3 +35,3 @@ resolve(result); | ||
pify.all = function (obj, P, opts) { | ||
var pify = module.exports = function (obj, P, opts) { | ||
if (typeof P !== 'function') { | ||
@@ -44,16 +43,13 @@ opts = P; | ||
opts = opts || {}; | ||
opts.exclude = opts.exclude || [/.+Sync$/]; | ||
var filter = function (key) { | ||
if (opts.include) { | ||
return opts.include.indexOf(key) !== -1; | ||
} | ||
var match = function (pattern) { | ||
return typeof pattern === 'string' ? key === pattern : pattern.test(key); | ||
}; | ||
if (opts.exclude) { | ||
return opts.exclude.indexOf(key) === -1; | ||
} | ||
return true; | ||
return opts.include ? opts.include.some(match) : !opts.exclude.some(match); | ||
}; | ||
var ret = (typeof obj === 'function') ? function () { | ||
var ret = typeof obj === 'function' ? function () { | ||
if (opts.excludeMain) { | ||
@@ -63,3 +59,3 @@ return obj.apply(this, arguments); | ||
return pify(obj, P, opts).apply(this, arguments); | ||
return processFn(obj, P, opts).apply(this, arguments); | ||
} : {}; | ||
@@ -69,5 +65,9 @@ | ||
var x = obj[key]; | ||
ret[key] = (typeof x === 'function') && filter(key) ? pify(x, P, opts) : x; | ||
ret[key] = typeof x === 'function' && filter(key) ? processFn(x, P, opts) : x; | ||
return ret; | ||
}, ret); | ||
}; | ||
pify.all = pify; |
{ | ||
"name": "pify", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Promisify a callback-style function", | ||
@@ -16,3 +16,4 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && npm run optimization-test", | ||
"optimization-test": "node --allow-natives-syntax optimization-test.js" | ||
}, | ||
@@ -45,4 +46,5 @@ "files": [ | ||
"pinkie-promise": "^1.0.0", | ||
"v8-natives": "0.0.2", | ||
"xo": "*" | ||
} | ||
} |
@@ -19,2 +19,4 @@ # pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify) | ||
// promisify a single function | ||
pify(fs.readFile)('package.json', 'utf8').then(data => { | ||
@@ -25,6 +27,5 @@ console.log(JSON.parse(data).name); | ||
// promisify all methods in a module | ||
const promiseFs = pify.all(fs); | ||
// or promisify all methods in a module | ||
promiseFs.readFile('package.json', 'utf8').then(data => { | ||
pify(fs).readFile('package.json', 'utf8').then(data => { | ||
console.log(JSON.parse(data).name); | ||
@@ -40,20 +41,10 @@ //=> 'pify' | ||
Returns a promise wrapped version of the supplied function. | ||
Returns a promise wrapped version of the supplied function or module. | ||
#### input | ||
Type: `function` | ||
Type: `function`, `object` | ||
Callback-style function. | ||
Callback-style function or module whose methods you want to promisify. | ||
### pify.all(module, [promiseModule], [options]) | ||
Returns a version of the module with all its methods promisified. | ||
#### module | ||
Type: `object` | ||
Module whose methods you want to promisify. | ||
#### promiseModule | ||
@@ -80,3 +71,3 @@ | ||
pify(request, {multiArgs: true})('http://sindresorhus.com').then(result => { | ||
pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => { | ||
const [httpResponse, body] = result; | ||
@@ -88,16 +79,13 @@ }); | ||
Type: `array` | ||
Type: `array` of (`string`|`regex`) | ||
*Works for `pify.all()` only.* | ||
Methods in a module to promisify. Remaining methods will be left untouched. | ||
Pick which methods in a module to promisify. Remaining methods will be left untouched. | ||
##### exclude | ||
Type: `array` | ||
Type: `array` of (`string`|`regex`) | ||
Default: `[/.+Sync$/]` | ||
*Works for `pify.all()` only.* | ||
Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default. | ||
Pick which methods in a module **not** to promisify. | ||
##### excludeMain | ||
@@ -108,6 +96,4 @@ | ||
*Works for `pify.all()` only.* | ||
By default, if given module is a function itself, this function will be promisified. Turn this option on if you want to promisify only methods of the module. | ||
By default, if given `module` is a function itself, this function will be promisified. Turn this option on if you want to promisify only methods of the module. | ||
```js | ||
@@ -127,3 +113,3 @@ const pify = require('pify'); | ||
// promisify methods but not fn() | ||
const promiseFn = pify.all(fn, {excludeMain: true}); | ||
const promiseFn = pify(fn, {excludeMain: true}); | ||
@@ -130,0 +116,0 @@ if (promiseFn()) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6020
4
52
120