Skip to content

Commit

Permalink
lint: use standard style
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 20, 2016
1 parent b4dc075 commit bd386d3
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 358 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cache:
- node_modules
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
Expand All @@ -24,5 +24,6 @@ script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ Object.defineProperty(exports, 'urlencoded', {
* @public
*/

function bodyParser(options){
function bodyParser (options) {
var opts = {}

// exclude type option
if (options) {
for (var prop in options) {
if ('type' !== prop) {
if (prop !== 'type') {
opts[prop] = options[prop]
}
}
Expand All @@ -105,11 +105,11 @@ function bodyParser(options){
var _urlencoded = exports.urlencoded(opts)
var _json = exports.json(opts)

return function bodyParser(req, res, next) {
_json(req, res, function(err){
if (err) return next(err);
_urlencoded(req, res, next);
});
return function bodyParser (req, res, next) {
_json(req, res, function (err) {
if (err) return next(err)
_urlencoded(req, res, next)
})
}
}

Expand All @@ -118,8 +118,8 @@ function bodyParser(options){
* @private
*/

function createParserGetter(name) {
return function get() {
function createParserGetter (name) {
return function get () {
return loadParser(name)
}
}
Expand All @@ -129,7 +129,7 @@ function createParserGetter(name) {
* @private
*/

function loadParser(parserName) {
function loadParser (parserName) {
var parser = parsers[parserName]

if (parser !== undefined) {
Expand All @@ -153,5 +153,5 @@ function loadParser(parserName) {
}

// store to prevent invoking require()
return parsers[parserName] = parser
return (parsers[parserName] = parser)
}
8 changes: 4 additions & 4 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = read
* @api private
*/

function read(req, res, next, parse, debug, options) {
function read (req, res, next, parse, debug, options) {
var length
var opts = options || {}
var stream
Expand Down Expand Up @@ -87,7 +87,7 @@ function read(req, res, next, parse, debug, options) {

// read off entire request
stream.resume()
onFinished(req, function onfinished() {
onFinished(req, function onfinished () {
next(err)
})
return
Expand Down Expand Up @@ -140,7 +140,7 @@ function read(req, res, next, parse, debug, options) {
* @api private
*/

function contentstream(req, debug, inflate) {
function contentstream (req, debug, inflate) {
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
var length = req.headers['content-length']
var stream
Expand Down Expand Up @@ -180,7 +180,7 @@ function contentstream(req, debug, inflate) {
* @private
*/

function setErrorStatus(error, status) {
function setErrorStatus (error, status) {
if (!error.status && !error.statusCode) {
error.status = status
error.statusCode = status
Expand Down
31 changes: 18 additions & 13 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = json
* %x0D ) ; Carriage return
*/

var firstcharRegExp = /^[\x20\x09\x0a\x0d]*(.)/
var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*(.)/ // eslint-disable-line no-control-regex

/**
* Create a middleware to parse JSON bodies.
Expand All @@ -47,7 +47,7 @@ var firstcharRegExp = /^[\x20\x09\x0a\x0d]*(.)/
* @public
*/

function json(options) {
function json (options) {
var opts = options || {}

var limit = typeof opts.limit !== 'number'
Expand All @@ -68,7 +68,7 @@ function json(options) {
? typeChecker(type)
: type

function parse(body) {
function parse (body) {
if (body.length === 0) {
// special-case empty json body, as it's a common client-side mistake
// TODO: maybe make this configurable or part of "strict" option
Expand All @@ -88,23 +88,29 @@ function json(options) {
return JSON.parse(body, reviver)
}

return function jsonParser(req, res, next) {
return function jsonParser (req, res, next) {
if (req._body) {
return debug('body already parsed'), next()
debug('body already parsed')
next()
return
}

req.body = req.body || {}

// skip requests without bodies
if (!typeis.hasBody(req)) {
return debug('skip empty body'), next()
debug('skip empty body')
next()
return
}

debug('content-type %j', req.headers['content-type'])

// determine if request should be parsed
if (!shouldParse(req)) {
return debug('skip parsing'), next()
debug('skip parsing')
next()
return
}

// assert charset per RFC 7159 sec 8.1
Expand Down Expand Up @@ -135,9 +141,8 @@ function json(options) {
* @api public
*/


function firstchar(str) {
var match = firstcharRegExp.exec(str)
function firstchar (str) {
var match = FIRST_CHAR_REGEXP.exec(str)
return match ? match[1] : ''
}

Expand All @@ -148,7 +153,7 @@ function firstchar(str) {
* @api private
*/

function getCharset(req) {
function getCharset (req) {
try {
return contentType.parse(req).parameters.charset.toLowerCase()
} catch (e) {
Expand All @@ -163,8 +168,8 @@ function getCharset(req) {
* @return {function}
*/

function typeChecker(type) {
return function checkType(req) {
function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
24 changes: 15 additions & 9 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ module.exports = raw
* @api public
*/

function raw(options) {
var opts = options || {};
function raw (options) {
var opts = options || {}

var inflate = opts.inflate !== false
var limit = typeof opts.limit !== 'number'
Expand All @@ -48,27 +48,33 @@ function raw(options) {
? typeChecker(type)
: type

function parse(buf) {
function parse (buf) {
return buf
}

return function rawParser(req, res, next) {
return function rawParser (req, res, next) {
if (req._body) {
return debug('body already parsed'), next()
debug('body already parsed')
next()
return
}

req.body = req.body || {}

// skip requests without bodies
if (!typeis.hasBody(req)) {
return debug('skip empty body'), next()
debug('skip empty body')
next()
return
}

debug('content-type %j', req.headers['content-type'])

// determine if request should be parsed
if (!shouldParse(req)) {
return debug('skip parsing'), next()
debug('skip parsing')
next()
return
}

// read
Expand All @@ -88,8 +94,8 @@ function raw(options) {
* @return {function}
*/

function typeChecker(type) {
return function checkType(req) {
function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
24 changes: 15 additions & 9 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = text
* @api public
*/

function text(options) {
function text (options) {
var opts = options || {}

var defaultCharset = opts.defaultCharset || 'utf-8'
Expand All @@ -50,27 +50,33 @@ function text(options) {
? typeChecker(type)
: type

function parse(buf) {
function parse (buf) {
return buf
}

return function textParser(req, res, next) {
return function textParser (req, res, next) {
if (req._body) {
return debug('body already parsed'), next()
debug('body already parsed')
next()
return
}

req.body = req.body || {}

// skip requests without bodies
if (!typeis.hasBody(req)) {
return debug('skip empty body'), next()
debug('skip empty body')
next()
return
}

debug('content-type %j', req.headers['content-type'])

// determine if request should be parsed
if (!shouldParse(req)) {
return debug('skip parsing'), next()
debug('skip parsing')
next()
return
}

// get charset
Expand All @@ -93,7 +99,7 @@ function text(options) {
* @api private
*/

function getCharset(req) {
function getCharset (req) {
try {
return contentType.parse(req).parameters.charset.toLowerCase()
} catch (e) {
Expand All @@ -108,8 +114,8 @@ function getCharset(req) {
* @return {function}
*/

function typeChecker(type) {
return function checkType(req) {
function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
Loading

0 comments on commit bd386d3

Please sign in to comment.