Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 15, 2018
1 parent bddb87a commit 9d17fe7
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 397 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
trough.js
trough.min.js
84 changes: 42 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
'use strict';
'use strict'

/* Expose. */
module.exports = trough;
module.exports = trough

/* Methods. */
var slice = [].slice;
var slice = [].slice

/* Create new middleware. */
function trough() {
var fns = [];
var middleware = {};
var fns = []
var middleware = {}

middleware.run = run;
middleware.use = use;
middleware.run = run
middleware.use = use

return middleware;
return middleware

/* Run `fns`. Last argument must be
* a completion handler. */
function run() {
var index = -1;
var input = slice.call(arguments, 0, -1);
var done = arguments[arguments.length - 1];
var index = -1
var input = slice.call(arguments, 0, -1)
var done = arguments[arguments.length - 1]

if (typeof done !== 'function') {
throw new Error('Expected function as last argument, not ' + done);
throw new Error('Expected function as last argument, not ' + done)
}

next.apply(null, [null].concat(input));
next.apply(null, [null].concat(input))

/* Run the next `fn`, if any. */
function next(err) {
var fn = fns[++index];
var params = slice.call(arguments, 0);
var values = params.slice(1);
var length = input.length;
var pos = -1;
var fn = fns[++index]
var params = slice.call(arguments, 0)
var values = params.slice(1)
var length = input.length
var pos = -1

if (err) {
done(err);
return;
done(err)
return
}

/* Copy non-nully input into values. */
while (++pos < length) {
if (values[pos] === null || values[pos] === undefined) {
values[pos] = input[pos];
values[pos] = input[pos]
}
}

input = values;
input = values

/* Next or done. */
if (fn) {
wrap(fn, next).apply(null, input);
wrap(fn, next).apply(null, input)
} else {
done.apply(null, [null].concat(input));
done.apply(null, [null].concat(input))
}
}
}

/* Add `fn` to the list. */
function use(fn) {
if (typeof fn !== 'function') {
throw new Error('Expected `fn` to be a function, not ' + fn);
throw new Error('Expected `fn` to be a function, not ' + fn)
}

fns.push(fn);
fns.push(fn)

return middleware;
return middleware
}
}

/* Wrap `fn`. Can be sync or async; return a promise,
* receive a completion handler, return new values and
* errors. */
function wrap(fn, next) {
var invoked;
var invoked

return wrapped;
return wrapped

function wrapped() {
var params = slice.call(arguments, 0);
var callback = fn.length > params.length;
var result;
var params = slice.call(arguments, 0)
var callback = fn.length > params.length
var result

if (callback) {
params.push(done);
params.push(done)
}

try {
result = fn.apply(null, params);
result = fn.apply(null, params)
} catch (err) {
/* Well, this is quite the pickle. `fn` received
* a callback and invoked it (thus continuing the
Expand All @@ -99,35 +99,35 @@ function wrap(fn, next) {
* so the only thing left to do is to throw the
* thing instea. */
if (callback && invoked) {
throw err;
throw err
}

return done(err);
return done(err)
}

if (!callback) {
if (result && typeof result.then === 'function') {
result.then(then, done);
result.then(then, done)
} else if (result instanceof Error) {
done(result);
done(result)
} else {
then(result);
then(result)
}
}
}

/* Invoke `next`, only once. */
function done() {
if (!invoked) {
invoked = true;
invoked = true

next.apply(null, arguments);
next.apply(null, arguments)
}
}

/* Invoke `done` with one value.
* Tracks if an error is passed, too. */
function then(value) {
done(null, value);
done(null, value)
}
}
28 changes: 18 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,31 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.0",
"nyc": "^11.0.0",
"prettier": "^1.12.0",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.4.0",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js -s trough > trough.js",
"build-mangle": "esmangle trough.js > trough.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"guard-for-in": "off",
Expand All @@ -59,5 +61,11 @@
"plugins": [
"preset-wooorm"
]
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
}
}
Loading

0 comments on commit 9d17fe7

Please sign in to comment.