[Pkg-javascript-commits] [node-pify] 01/04: New upstream version 3.0.0

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Fri Oct 20 08:22:31 UTC 2017


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-pify.

commit 0cf4ab1cae7b4b43288e39b15c81bbee9daff387
Author: Pirate Praveen <praveen at debian.org>
Date:   Fri Oct 20 11:49:21 2017 +0530

    New upstream version 3.0.0
---
 .editorconfig        |   5 +-
 .gitattributes       |   1 +
 .travis.yml          |   5 +-
 index.js             | 102 +++++++++++--------
 license              |  20 +---
 optimization-test.js |  38 +++----
 package.json         |  13 ++-
 readme.md            |  62 +++++++-----
 test.js              | 276 +++++++++++++++++++++++++++++++++++----------------
 9 files changed, 323 insertions(+), 199 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 8f9d77e..1c6314a 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -7,9 +7,6 @@ charset = utf-8
 trim_trailing_whitespace = true
 insert_final_newline = true
 
-[{package.json,*.yml}]
+[*.yml]
 indent_style = space
 indent_size = 2
-
-[*.md]
-trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
index 176a458..391f0a4 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1 +1,2 @@
 * text=auto
+*.js text eol=lf
diff --git a/.travis.yml b/.travis.yml
index 2a2f1c6..97519af 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,4 @@
 language: node_js
 node_js:
-  - 'stable'
-  - '0.12'
-  - '0.10'
+  - '6'
+  - '4'
diff --git a/index.js b/index.js
index 7c720eb..1dee43a 100644
--- a/index.js
+++ b/index.js
@@ -1,68 +1,84 @@
 'use strict';
 
