mirror of
https://github.com/kennethreitz/bake.git
synced 2026-06-05 23:00:17 +00:00
cleanup
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
root=true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
|
||||
[*.js]
|
||||
indent_style = tab
|
||||
|
||||
[*.json]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"predef": ["describe", "it"]
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
/node_modules/
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
# Changelog
|
||||
|
||||
## v1.2.0
|
||||
|
||||
- Add UMD so that `denodeify` can be used in the browser
|
||||
|
||||
## v1.1.2
|
||||
|
||||
- Performance improvements, no API changes
|
||||
|
||||
## v1.1.1
|
||||
|
||||
- Add `'use strict';`
|
||||
|
||||
## v1.1.0
|
||||
|
||||
- First bower release
|
||||
|
||||
## v1.0.0
|
||||
|
||||
- First version of `denodeify`
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
denodeify [ ](https://codeship.io/projects/34622)
|
||||
=========
|
||||
|
||||
Tool to turn functions with Node-style callback APIs into functions that return [Promises](https://github.com/jakearchibald/es6-promise).
|
||||
|
||||
Inspired by and adapted from Q's [`Q.denodeify`/`Q.nfcall` function](https://github.com/kriskowal/q/wiki/API-Reference#qnfbindnodefunc-args).
|
||||
|
||||
Warning: This micro-library doesn't force you to use any particular Promise implementation by using whatever `Promise` has been defined as globally. This is so that you may use any ES6 standard Promise compliant library - or, of course, native ES6 Promises.
|
||||
|
||||
If you're running the code on a browser or node version that doesn't include native promises you will need to include a polyfill. The following polyfills are tested as part of this module's test suite:-
|
||||
- [Jake Archibald](https://twitter.com/jaffathecake)'s [ES6 Promise library](https://github.com/jakearchibald/es6-promise) (which is actually adapted from [Stefan Penner](https://twitter.com/stefanpenner)'s [RSVP.js](https://github.com/tildeio/rsvp.js)). - `require('es6-promise').polyfill();`
|
||||
- [Getify](https://twitter.com/getify)'s [Native Promise Only library](https://github.com/getify/native-promise-only) - `require('native-promise-only');`
|
||||
- [ES6 Shim](https://github.com/es-shims/es6-shim) - `require('es6-shim');`
|
||||
- [Calvin Metcalf](https://twitter.com/CWMma)'s [Lie](https://github.com/calvinmetcalf/lie) - `global.Promise = global.Promise || require('lie');`
|
||||
|
||||
Note: as of v1.2.0 you can use **denodeify** in the front end. Pull it in via CommonJS, AMD or simply add to your webpage and it'll be available on `window.denodeify`.
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install denodeify --save
|
||||
```
|
||||
|
||||
Or:-
|
||||
|
||||
```
|
||||
bower install denodeify --save
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
Simple example with [`readFile`](https://www.npmjs.org/package/read-file):-
|
||||
|
||||
```js
|
||||
require('es6-promise').polyfill();
|
||||
|
||||
var denodeify = require('denodeify');
|
||||
var readFile = denodeify(require('fs').readFile);
|
||||
|
||||
readFile('my-file.txt', { encoding: 'UTF-8' })
|
||||
.then(function(text) {
|
||||
console.log("My file's contents is: " + text);
|
||||
});
|
||||
```
|
||||
|
||||
(Note: you will need to also install [es6-promise](https://github.com/jakearchibald/es6-promise) with `npm install es6-promise` for this code sample to work within node versions that don't have `Promise` natively available)
|
||||
|
||||
More complex example with `exec`:-
|
||||
|
||||
## Advanced usage
|
||||
|
||||
You can also pass in a function as a second argument of `denodeify` that allows you to manipulate the data returned by the wrapped function before it gets passed to the Promise's `reject` or `resolve` functions, for example:-
|
||||
|
||||
```js
|
||||
require('es6-promise').polyfill();
|
||||
|
||||
var denodeify = require('denodeify');
|
||||
var exec = denodeify(require('child_process').exec, function(err, stdout, stderr) {
|
||||
|
||||
// Throw away stderr data
|
||||
return [err, stdout];
|
||||
});
|
||||
|
||||
exec('hostname')
|
||||
.then(function(host) {
|
||||
console.log("My hostname is: " + host.replace('\n', ''));
|
||||
});
|
||||
```
|
||||
|
||||
Or,
|
||||
|
||||
```js
|
||||
require('es6-promise').polyfill();
|
||||
|
||||
var denodeify = require('denodeify');
|
||||
var exec = denodeify(require('child_process').exec, function(err, stdout, stderr) {
|
||||
return [err, [stdout, stderr]];
|
||||
});
|
||||
|
||||
exec('my-command')
|
||||
.then(function(results) {
|
||||
console.log("stdout is: " + results[0]);
|
||||
console.log("stderr is: " + results[1]);
|
||||
});
|
||||
```
|
||||
|
||||
Useful for functions that return multiple arguments, for example [`child_process#exec`](http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback).
|
||||
|
||||
## Credits and collaboration ##
|
||||
|
||||
The lead developer of **denodeify** is [Matt Andrews](http://twitter.com/andrewsmatt) at FT Labs with much help and support from [Kornel Lesiński](https://twitter.com/pornelski). All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request.
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "denodeify",
|
||||
"main": "index.js",
|
||||
"homepage": "https://github.com/matthew-andrews/denodeify",
|
||||
"authors": [
|
||||
"Matthew Andrews <matt@mattandre.ws>"
|
||||
],
|
||||
"description": "Tool to turn functions with Node-style callback APIs into functions that return Promises.",
|
||||
"keywords": [
|
||||
"denodeify",
|
||||
"Promise",
|
||||
"node"
|
||||
],
|
||||
"license": "MIT",
|
||||
"ignore": [
|
||||
"**/.*",
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"test",
|
||||
".editorconfig"
|
||||
]
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
;(function(define){define(function(require,exports,module){
|
||||
|
||||
function denodeify(nodeStyleFunction, filter) {
|
||||
'use strict';
|
||||
|
||||
return function() {
|
||||
var self = this;
|
||||
var functionArguments = new Array(arguments.length + 1);
|
||||
|
||||
for (var i = 0; i < arguments.length; i += 1) {
|
||||
functionArguments[i] = arguments[i];
|
||||
}
|
||||
|
||||
function promiseHandler(resolve, reject) {
|
||||
function callbackFunction() {
|
||||
var args = new Array(arguments.length);
|
||||
|
||||
for (var i = 0; i < args.length; i += 1) {
|
||||
args[i] = arguments[i];
|
||||
}
|
||||
|
||||
if (filter) {
|
||||
args = filter.apply(self, args);
|
||||
}
|
||||
|
||||
var error = args[0];
|
||||
var result = args[1];
|
||||
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
|
||||
return resolve(result);
|
||||
}
|
||||
|
||||
functionArguments[functionArguments.length - 1] = callbackFunction;
|
||||
nodeStyleFunction.apply(self, functionArguments);
|
||||
}
|
||||
|
||||
return new Promise(promiseHandler);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = denodeify;
|
||||
|
||||
});})(typeof define=='function'&&define.amd?define
|
||||
:(function(n,w){'use strict';return typeof module=='object'?function(c){
|
||||
c(require,exports,module);}:function(c){var m={exports:{}};c(function(n){
|
||||
return w[n];},m.exports,m);w[n]=m.exports;};})('denodeify',this));
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "denodeify",
|
||||
"version": "1.2.1",
|
||||
"description": "Tool to turn functions with Node-style callback APIs into functions that return Promises",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"files": "find . -name '*.js' ! -path './node_modules/*'",
|
||||
"jshint": "./node_modules/.bin/jshint `npm run -s files`",
|
||||
"lintspaces": "./node_modules/.bin/lintspaces -i js-comments -e .editorconfig `npm run -s files`",
|
||||
"test": "./node_modules/.bin/mocha test/*test.js && npm run jshint && npm run lintspaces"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/matthew-andrews/denodeify.git"
|
||||
},
|
||||
"author": "Matt Andrews",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/matthew-andrews/denodeify/issues"
|
||||
},
|
||||
"homepage": "https://github.com/matthew-andrews/denodeify",
|
||||
"devDependencies": {
|
||||
"es6-promise": "^1.0.0",
|
||||
"es6-shim": "^0.18.0",
|
||||
"jshint": "^2.5.5",
|
||||
"lie": "^2.7.7",
|
||||
"lintspaces-cli": "0.0.4",
|
||||
"mocha": "^1.21.4",
|
||||
"native-promise-only": "^0.7.6-a"
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
delete global.Promise;
|
||||
require('es6-promise').polyfill();
|
||||
|
||||
var helpers = require('./helpers');
|
||||
|
||||
describe('denodeify with es6 promise', helpers.basicDenodeify);
|
||||
describe('denodeify with es6 promise using multiple arguments', helpers.multipleArguments);
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
delete global.Promise;
|
||||
require('es6-shim');
|
||||
|
||||
var helpers = require('./helpers');
|
||||
|
||||
describe('denodeify with es6 shim', helpers.basicDenodeify);
|
||||
describe('denodeify with es6 shim using multiple arguments', helpers.multipleArguments);
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
var assert = require('assert');
|
||||
var denodeify = require('../');
|
||||
|
||||
function myNodeStyleFunction(argument1, argument2, callback) {
|
||||
if (argument1 && argument2) {
|
||||
callback(null, argument1+argument2);
|
||||
} else {
|
||||
callback('Need both arguments');
|
||||
}
|
||||
}
|
||||
|
||||
exports.basicDenodeify = function() {
|
||||
it('should resolve when there are no errors', function(done) {
|
||||
var myDenodeifiedNodeStyleFunction = denodeify(myNodeStyleFunction);
|
||||
myDenodeifiedNodeStyleFunction(1, 2)
|
||||
.then(function(result) {
|
||||
assert.equal(3, result);
|
||||
done();
|
||||
}, function() {
|
||||
throw new Error('Error callback called wrongly');
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject when there are errors', function(done) {
|
||||
var myDenodeifiedNodeStyleFunction = denodeify(myNodeStyleFunction);
|
||||
var promise = myDenodeifiedNodeStyleFunction(1, undefined);
|
||||
assert(promise instanceof Promise);
|
||||
promise
|
||||
.then(function(result) {
|
||||
throw new Error('A Promised myNodeStyleFunction with one argument should never resolve');
|
||||
}, function(error) {
|
||||
assert.equal(error, 'Need both arguments');
|
||||
done();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function multipleArgumentsNodeStyleFunction(callback) {
|
||||
callback(null, 'a', 'b');
|
||||
}
|
||||
|
||||
function myFilter(err, a, b) {
|
||||
return [err, [a, b]];
|
||||
}
|
||||
|
||||
exports.multipleArguments = function() {
|
||||
it('should pass multiple arguments to the next then', function(done) {
|
||||
var myDenodeifiedNodeStyleFunction = denodeify(multipleArgumentsNodeStyleFunction, myFilter);
|
||||
myDenodeifiedNodeStyleFunction()
|
||||
.then(function(results) {
|
||||
assert.equal(results[0], 'a');
|
||||
assert.equal(results[1], 'b');
|
||||
done();
|
||||
});
|
||||
});
|
||||
};
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
delete global.Promise;
|
||||
global.Promise = require('lie');
|
||||
|
||||
var helpers = require('./helpers');
|
||||
|
||||
describe('denodeify with lie', helpers.basicDenodeify);
|
||||
describe('denodeify with lie using multiple arguments', helpers.multipleArguments);
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
delete global.Promise;
|
||||
require("native-promise-only");
|
||||
|
||||
var helpers = require('./helpers');
|
||||
|
||||
describe('denodeify with native promise only', helpers.basicDenodeify);
|
||||
describe('denodeify with native promise only using multiple arguments', helpers.multipleArguments);
|
||||
Reference in New Issue
Block a user