Comparing version 3.0.0 to 4.0.0
78
index.js
'use strict'; | ||
const processFn = (fn, opts) => function () { | ||
const P = opts.promiseModule; | ||
const args = new Array(arguments.length); | ||
const processFn = (fn, options) => function (...args) { | ||
const P = options.promiseModule; | ||
for (let i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
return new P((resolve, reject) => { | ||
if (opts.errorFirst) { | ||
args.push(function (err, result) { | ||
if (opts.multiArgs) { | ||
const results = new Array(arguments.length - 1); | ||
for (let i = 1; i < arguments.length; i++) { | ||
results[i - 1] = arguments[i]; | ||
} | ||
if (err) { | ||
results.unshift(err); | ||
reject(results); | ||
if (options.multiArgs) { | ||
args.push((...result) => { | ||
if (options.errorFirst) { | ||
if (result[0]) { | ||
reject(result); | ||
} else { | ||
resolve(results); | ||
result.shift(); | ||
resolve(result); | ||
} | ||
} else if (err) { | ||
reject(err); | ||
} else { | ||
@@ -33,12 +20,6 @@ resolve(result); | ||
}); | ||
} else { | ||
args.push(function (result) { | ||
if (opts.multiArgs) { | ||
const results = new Array(arguments.length - 1); | ||
for (let i = 0; i < arguments.length; i++) { | ||
results[i] = arguments[i]; | ||
} | ||
resolve(results); | ||
} else if (options.errorFirst) { | ||
args.push((error, result) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
@@ -48,2 +29,4 @@ resolve(result); | ||
}); | ||
} else { | ||
args.push(resolve); | ||
} | ||
@@ -55,30 +38,29 @@ | ||
module.exports = (obj, opts) => { | ||
opts = Object.assign({ | ||
module.exports = (input, options) => { | ||
options = Object.assign({ | ||
exclude: [/.+(Sync|Stream)$/], | ||
errorFirst: true, | ||
promiseModule: Promise | ||
}, opts); | ||
}, options); | ||
const objType = typeof input; | ||
if (!(input !== null && (objType === 'object' || objType === 'function'))) { | ||
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objType}\``); | ||
} | ||
const filter = key => { | ||
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key); | ||
return opts.include ? opts.include.some(match) : !opts.exclude.some(match); | ||
return options.include ? options.include.some(match) : !options.exclude.some(match); | ||
}; | ||
let ret; | ||
if (typeof obj === 'function') { | ||
ret = function () { | ||
if (opts.excludeMain) { | ||
return obj.apply(this, arguments); | ||
} | ||
return processFn(obj, opts).apply(this, arguments); | ||
}; | ||
if (objType === 'function') { | ||
ret = (...args) => options.excludeMain ? input(...args) : processFn(input, options)(...args); | ||
} else { | ||
ret = Object.create(Object.getPrototypeOf(obj)); | ||
ret = Object.create(Object.getPrototypeOf(input)); | ||
} | ||
for (const key in obj) { // eslint-disable-line guard-for-in | ||
const x = obj[key]; | ||
ret[key] = typeof x === 'function' && filter(key) ? processFn(x, opts) : x; | ||
for (const key in input) { // eslint-disable-line guard-for-in | ||
const property = input[key]; | ||
ret[key] = typeof property === 'function' && filter(key) ? processFn(property, options) : property; | ||
} | ||
@@ -85,0 +67,0 @@ |
{ | ||
"name": "pify", | ||
"version": "3.0.0", | ||
"description": "Promisify a callback-style function", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pify", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && npm run optimization-test", | ||
"optimization-test": "node --allow-natives-syntax optimization-test.js" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"promises", | ||
"promisify", | ||
"all", | ||
"denodify", | ||
"denodeify", | ||
"callback", | ||
"cb", | ||
"node", | ||
"then", | ||
"thenify", | ||
"convert", | ||
"transform", | ||
"wrap", | ||
"wrapper", | ||
"bind", | ||
"to", | ||
"async", | ||
"await", | ||
"es2015", | ||
"bluebird" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"pinkie-promise": "^2.0.0", | ||
"v8-natives": "^1.0.0", | ||
"xo": "*" | ||
} | ||
"name": "pify", | ||
"version": "4.0.0", | ||
"description": "Promisify a callback-style function", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pify", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava", | ||
"optimization-test": "node --allow-natives-syntax optimization-test.js" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"promises", | ||
"promisify", | ||
"all", | ||
"denodify", | ||
"denodeify", | ||
"callback", | ||
"cb", | ||
"node", | ||
"then", | ||
"thenify", | ||
"convert", | ||
"transform", | ||
"wrap", | ||
"wrapper", | ||
"bind", | ||
"to", | ||
"async", | ||
"await", | ||
"es2015", | ||
"bluebird" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"pinkie-promise": "^2.0.0", | ||
"v8-natives": "^1.1.0", | ||
"xo": "*" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify) | ||
``` | ||
$ npm install --save pify | ||
$ npm install pify | ||
``` | ||
@@ -12,0 +12,0 @@ |
6765
57