-var processFn = function (fn, P, opts) {
-	return function () {
-		var that = this;
-		var args = new Array(arguments.length);
+const processFn = (fn, opts) => function () {
+	const P = opts.promiseModule;
+	const args = new Array(arguments.length);
 
-		for (var i = 0; i < arguments.length; i++) {
-			args[i] = arguments[i];
-		}
+	for (let i = 0; i < arguments.length; i++) {
+		args[i] = arguments[i];
+	}
 
-		return new P(function (resolve, reject) {
+	return new P((resolve, reject) => {
+		if (opts.errorFirst) {
 			args.push(function (err, result) {
-				if (err) {
-					reject(err);
-				} else if (opts.multiArgs) {
-					var results = new Array(arguments.length - 1);
+				if (opts.multiArgs) {
+					const results = new Array(arguments.length - 1);
 
-					for (var i = 1; i < arguments.length; i++) {
+					for (let i = 1; i < arguments.length; i++) {
 						results[i - 1] = arguments[i];
 					}
 
-					resolve(results);
+					if (err) {
+						results.unshift(err);
+						reject(results);
+					} else {
+						resolve(results);
+					}
+				} else if (err) {
+					reject(err);
 				} else {
 					resolve(result);
 				}
 			});
+		} else {
+			args.push(function (result) {
+				if (opts.multiArgs) {
+					const results = new Array(arguments.length - 1);
 
-			fn.apply(that, args);
-		});
-	};
-};
+					for (let i = 0; i < arguments.length; i++) {
+						results[i] = arguments[i];
+					}
 
-var pify = module.exports = function (obj, P, opts) {
-	if (typeof P !== 'function') {
-		opts = P;
-		P = Promise;
-	}
+					resolve(results);
+				} else {
+					resolve(result);
+				}
+			});
+		}
 
-	opts = opts || {};
-	opts.exclude = opts.exclude || [/.+Sync$/];
+		fn.apply(this, args);
+	});
+};
 
-	var filter = function (key) {
-		var match = function (pattern) {
-			return typeof pattern === 'string' ? key === pattern : pattern.test(key);
-		};
+module.exports = (obj, opts) => {
+	opts = Object.assign({
+		exclude: [/.+(Sync|Stream)$/],
+		errorFirst: true,
+		promiseModule: Promise
+	}, opts);
 
+	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);
 	};
 
-	var ret = typeof obj === 'function' ? function () {
-		if (opts.excludeMain) {
-			return obj.apply(this, arguments);
-		}
-
-		return processFn(obj, P, opts).apply(this, arguments);
-	} : {};
+	let ret;
+	if (typeof obj === 'function') {
+		ret = function () {
+			if (opts.excludeMain) {
+				return obj.apply(this, arguments);
+			}
 
-	return Object.keys(obj).reduce(function (ret, key) {
-		var x = obj[key];
+			return processFn(obj, opts).apply(this, arguments);
+		};
+	} else {
+		ret = Object.create(Object.getPrototypeOf(obj));
+	}
 
-		ret[key] = typeof x === 'function' && filter(key) ? processFn(x, P, opts) : x;
+	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;
+	}
 
-		return ret;
-	}, ret);
+	return ret;
 };
-
-pify.all = pify;
diff --git a/license b/license
index 654d0bf..e7af2f7 100644
--- a/license
+++ b/license
@@ -1,21 +1,9 @@
-The MIT License (MIT)
+MIT License
 
 Copyright (c) Sindre Sorhus <sindresorhus at gmail.com> (sindresorhus.com)
 
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/optimization-test.js b/optimization-test.js
index c883b7a..72b71f9 100644
--- a/optimization-test.js
+++ b/optimization-test.js
@@ -1,44 +1,44 @@
 /* eslint-disable no-fallthrough */
 'use strict';
-var assert = require('assert');
-var Promise = require('pinkie-promise');
-var v8 = require('v8-natives');
-var fn = require('./');
+const assert = require('assert');
+const v8 = require('v8-natives');
+const pify = require('.');
 
 function assertOptimized(fn, name) {
-	var status = v8.getOptimizationStatus(fn);
+	const status = v8.getOptimizationStatus(fn);
 
 	switch (status) {
 		case 1:
-			// fn is optimized
+			// `fn` is optimized
+			console.log('pify is optimized');
 			return;
 		case 2:
-			assert(false, name + ' is not optimized (' + status + ')');
+			assert(false, `${name} is not optimized (${status})`);
 		case 3:
-			// fn is always optimized
+			// `fn` is always optimized
 			return;
 		case 4:
-			assert(false, name + ' is never optimized (' + status + ')');
+			assert(false, `${name} is never optimized (${status})`);
 		case 6:
-			assert(false, name + ' is maybe deoptimized (' + status + ')');
+			assert(false, `${name} is maybe deoptimized (${status})`);
 		default:
-			assert(false, 'unknown OptimizationStatus: ' + status + ' (' + name + ')');
+			assert(false, `unknown OptimizationStatus: ${status} (${name})`);
 	}
 }
 
-var sut = fn({
-	unicorn: function (cb) {
+const sut = pify({
+	unicorn: cb => {
 		cb(null, 'unicorn');
 	}
-}, Promise);
+});
 
-sut.unicorn().then(function () {
+sut.unicorn().then(() => {
 	v8.optimizeFunctionOnNextCall(sut.unicorn);
 
-	return sut.unicorn().then(function () {
+	return sut.unicorn().then(() => {
 		assertOptimized(sut.unicorn, 'unicorn');
 	});
-}).catch(function (err) {
-	console.log(err.stack);
-	process.exit(1);
+}).catch(err => {
+	console.error(err.stack);
+	process.exit(1); // eslint-disable-line unicorn/no-process-exit
 });
diff --git a/package.json b/package.json
index 311d198..468d857 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "pify",
-  "version": "2.3.0",
+  "version": "3.0.0",
   "description": "Promisify a callback-style function",
   "license": "MIT",
   "repository": "sindresorhus/pify",
@@ -10,7 +10,7 @@
     "url": "sindresorhus.com"
   },
   "engines": {
-    "node": ">=0.10.0"
+    "node": ">=4"
   },
   "scripts": {
     "test": "xo && ava && npm run optimization-test",
@@ -23,6 +23,7 @@
     "promise",
     "promises",
     "promisify",
+    "all",
     "denodify",
     "denodeify",
     "callback",
@@ -37,12 +38,14 @@
     "bind",
     "to",
     "async",
-    "es2015"
+    "await",
+    "es2015",
+    "bluebird"
   ],
   "devDependencies": {
     "ava": "*",
-    "pinkie-promise": "^1.0.0",
-    "v8-natives": "0.0.2",
+    "pinkie-promise": "^2.0.0",
+    "v8-natives": "^1.0.0",
     "xo": "*"
   }
 }
diff --git a/readme.md b/readme.md
index c79ca8b..376ca4e 100644
--- a/readme.md
+++ b/readme.md
@@ -16,15 +16,13 @@ $ npm install --save pify
 const fs = require('fs');
 const pify = require('pify');
 
-// promisify a single function
-
+// Promisify a single function
 pify(fs.readFile)('package.json', 'utf8').then(data => {
 	console.log(JSON.parse(data).name);
 	//=> 'pify'
 });
 
-// or promisify all methods in a module
-
+// Promisify all methods in a module
 pify(fs).readFile('package.json', 'utf8').then(data => {
 	console.log(JSON.parse(data).name);
 	//=> 'pify'
@@ -34,32 +32,24 @@ pify(fs).readFile('package.json', 'utf8').then(data => {
 
 ## API
 
-### pify(input, [promiseModule], [options])
+### pify(input, [options])
 
-Returns a promise wrapped version of the supplied function or module.
+Returns a `Promise` wrapped version of the supplied function or module.
 
 #### input
 
-Type: `function`, `object`
+Type: `Function` `Object`
 
 Callback-style function or module whose methods you want to promisify.
 
-#### promiseModule
-
-Type: `function`
-
-Custom promise module to use instead of the native one.
-
-Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
-
 #### options
 
 ##### multiArgs
 
-Type: `boolean`  
+Type: `boolean`<br>
 Default: `false`
 
-By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument.
+By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error.
 
 ```js
 const request = require('request');
@@ -72,23 +62,23 @@ pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
 
 ##### include
 
-Type: `array` of (`string`|`regex`)
+Type: `string[]` `RegExp[]`
 
 Methods in a module to promisify. Remaining methods will be left untouched.
 
 ##### exclude
 
-Type: `array` of (`string`|`regex`)  
-Default: `[/.+Sync$/]`
+Type: `string[]` `RegExp[]`<br>
+Default: `[/.+(Sync|Stream)$/]`
 
 Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
 
 ##### excludeMain
 
-Type: `boolean`  
+Type: `boolean`<br>
 Default: `false`
 
-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.
+If given module is a function itself, it will be promisified. Turn this option on if you want to promisify only methods of the module.
 
 ```js
 const pify = require('pify');
@@ -99,11 +89,11 @@ function fn() {
 
 fn.method = (data, callback) => {
 	setImmediate(() => {
-		callback(data, null);
+		callback(null, data);
 	});
 };
 
-// promisify methods but not fn()
+// Promisify methods but not `fn()`
 const promiseFn = pify(fn, {excludeMain: true});
 
 if (promiseFn()) {
@@ -113,7 +103,29 @@ if (promiseFn()) {
 }
 ```
 
+##### errorFirst
+
+Type: `boolean`<br>
+Default: `true`
+
+Whether the callback has an error as the first argument. You'll want to set this to `false` if you're dealing with an API that doesn't have an error as the first argument, like `fs.exists()`, some browser APIs, Chrome Extension APIs, etc.
+
+##### promiseModule
+
+Type: `Function`
+
+Custom promise module to use instead of the native one.
+
+Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
+
+
+## Related
+
+- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
+- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
+- [More…](https://github.com/sindresorhus/promise-fun)
+
 
 ## License
 
-MIT © [Sindre Sorhus](http://sindresorhus.com)
+MIT © [Sindre Sorhus](https://sindresorhus.com)
diff --git a/test.js b/test.js
index 183bd1a..094b73a 100644
--- a/test.js
+++ b/test.js
@@ -1,128 +1,122 @@
-'use strict';
-var fs = require('fs');
-var test = require('ava');
-var pinkiePromise = global.Promise = require('pinkie-promise');
-var fn = require('./');
-
-function fixture(cb) {
-	setImmediate(function () {
-		cb(null, 'unicorn');
-	});
-}
-
-function fixture2(x, cb) {
-	setImmediate(function () {
-		cb(null, x);
-	});
-}
-
-function fixture3(cb) {
-	setImmediate(function () {
-		cb(null, 'unicorn', 'rainbow');
-	});
-}
+import util from 'util';
+import fs from 'fs';
+import stream from 'stream';
+import test from 'ava';
+import pinkiePromise from 'pinkie-promise';
+import m from '.';
 
-function fixture4(cb) {
-	setImmediate(function () {
-		cb(null, 'unicorn');
-	});
+const fixture = cb => setImmediate(() => cb(null, 'unicorn'));
+const fixture1 = cb => setImmediate(() => cb('error', 'unicorn', 'rainbow'));
+const fixture2 = (x, cb) => setImmediate(() => cb(null, x));
+const fixture3 = cb => setImmediate(() => cb(null, 'unicorn', 'rainbow'));
+const fixture4 = cb => setImmediate(() => {
+	cb(null, 'unicorn');
 	return 'rainbow';
-}
+});
 
-fixture4.meow = function (cb) {
-	setImmediate(function () {
+fixture4.meow = cb => {
+	setImmediate(() => {
 		cb(null, 'unicorn');
 	});
 };
 
-function fixture5() {
-	return 'rainbow';
-}
+const fixture5 = () => 'rainbow';
 
-var fixtureModule = {
+const fixtureModule = {
 	method1: fixture,
 	method2: fixture,
 	method3: fixture5
 };
 
-test('main', function (t) {
-	t.is(typeof fn(fixture)().then, 'function');
+function FixtureGrandparent() {}
+FixtureGrandparent.prototype.grandparentMethod1 = fixture;
+FixtureGrandparent.prototype.overriddenMethod1 = fixture;
+function FixtureParent() {}
+util.inherits(FixtureParent, FixtureGrandparent);
+FixtureParent.prototype.parentMethod1 = fixture;
+FixtureParent.prototype.overriddenMethod1 = fixture2;
+FixtureParent.prototype.overriddenValue1 = 2;
+function FixtureClass() {
+	this.instanceMethod1 = fixture;
+	this.instanceValue1 = 72;
+}
+util.inherits(FixtureClass, FixtureParent);
+FixtureClass.prototype.method1 = fixture;
+FixtureParent.prototype.overriddenValue1 = 4;
+FixtureClass.prototype.value1 = 'neo';
 
-	return fn(fixture)().then(function (data) {
-		t.is(data, 'unicorn');
-	});
+test('main', async t => {
+	t.is(typeof m(fixture)().then, 'function');
+	t.is(await m(fixture)(), 'unicorn');
 });
 
-test('pass argument', function (t) {
-	return fn(fixture2)('rainbow').then(function (data) {
-		t.is(data, 'rainbow');
-	});
+test('error', async t => {
+	t.is(await m(fixture1)().catch(err => err), 'error');
 });
 
-test('custom Promise module', function (t) {
-	return fn(fixture, pinkiePromise)().then(function (data) {
-		t.is(data, 'unicorn');
-	});
+test('pass argument', async t => {
+	t.is(await m(fixture2)('rainbow'), 'rainbow');
 });
 
-test('multiArgs option', function (t) {
-	return fn(fixture3, {multiArgs: true})().then(function (data) {
-		t.same(data, ['unicorn', 'rainbow']);
-	});
+test('custom Promise module', async t => {
+	t.is(await m(fixture, {promiseModule: pinkiePromise})(), 'unicorn');
 });
 
-test('wrap core method', function (t) {
-	return fn(fs.readFile)('package.json').then(function (data) {
-		t.is(JSON.parse(data).name, 'pify');
-	});
+test('multiArgs option', async t => {
+	t.deepEqual(await m(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']);
 });
 
-test('module support', function (t) {
-	return fn(fs).readFile('package.json').then(function (data) {
-		t.is(JSON.parse(data).name, 'pify');
-	});
+test('multiArgs option — rejection', async t => {
+	t.deepEqual(await m(fixture1, {multiArgs: true})().catch(err => err), ['error', 'unicorn', 'rainbow']);
+});
+
+test('wrap core method', async t => {
+	t.is(JSON.parse(await m(fs.readFile)('package.json')).name, 'pify');
+});
+
+test('module support', async t => {
+	t.is(JSON.parse(await m(fs).readFile('package.json')).name, 'pify');
+});
+
+test('module support - doesn\'t transform *Sync methods by default', t => {
+	t.is(JSON.parse(m(fs).readFileSync('package.json')).name, 'pify');
 });
 
-test('module support - doesn\'t transform *Sync methods by default', function (t) {
-	var data = fn(fs).readFileSync('package.json');
-	t.is(JSON.parse(data).name, 'pify');
-	t.end();
+test('module support - doesn\'t transform *Stream methods by default', t => {
+	t.true(m(fs).createReadStream('package.json') instanceof stream.Readable);
 });
 
-test('module support - preserves non-function members', function (t) {
-	var module = {
-		method: function () {},
+test('module support - preserves non-function members', t => {
+	const module = {
+		method: () => {},
 		nonMethod: 3
 	};
 
-	t.same(Object.keys(module), Object.keys(fn(module)));
-	t.end();
+	t.deepEqual(Object.keys(module), Object.keys(m(module)));
 });
 
-test('module support - transforms only members in opions.include', function (t) {
-	var pModule = fn(fixtureModule, {
+test('module support - transforms only members in options.include', t => {
+	const pModule = m(fixtureModule, {
 		include: ['method1', 'method2']
 	});
 
 	t.is(typeof pModule.method1().then, 'function');
 	t.is(typeof pModule.method2().then, 'function');
 	t.not(typeof pModule.method3().then, 'function');
-	t.end();
 });
 
-test('module support - doesn\'t transform members in opions.exclude', function (t) {
-	var pModule = fn(fixtureModule, {
+test('module support - doesn\'t transform members in options.exclude', t => {
+	const pModule = m(fixtureModule, {
 		exclude: ['method3']
 	});
 
 	t.is(typeof pModule.method1().then, 'function');
 	t.is(typeof pModule.method2().then, 'function');
 	t.not(typeof pModule.method3().then, 'function');
-	t.end();
 });
 
-test('module support - options.include over opions.exclude', function (t) {
-	var pModule = fn(fixtureModule, {
+test('module support - options.include over options.exclude', t => {
+	const pModule = m(fixtureModule, {
 		include: ['method1', 'method2'],
 		exclude: ['method2', 'method3']
 	});
@@ -130,23 +124,137 @@ test('module support - options.include over opions.exclude', function (t) {
 	t.is(typeof pModule.method1().then, 'function');
 	t.is(typeof pModule.method2().then, 'function');
 	t.not(typeof pModule.method3().then, 'function');
-	t.end();
 });
 
-test('module support — function modules', function (t) {
-	var pModule = fn(fixture4);
+test('module support — function modules', t => {
+	const pModule = m(fixture4);
 
 	t.is(typeof pModule().then, 'function');
 	t.is(typeof pModule.meow().then, 'function');
-	t.end();
 });
 
-test('module support — function modules exclusion', function (t) {
-	var pModule = fn(fixture4, {
+test('module support — function modules exclusion', t => {
+	const pModule = m(fixture4, {
 		excludeMain: true
 	});
 
 	t.is(typeof pModule.meow().then, 'function');
-	t.not(typeof pModule(function () {}).then, 'function');
-	t.end();
+	t.not(typeof pModule(() => {}).then, 'function');
+});
+
+test('`errorFirst` option', async t => {
+	const fixture = (foo, cb) => {
+		cb(foo);
+	};
+
+	t.is(await m(fixture, {errorFirst: false})('🦄'), '🦄');
+});
+
+test('`errorFirst` option and `multiArgs`', async t => {
+	const fixture = (foo, bar, cb) => {
+		cb(foo, bar);
+	};
+
+	t.deepEqual(await m(fixture, {
+		errorFirst: false,
+		multiArgs: true
+	})('🦄', '🌈'), ['🦄', '🌈']);
+});
+
+test('class support - creates a copy', async t => {
+	const obj = {
+		x: 'foo',
+		y(cb) {
+			setImmediate(() => {
+				cb(null, this.x);
+			});
+		}
+	};
+
+	const pified = m(obj, {bind: false});
+	obj.x = 'bar';
+
+	t.is(await pified.y(), 'foo');
+	t.is(pified.x, 'foo');
+});
+
+test('class support — transforms inherited methods', t => {
+	const instance = new FixtureClass();
+	const pInstance = m(instance);
+
+	const flattened = {};
+	for (let prot = instance; prot; prot = Object.getPrototypeOf(prot)) {
+		Object.assign(flattened, prot);
+	}
+
+	const keys = Object.keys(flattened);
+	keys.sort();
+	const pKeys = Object.keys(pInstance);
+	pKeys.sort();
+	t.deepEqual(keys, pKeys);
+
+	t.is(instance.value1, pInstance.value1);
+	t.is(typeof pInstance.instanceMethod1().then, 'function');
+	t.is(typeof pInstance.method1().then, 'function');
+	t.is(typeof pInstance.parentMethod1().then, 'function');
+	t.is(typeof pInstance.grandparentMethod1().then, 'function');
+});
+
+test('class support — preserves prototype', t => {
+	const instance = new FixtureClass();
+	const pInstance = m(instance);
+
+	t.true(pInstance instanceof FixtureClass);
+});
+
+test('class support — respects inheritance order', async t => {
+	const instance = new FixtureClass();
+	const pInstance = m(instance);
+
+	t.is(instance.overriddenValue1, pInstance.overriddenValue1);
+	t.is(await pInstance.overriddenMethod1('rainbow'), 'rainbow');
+});
+
+test('class support - transforms only members in options.include, copies all', t => {
+	const instance = new FixtureClass();
+	const pInstance = m(instance, {
+		include: ['parentMethod1']
+	});
+
+	const flattened = {};
+	for (let prot = instance; prot; prot = Object.getPrototypeOf(prot)) {
+		Object.assign(flattened, prot);
+	}
+
+	const keys = Object.keys(flattened);
+	keys.sort();
+	const pKeys = Object.keys(pInstance);
+	pKeys.sort();
+	t.deepEqual(keys, pKeys);
+
+	t.is(typeof pInstance.parentMethod1().then, 'function');
+	t.not(typeof pInstance.method1(() => {}).then, 'function');
+	t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
+});
+
+test('class support - doesn\'t transform members in options.exclude', t => {
+	const instance = new FixtureClass();
+	const pInstance = m(instance, {
+		exclude: ['grandparentMethod1']
+	});
+
+	t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
+	t.is(typeof pInstance.parentMethod1().then, 'function');
+});
+
+test('class support - options.include over options.exclude', t => {
+	const instance = new FixtureClass();
+	const pInstance = m(instance, {
+		include: ['method1', 'parentMethod1'],
+		exclude: ['parentMethod1', 'grandparentMethod1']
+	});
+
+	t.is(typeof pInstance.method1().then, 'function');
+	t.is(typeof pInstance.parentMethod1().then, 'function');
+	t.not(typeof pInstance.grandparentMethod1(() => {}).then, 'function');
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-pify.git



More information about the Pkg-javascript-commits mailing list