[Pkg-javascript-commits] [node-pify] 01/05: Import Upstream version 2.3.0

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Tue Oct 18 17:22:36 UTC 2016


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

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

commit 8a7c4f4a0ddfaf07eb1274e9c913178449a51327
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date:   Tue Oct 18 21:18:51 2016 +0530

    Import Upstream version 2.3.0
---
 .editorconfig        |  15 +++++
 .gitattributes       |   1 +
 .gitignore           |   1 +
 .travis.yml          |   5 ++
 index.js             |  68 +++++++++++++++++++++++
 license              |  21 +++++++
 optimization-test.js |  44 +++++++++++++++
 package.json         |  48 ++++++++++++++++
 readme.md            | 119 ++++++++++++++++++++++++++++++++++++++++
 test.js              | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 474 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8f9d77e
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{package.json,*.yml}]
+indent_style = space
+indent_size = 2
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..2a2f1c6
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - 'stable'
+  - '0.12'
+  - '0.10'
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..7c720eb
--- /dev/null
+++ b/index.js
@@ -0,0 +1,68 @@
+'use strict';
+
+var processFn = function (fn, P, opts) {
+	return function () {
+		var that = this;
+		var args = new Array(arguments.length);
+
+		for (var i = 0; i < arguments.length; i++) {
+			args[i] = arguments[i];
+		}
+
+		return new P(function (resolve, reject) {
+			args.push(function (err, result) {
+				if (err) {
+					reject(err);
+				} else if (opts.multiArgs) {
+					var results = new Array(arguments.length - 1);
+
+					for (var i = 1; i < arguments.length; i++) {
+						results[i - 1] = arguments[i];
+					}
+
+					resolve(results);
+				} else {
+					resolve(result);
+				}
+			});
+
+			fn.apply(that, args);
+		});
+	};
+};
+
+var pify = module.exports = function (obj, P, opts) {
+	if (typeof P !== 'function') {
+		opts = P;
+		P = Promise;
+	}
+
+	opts = opts || {};
+	opts.exclude = opts.exclude || [/.+Sync$/];
+
+	var filter = function (key) {
+		var match = function (pattern) {
+			return 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);
+	} : {};
+
+	return Object.keys(obj).reduce(function (ret, key) {
+		var x = obj[key];
+
+		ret[key] = typeof x === 'function' && filter(key) ? processFn(x, P, opts) : x;
+
+		return ret;
+	}, ret);
+};
+
+pify.all = pify;
diff --git a/license b/license
new file mode 100644
index 0000000..654d0bf
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+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:
+
+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.
diff --git a/optimization-test.js b/optimization-test.js
new file mode 100644
index 0000000..c883b7a
--- /dev/null
+++ b/optimization-test.js
@@ -0,0 +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('./');
+
+function assertOptimized(fn, name) {
+	var status = v8.getOptimizationStatus(fn);
+
+	switch (status) {
+		case 1:
+			// fn is optimized
+			return;
+		case 2:
+			assert(false, name + ' is not optimized (' + status + ')');
+		case 3:
+			// fn is always optimized
+			return;
+		case 4:
+			assert(false, name + ' is never optimized (' + status + ')');
+		case 6:
+			assert(false, name + ' is maybe deoptimized (' + status + ')');
+		default:
+			assert(false, 'unknown OptimizationStatus: ' + status + ' (' + name + ')');
+	}
+}
+
+var sut = fn({
+	unicorn: function (cb) {
+		cb(null, 'unicorn');
+	}
+}, Promise);
+
+sut.unicorn().then(function () {
+	v8.optimizeFunctionOnNextCall(sut.unicorn);
+
+	return sut.unicorn().then(function () {
+		assertOptimized(sut.unicorn, 'unicorn');
+	});
+}).catch(function (err) {
+	console.log(err.stack);
+	process.exit(1);
+});
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..311d198
--- /dev/null
+++ b/package.json
@@ -0,0 +1,48 @@
+{
+  "name": "pify",
+  "version": "2.3.0",
+  "description": "Promisify a callback-style function",
+  "license": "MIT",
+  "repository": "sindresorhus/pify",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "xo && ava && npm run optimization-test",
+    "optimization-test": "node --allow-natives-syntax optimization-test.js"
+  },
+  "files": [
+    "index.js"
+  ],
+  "keywords": [
+    "promise",
+    "promises",
+    "promisify",
+    "denodify",
+    "denodeify",
+    "callback",
+    "cb",
+    "node",
+    "then",
+    "thenify",
+    "convert",
+    "transform",
+    "wrap",
+    "wrapper",
+    "bind",
+    "to",
+    "async",
+    "es2015"
+  ],
+  "devDependencies": {
+    "ava": "*",
+    "pinkie-promise": "^1.0.0",
+    "v8-natives": "0.0.2",
+    "xo": "*"
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..c79ca8b
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,119 @@
+# pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify)
+
+> Promisify a callback-style function
+
+
+## Install
+
+```
+$ npm install --save pify
+```
+
+
+## Usage
+
+```js
+const fs = require('fs');
+const pify = require('pify');
+
+// 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
+
+pify(fs).readFile('package.json', 'utf8').then(data => {
+	console.log(JSON.parse(data).name);
+	//=> 'pify'
+});
+```
+
+
+## API
+
+### pify(input, [promiseModule], [options])
+
+Returns a promise wrapped version of the supplied function or module.
+
+#### input
+
+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`  
+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.
+
+```js
+const request = require('request');
+const pify = require('pify');
+
+pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
+	const [httpResponse, body] = result;
+});
+```
+
+##### include
+
+Type: `array` of (`string`|`regex`)
+
+Methods in a module to promisify. Remaining methods will be left untouched.
+
+##### exclude
+
+Type: `array` of (`string`|`regex`)  
+Default: `[/.+Sync$/]`
+
+Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
+
+##### excludeMain
+
+Type: `boolean`  
+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.
+
+```js
+const pify = require('pify');
+
+function fn() {
+	return true;
+}
+
+fn.method = (data, callback) => {
+	setImmediate(() => {
+		callback(data, null);
+	});
+};
+
+// promisify methods but not fn()
+const promiseFn = pify(fn, {excludeMain: true});
+
+if (promiseFn()) {
+	promiseFn.method('hi').then(data => {
+		console.log(data);
+	});
+}
+```
+
+
+## License
+
+MIT © [Sindre Sorhus](http://sindresorhus.com)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..183bd1a
--- /dev/null
+++ b/test.js
@@ -0,0 +1,152 @@
+'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');
+	});
+}
+
+function fixture4(cb) {
+	setImmediate(function () {
+		cb(null, 'unicorn');
+	});
+	return 'rainbow';
+}
+
+fixture4.meow = function (cb) {
+	setImmediate(function () {
+		cb(null, 'unicorn');
+	});
+};
+
+function fixture5() {
+	return 'rainbow';
+}
+
+var fixtureModule = {
+	method1: fixture,
+	method2: fixture,
+	method3: fixture5
+};
+
+test('main', function (t) {
+	t.is(typeof fn(fixture)().then, 'function');
+
+	return fn(fixture)().then(function (data) {
+		t.is(data, 'unicorn');
+	});
+});
+
+test('pass argument', function (t) {
+	return fn(fixture2)('rainbow').then(function (data) {
+		t.is(data, 'rainbow');
+	});
+});
+
+test('custom Promise module', function (t) {
+	return fn(fixture, pinkiePromise)().then(function (data) {
+		t.is(data, 'unicorn');
+	});
+});
+
+test('multiArgs option', function (t) {
+	return fn(fixture3, {multiArgs: true})().then(function (data) {
+		t.same(data, ['unicorn', 'rainbow']);
+	});
+});
+
+test('wrap core method', function (t) {
+	return fn(fs.readFile)('package.json').then(function (data) {
+		t.is(JSON.parse(data).name, 'pify');
+	});
+});
+
+test('module support', function (t) {
+	return fn(fs).readFile('package.json').then(function (data) {
+		t.is(JSON.parse(data).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 - preserves non-function members', function (t) {
+	var module = {
+		method: function () {},
+		nonMethod: 3
+	};
+
+	t.same(Object.keys(module), Object.keys(fn(module)));
+	t.end();
+});
+
+test('module support - transforms only members in opions.include', function (t) {
+	var pModule = fn(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, {
+		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, {
+		include: ['method1', 'method2'],
+		exclude: ['method2', '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 — function modules', function (t) {
+	var pModule = fn(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, {
+		excludeMain: true
+	});
+
+	t.is(typeof pModule.meow().then, 'function');
+	t.not(typeof pModule(function () {}).then, 'function');
+	t.end();
+});

-- 
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