Skip to content

Commit

Permalink
Merge pull request #41 from elliotttf/semantic-release
Browse files Browse the repository at this point in the history
chore(semantic-release): Adding semantic release
  • Loading branch information
elliotttf committed Nov 9, 2016
2 parents 4002671 + 2fe0f7e commit 8a76666
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 162 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# node_modules ignored by default

# Ignore generated files
coverage
newrelic.js
80 changes: 0 additions & 80 deletions .eslintrc

This file was deleted.

28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "script"
},
"extends": "airbnb-base",
"rules": {
"brace-style": ["error", "stroustrup"],
"max-len": ["warn", {
"code": 100,
"comments": 80,
"ignoreUrls": true,
"ignorePattern": "(logger\\.|new Error\\)|new TypeError\\()",
"ignoreTrailingComments": true,
"tabWidth": 2
}],
"no-warning-comments": "warn",
"quotes": ["error", "single", {"avoidEscape": true, "allowTemplateLiterals": true}],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}],
"valid-jsdoc": "error"
}
}
25 changes: 18 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
language: node_js
sudo: false
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- "0.12"
- "4"
- "node"
script: npm test && npm run coverage
after_success: npm run coveralls

- '6'
before_install:
- npm i -g npm@^2.0.0
before_script:
- npm prune
script: npm run lint && npm t
after_success:
- npm run coveralls
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
24 changes: 12 additions & 12 deletions lib/conditional.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var once = require('once');
'use strict';

const once = require('once');

/**
* Returns a middleware that can be used to conditionally execute another
Expand Down Expand Up @@ -29,17 +31,15 @@ var once = require('once');
* }
* ));
*/
module.exports = function (condition, success, fail) {
return function (req, res, next) {
next = once(next);
if (condition === true || (typeof condition === 'function' && condition(req, res, next))) {
return success(req, res, next);
}
if (fail) {
return fail(req, res, next);
}
module.exports = (condition, success, fail) => (req, res, next) => {
const nextOnce = once(next);
if (condition === true || (typeof condition === 'function' && condition(req, res, nextOnce))) {
return success(req, res, nextOnce);
}
if (fail) {
return fail(req, res, nextOnce);
}

return next();
};
return nextOnce();
};

18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"name": "express-conditional-middleware",
"version": "1.1.2",
"description": "Allow express middlewares to be overridden based on a condition.",
"repository": "https://github.com/elliotttf/express-conditional-middleware",
"repository": {
"type": "git",
"url": "https://github.com/elliotttf/express-conditional-middleware.git"
},
"main": "lib/conditional.js",
"scripts": {
"test": "istanbul cover --print both nodeunit ./test/index.js",
"lint": "eslint .",
"coverage": "istanbul check-coverage --statements 100 --lines 100 --branches 100 --functions 100",
"coveralls": "cat ./coverage/lcov.info | coveralls"
"coveralls": "cat ./coverage/lcov.info | coveralls",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"author": "Elliott Foster <elliottf@codebrews.com> (http://codebrews.com/)",
"license": "MIT",
Expand All @@ -17,10 +20,13 @@
},
"devDependencies": {
"coveralls": "^2.11.6",
"eslint": "^2.2.0 <3",
"eslint": "^3.9.1",
"eslint-config-airbnb-base": "^10.0.1",
"eslint-plugin-import": "^2.2.0",
"ghooks": "^1.2.4",
"istanbul": "^0.4.2",
"nodeunit": "^0.10.2"
"nodeunit": "^0.10.2",
"semantic-release": "^4.3.5"
},
"config": {
"ghooks": {
Expand All @@ -29,6 +35,6 @@
}
},
"engines": {
"node": ">=0.12"
"node": ">=6"
}
}
92 changes: 36 additions & 56 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,85 @@
var conditional = require('../lib/conditional.js');
'use strict';

const conditional = require('../lib/conditional.js');

module.exports = {
bool: function (test) {
bool(test) {
test.expect(3);

var middleware = conditional(true, function () {
let middleware = conditional(true, () => {
test.ok(true, 'true conditional worked.');
});

middleware({}, {}, function () {
middleware({}, {}, () => {
test.done();
});

middleware = conditional(false, function () {
middleware = conditional(false, () => {
test.done();
});

middleware({}, {}, function () {
middleware({}, {}, () => {
test.ok(true, 'false conditional worked.');
});

middleware = conditional(false, function () {
middleware = conditional(false, () => {
test.done();
}, function () {
}, () => {
test.ok(true, 'fail conditional worked.');
test.done();
});

middleware({}, {}, function () {
middleware({}, {}, () => {
test.done();
});
},

func: function (test) {
func(test) {
test.expect(3);

var conditionFunc = function (req) {
return req.working === true;
};
const conditionFunc = req => req.working === true;

var middleware = conditional(
conditionFunc,
function () {
test.ok(true, 'Conditional function returned true.');
}
);
let middleware = conditional(conditionFunc, () => test.ok(true,
'Conditional function returned true.'));

middleware({working: true}, {}, function () {
test.done();
});
middleware({ working: true }, {}, () => test.done());

middleware = conditional(
conditionFunc,
function () {
test.done();
}
);
middleware = conditional(conditionFunc, () => test.done());

middleware({working: false}, {}, function () {
middleware({ working: false }, {}, () => {
test.ok(true, 'Conditional function returned false.');
});

middleware = conditional(
conditionFunc,
function () {
test.done();
},
function () {
test.ok(true, 'Conditional function returned false, executed otherwise.');
test.done();
}
);

middleware({working: false}, {}, function () {
middleware = conditional(conditionFunc, () => test.done(), () => {
test.ok(true, 'Conditional function returned false, executed otherwise.');
test.done();
});

middleware({ working: false }, {}, () => {
test.done();
});
},

funcOnce: function (test) {
funcOnce(test) {
test.expect(2);

var count = 0;
let count = 0;

var conditionFunc = function (req, res, next) {
const conditionFunc = (req, res, next) => {
next();
return true;
};

var middleware = conditional(
conditionFunc,
function (req, res, next) {
test.ok(true, 'Conditional executed.');
next();
test.done();
}
);

middleware({}, {}, function () {
count++;
const middleware = conditional(conditionFunc, (req, res, next) => {
test.ok(true, 'Conditional executed.');
next();
test.done();
});

middleware({}, {}, () => {
count += 1;
test.equal(count, 1, 'Middleware only executed once.');
});
}
},
};

0 comments on commit 8a76666

Please sign in to comment.