Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 2, 2018
1 parent 9ef944c commit 523bad9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
69 changes: 26 additions & 43 deletions index.js
@@ -1,37 +1,26 @@
'use strict';

const processFn = (fn, opts) => function () {
const P = opts.promiseModule;
const args = new Array(arguments.length);

for (let i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
const processFn = (fn, options) => function (...args) {
const P = options.promiseModule;

return new P((resolve, reject) => {
if (opts.multiArgs) {
args.push(function (err) {
const results = new Array(arguments.length - 1);

for (let i = 0; i < arguments.length; i++) {
results[i] = arguments[i];
}

if (opts.errorFirst) {
if (err) {
reject(results);
if (options.multiArgs) {
args.push((...result) => {
if (options.errorFirst) {
if (result[0]) {
reject(result);
} else {
results.shift();
resolve(results);
result.shift();
resolve(result);
}
} else {
resolve(results);
resolve(result);
}
});
} else if (opts.errorFirst) {
args.push((err, result) => {
if (err) {
reject(err);
} else if (options.errorFirst) {
args.push((error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
Expand All @@ -44,39 +33,33 @@ const processFn = (fn, opts) => function () {
});
};

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 obj;
if (objType === 'string' || objType === 'undefined' || obj === null) {
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${obj === null ? 'null' : objType}\``);
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 (objType === 'function') {
ret = function () {
if (opts.excludeMain) {
return obj.apply(this, arguments);
}

return processFn(obj, opts).apply(this, arguments);
};
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;
}

return ret;
Expand Down
98 changes: 49 additions & 49 deletions package.json
@@ -1,51 +1,51 @@
{
"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": "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": ">=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": "*"
}
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -6,7 +6,7 @@
## Install

```
$ npm install --save pify
$ npm install pify
```


Expand Down

0 comments on commit 523bad9

Please sign in to comment.