Skip to content

Commit

Permalink
extract shared utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip9587 committed Oct 22, 2024
1 parent a5055df commit 623e6d6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 100 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
=========================

* extract shared utility functions
* remove `unpipe` package and use native `unpipe()` method

2.0.1 / 2024-09-10
Expand Down
30 changes: 1 addition & 29 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/

var bytes = require('bytes')
var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:json')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var { getCharset, typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -196,21 +196,6 @@ function firstchar (str) {
: undefined
}

/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/

function getCharset (req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}

/**
* Normalize a SyntaxError for JSON.parse.
*
Expand All @@ -235,16 +220,3 @@ function normalizeJsonSyntaxError (error, obj) {

return error
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
14 changes: 1 addition & 13 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var debug = require('debug')('body-parser:raw')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var { typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -89,16 +90,3 @@ function raw (options) {
})
}
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
30 changes: 1 addition & 29 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/

var bytes = require('bytes')
var contentType = require('content-type')
var debug = require('debug')('body-parser:text')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var { getCharset, typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -94,31 +94,3 @@ function text (options) {
})
}
}

/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/

function getCharset (req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
30 changes: 1 addition & 29 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
*/

var bytes = require('bytes')
var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:urlencoded')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')
var qs = require('qs')
var { getCharset, typeChecker } = require('../utils')

/**
* Module exports.
Expand Down Expand Up @@ -184,21 +184,6 @@ function createQueryParser (options, extended) {
}
}

/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/

function getCharset (req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}

/**
* Count the number of parameters, stopping once limit reached
*
Expand All @@ -222,16 +207,3 @@ function parameterCount (body, limit) {

return count
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

function typeChecker (type) {
return function checkType (req) {
return Boolean(typeis(req, type))
}
}
51 changes: 51 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/

'use strict'

/**
* Module dependencies.
*/

var contentType = require('content-type')
var typeis = require('type-is')

/**
* Module exports.
*/

module.exports = {
getCharset,
typeChecker
}

/**
* Get the charset of a request.
*
* @param {object} req
* @api private
*/

function getCharset (req) {
try {
return (contentType.parse(req).parameters.charset || '').toLowerCase()
} catch (e) {
return undefined
}
}

/**
* Get the simple type checker.
*
* @param {string} type
* @return {function}
*/

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

0 comments on commit 623e6d6

Please sign in to comment.