Skip to content

Commit

Permalink
Add suffix option
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
dougwilson committed Sep 23, 2014
1 parent b6d4723 commit 77c5f56
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Add `suffix` option

2.1.0 / 2014-09-16
==================

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ milliseconds, defaults to `3` (ex: `2.300ms`).

The name of the header to set, defaults to `X-Response-Time`.

##### suffix

Boolean to indicate if units of measurement suffix should be added to
the output, defaults to `true` (ex: `2.300ms` vs `2.300`).

## Examples

### express/connect
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ module.exports = function responseTime(options) {
// header name
var header = options.header || 'X-Response-Time'

// display suffix
var suffix = options.suffix !== undefined
? Boolean(options.suffix)
: true

return function responseTime(req, res, next) {
var startAt = process.hrtime()

Expand All @@ -52,8 +57,13 @@ module.exports = function responseTime(options) {

var diff = process.hrtime(startAt)
var ms = diff[0] * 1e3 + diff[1] * 1e-6
var val = ms.toFixed(digits)

if (suffix) {
val += 'ms'
}

this.setHeader(header, ms.toFixed(digits) + 'ms')
this.setHeader(header, val)
})

next()
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ describe('responseTime(options)', function () {
.expect('X-Time-Taken', /^[0-9\.]+ms$/, done)
})
})

describe('with "suffix" option', function () {
it('should default to true', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9\.]+ms$/, done)
})

it('should allow disabling suffix', function (done) {
var server = createServer({ suffix: false })
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9\.]+$/, done)
})
})
})

function createServer(opts, fn) {
Expand Down

0 comments on commit 77c5f56

Please sign in to comment.