trough
Advanced tools
Comparing version 1.0.0 to 1.0.1
63
index.js
@@ -1,11 +0,1 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2016 Titus Wormer | ||
* @license MIT | ||
* @module trough | ||
* @fileoverview Middleware. Inspired by `segmentio/ware`, | ||
* but able to change the values from transformer to | ||
* transformer. | ||
*/ | ||
'use strict'; | ||
@@ -19,7 +9,3 @@ | ||
/** | ||
* Create new middleware. | ||
* | ||
* @return {Object} - Middlewre. | ||
*/ | ||
/* Create new middleware. */ | ||
function trough() { | ||
@@ -34,8 +20,4 @@ var fns = []; | ||
/** | ||
* Run `fns`. Last argument must be | ||
* a completion handler. | ||
* | ||
* @param {...*} input - Parameters | ||
*/ | ||
/* Run `fns`. Last argument must be | ||
* a completion handler. */ | ||
function run() { | ||
@@ -52,10 +34,3 @@ var index = -1; | ||
return; | ||
/** | ||
* Run the next `fn`, if any. | ||
* | ||
* @param {Error?} err - Failure. | ||
* @param {...*} values - Other input. | ||
*/ | ||
/* Run the next `fn`, if any. */ | ||
function next(err) { | ||
@@ -91,7 +66,3 @@ var fn = fns[++index]; | ||
/** | ||
* Add `fn` to the list. | ||
* | ||
* @param {Function} fn - Anything `wrap` accepts. | ||
*/ | ||
/* Add `fn` to the list. */ | ||
function use(fn) { | ||
@@ -108,11 +79,5 @@ if (typeof fn !== 'function') { | ||
/** | ||
* Wrap `fn`. Can be sync or async; return a promise, | ||
/* Wrap `fn`. Can be sync or async; return a promise, | ||
* receive a completion handler, return new values and | ||
* errors. | ||
* | ||
* @param {Function} fn - Thing to wrap. | ||
* @param {Function} next - Completion handler. | ||
* @return {Function} - Wrapped `fn`. | ||
*/ | ||
* errors. */ | ||
function wrap(fn, next) { | ||
@@ -159,7 +124,3 @@ var invoked; | ||
/** | ||
* Invoke `next`, only once. | ||
* | ||
* @param {Error?} err - Optional error. | ||
*/ | ||
/* Invoke `next`, only once. */ | ||
function done() { | ||
@@ -173,8 +134,4 @@ if (!invoked) { | ||
/** | ||
* Invoke `done` with one value. | ||
* Tracks if an error is passed, too. | ||
* | ||
* @param {*} value - Optional value. | ||
*/ | ||
/* Invoke `done` with one value. | ||
* Tracks if an error is passed, too. */ | ||
function then(value) { | ||
@@ -181,0 +138,0 @@ done(null, value); |
{ | ||
"name": "trough", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Middleware: a channel used to convey a liquid", | ||
@@ -10,3 +10,2 @@ "license": "MIT", | ||
], | ||
"dependencies": {}, | ||
"repository": "https://github.com/wooorm/trough", | ||
@@ -18,23 +17,17 @@ "bugs": "https://github.com/wooorm/trough/issues", | ||
], | ||
"engines": { | ||
"node": ">=0.11.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"browserify": "^13.0.0", | ||
"browserify": "^14.1.0", | ||
"esmangle": "^1.0.0", | ||
"nyc": "^7.1.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-toc": "^3.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"nyc": "^11.0.0", | ||
"remark-cli": "^3.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.16.0" | ||
"xo": "^0.18.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail", | ||
"build-md": "remark . -qfo", | ||
"build-bundle": "browserify index.js -s trough > trough.js", | ||
@@ -56,31 +49,16 @@ "build-mangle": "esmangle trough.js > trough.min.js", | ||
"space": true, | ||
"esnext": false, | ||
"rules": { | ||
"guard-for-in": "off", | ||
"max-lines": "off" | ||
"unicorn/prefer-type-error": "off" | ||
}, | ||
"ignores": [ | ||
"trough.js", | ||
"trough.min.js" | ||
"trough.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"plugins": { | ||
"comment-config": null, | ||
"lint": { | ||
"heading-increment": false, | ||
"no-duplicate-headings": false, | ||
"list-item-spacing": false | ||
}, | ||
"github": null, | ||
"toc": { | ||
"tight": true, | ||
"maxDepth": 3 | ||
}, | ||
"validate-links": null | ||
}, | ||
"settings": { | ||
"bullet": "*" | ||
} | ||
"plugins": [ | ||
"preset-wooorm" | ||
] | ||
} | ||
} |
100
readme.md
@@ -5,3 +5,3 @@ # trough [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
**trough** is like [`ware`][ware] with less sugar, and middleware | ||
`trough` is like [`ware`][ware] with less sugar, and middleware | ||
functions can change the input of the next. | ||
@@ -54,8 +54,8 @@ | ||
Error: Expected file | ||
at ~/example.js:18:12 | ||
at wrapped (~/node_modules/trough/index.js:120:19) | ||
at next (~/node_modules/trough/index.js:77:24) | ||
at done (~/node_modules/trough/index.js:157:12) | ||
at ~/example.js:11:7 | ||
at FSReqWrap.oncomplete (fs.js:123:15) | ||
at ~/example.js:21:12 | ||
at wrapped (~/node_modules/trough/index.js:93:19) | ||
at next (~/node_modules/trough/index.js:56:24) | ||
at done (~/node_modules/trough/index.js:124:12) | ||
at ~/node_modules/example.js:14:7 | ||
at FSReqWrap.oncomplete (fs.js:153:5) | ||
null <Buffer 23 20 74 72 6f 75 67 68 20 5b 21 5b 42 75 69 6c 64 20 53 74 61 74 75 73 5d 5b 74 72 61 76 69 73 2d 62 61 64 67 65 5d 5d 5b 74 72 61 76 69 73 5d 20 5b ... > | ||
@@ -113,5 +113,7 @@ ``` | ||
trough().use(function (val) { | ||
return new Error('Got: ' + val); | ||
}).run('some value', console.log); | ||
trough() | ||
.use(function (val) { | ||
return new Error('Got: ' + val); | ||
}) | ||
.run('some value', console.log); | ||
``` | ||
@@ -123,3 +125,3 @@ | ||
Error: Got: some value | ||
at ~example.js:6:10 | ||
at ~/example.js:5:12 | ||
... | ||
@@ -133,5 +135,7 @@ ``` | ||
trough().use(function (val) { | ||
throw new Error('Got: ' + val); | ||
}).run('more value', console.log); | ||
trough() | ||
.use(function (val) { | ||
throw new Error('Got: ' + val); | ||
}) | ||
.run('more value', console.log); | ||
``` | ||
@@ -143,3 +147,3 @@ | ||
Error: Got: more value | ||
at ~example.js:6:10 | ||
at ~/example.js:5:11 | ||
... | ||
@@ -153,5 +157,7 @@ ``` | ||
trough().use(function (val) { | ||
return 'even ' + val; | ||
}).run('more value', 'untouched', console.log); | ||
trough() | ||
.use(function (val) { | ||
return 'even ' + val; | ||
}) | ||
.run('more value', 'untouched', console.log); | ||
``` | ||
@@ -179,7 +185,11 @@ | ||
```js | ||
trough().use(function (val) { | ||
return new Promise(function (resolve, reject) { | ||
reject('Got: ' + val); | ||
}); | ||
}).run('val', console.log); | ||
var trough = require('trough'); | ||
trough() | ||
.use(function (val) { | ||
return new Promise(function (resolve, reject) { | ||
reject('Got: ' + val); | ||
}); | ||
}) | ||
.run('val', console.log); | ||
``` | ||
@@ -197,7 +207,13 @@ | ||
```js | ||
trough().use(function (val) { | ||
return new Promise(function (resolve, reject) { | ||
setTimeout(function () { resolve(null); }, 100); | ||
}); | ||
}).run('Input', console.log); | ||
var trough = require('trough'); | ||
trough() | ||
.use(function () { | ||
return new Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve(null); | ||
}, 100); | ||
}); | ||
}) | ||
.run('Input', console.log); | ||
``` | ||
@@ -217,3 +233,3 @@ | ||
If `next` is given a a value (neither `null` nor `undefined`) as its first | ||
If `next` is given a value (neither `null` nor `undefined`) as its first | ||
argument, the pipeline fails and `done` is invoked with that value. | ||
@@ -231,5 +247,9 @@ | ||
```js | ||
trough().use(function (val, next) { | ||
next(new Error('Got: ' + val)); | ||
}).run('val', console.log); | ||
var trough = require('trough'); | ||
trough() | ||
.use(function (val, next) { | ||
next(new Error('Got: ' + val)); | ||
}) | ||
.run('val', console.log); | ||
``` | ||
@@ -241,3 +261,3 @@ | ||
Error: Got: val | ||
at ~/example.js:6:8 | ||
at ~/example.js:5:10 | ||
``` | ||
@@ -248,7 +268,11 @@ | ||
```js | ||
trough().use(function (val, next) { | ||
setTimeout(function () { | ||
next(null, null, 'values'); | ||
}, 100); | ||
}).run('some', console.log); | ||
var trough = require('trough'); | ||
trough() | ||
.use(function (val, next) { | ||
setTimeout(function () { | ||
next(null, null, 'values'); | ||
}, 100); | ||
}) | ||
.run('some', console.log); | ||
``` | ||
@@ -255,0 +279,0 @@ |
7
306
11728
4
107