From 523bad995294079f19b8d66e4d92aecaca1504e7 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 2 May 2018 11:33:15 +0700 Subject: [PATCH] Require Node.js 6 --- .gitignore | 1 + .npmrc | 1 + .travis.yml | 3 +- index.js | 69 ++++++++++++++---------------------- package.json | 98 ++++++++++++++++++++++++++-------------------------- readme.md | 2 +- 6 files changed, 80 insertions(+), 94 deletions(-) create mode 100644 .npmrc diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index 97519af..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: + - '10' + - '8' - '6' - - '4' diff --git a/index.js b/index.js index 2eff0bb..767bc91 100644 --- a/index.js +++ b/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); } @@ -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; diff --git a/package.json b/package.json index 468d857..4353ee1 100644 --- a/package.json +++ b/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": "*" + } } diff --git a/readme.md b/readme.md index 376ca4e..d19fc65 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ ## Install ``` -$ npm install --save pify +$ npm install pify ```